Basic UNIX commands
Know more about a cmd use $man command
Directory
- pwd
Show your Present Working Directory
- mkdir
Make a directory
- cd dirname
Change directory
- cd
Return to your home directory
- cd ..
Return to the upper directory
- cd /
Return to the root directory
- ls | grep extension
Will show you all files with the 'extension'
- ll | grep string
Will show you all files with the 'string'
- grep string filename(s)/grep [options] pattern [files]
Looks for the string in the files (txt file typically).
e.g.,
$grep python grep_example.txt
txt file (cat grep_example.txt)
-c : This prints only a count of the lines that match a pattern
-h : Display the matched lines, but do not display the filenames.
-i : Ignores, case for matching
-l : Displays list of filenames only.
-n : Display the matched lines and their line numbers.
-v : This prints out all the lines that do not matches the pattern
-e exp : Specifies expression with this option. Can use multiple times.
-f file : Takes patterns from file, one per line.
-E : Treats pattern as an extended regular expression (ERE)
-w : Match whole word
-o : Print only the matched parts of a matching line,
with each such part on a separate output line.
-A n : Prints searched line and nlines after the result.
-B n : Prints searched line and n line before the result.
-C n : Prints searched line and n lines after before the result.
Examples,
File
- ls
Lists your files
- ls -l
lists your files in 'long format', which contains lots of useful information, e.g. the exact size of the file, who owns the file and who has the right to look at it, and when it was last modified.
- ls -a
lists all files, including the ones whose filenames begin in a dot, which you do not always want to see.
There are many more options, for example to list files by size, by date, recursively etc.
There are many more options, for example to list files by size, by date, recursively etc.
- touch filename
Create a new file
e.g., $touch grep_example.txt
- rm filename
Delete a file
- rm -r dir (rmdir dir)
Delete a directory
- cp filename1 filename2
Copies a file
- more filename
Shows the first part of a file, just as much as will fit on one screen
Just hit the space bar to see more or q to quit. You can use /pattern to search for a pattern.
More examples https://shapeshed.com/unix-more/
- chmod options filename
Lets you change the read, write, and execute permissions on your files.
The default is that only you can look at them and change them, but you may sometimes want to change these permissions.
For example, chmod o+r filename will make the file readable for everyone,
and chmod o-r filename will make it unreadable for others again.
Note that for someone to be able to actually look at the file the directories it is in need to be at least executable.
More https://www.geeksforgeeks.org/chmod-command-linux/
Other
- export variable=value (now space before and after =)
$export mytestpath = test123
- echo $variable
Shows the value of an environment variable
$echo $mytestpath
>>test123
- clear
cleans screen
- source filename
You need to source your dotfiles after making changes for them to take effect (or log off and in again)
More Unix commands
https://mally.stanford.edu/~sr/computing/more-unix.html
https://mally.stanford.edu/~sr/computing/basic-unix.html
No comments:
Post a Comment