The 15 Most Important UNIX commands
- man – show manual for a command, example: man ls hit q to exit the man page.
- cd – change directory, example: cd /etc/
- ls – list directory, similar to dir on windows. example: ls /etc, use ls -l /etc to see more detail
- cp – copy a file or directory, example: cp source dest if you want to copy a directory use the -R option for recursive: cp -R /source /dest
- mv – move a file, example: mv source dest
- rm – remove a file, example: rm somefile to remove a directory you may need the -R option, you can also use the -f option which tells it not to confirm each file: rm -Rf /dir
- cat – concatenate, or output a file cat /var/log/messages
- more – outputs one page of a file and pauses. example: more /var/log/messages press q to exit before getting to the bottom. You can also pipe to more | more from other commands, for example ls -l /etc | more
- scp – secure copy, copies a file over SSH to another server. example: scp /local/file firstname.lastname@example.org:/path/to/save/file
- tar – tape archiver, tar takes a bunch of files, and munges them into one .tar file, the files are often compressed with the gzip algorithm, and use the .tar.gz extension. to create a tar tar -cf archive.tar /directory, then to extract the archive to the current directory run tar -xf archive.tar to use gzip, just add a z to the options, to create a tar.gz: tar -czf archive.tar.gz /dir to extract it tar -xzf archive.tar.gz
- grep – pattern matcher, grep takes a regular expression, or to match a simple string you can use fast grep, fgrep failure /var/log/messages, I’m usually just looking for a simple pattern so I tend to use fgrep more than regular grep.
- find – lists files and directories recursively on a single line, I usually pipe grep into the mix when I use find, eg: find / | fgrep log
- tail – prints the last 10 lines , this is handy for checking log files tail /var/log/messages if you need see more lines, use the -n option, tail -n 50 /var/log/messages you can also use the -f option, which will continuously show you the end of the file as things are added to it (very handy for watching logs) tail -f /var/log/messages.Example
Outputs the last 10 lines of the file myfile.txt.1tail myfile.txt -n 100
Outputs the last 100 lines of the file myfile.txt.1tail -f myfile.txt
Outputs the last 10 lines of myfile.txt, and monitors myfile.txt for updates; tail then continues to output any new lines that are added to myfile.txt.1tail -f access.log | grep 184.108.40.206
This is a useful example of using tail and grep to selectively monitor a log file in real time.
- head – same as tail, but shows the first few lines the file
- vi – text editor, there are several text editors such as emacs, and nano, but vi is usually installed on any server so its a good one to learn. To edit a file type vi file to edit a line press Esc i then to save changes and exit use Esc wq, or to quit without saving use Esc q!. There are a million other commands, but that will enable you to edit files at a basic level.
- To Get Into and Out Of
- viTo Start viTo use vi on a file, type in vi filename. If the file named filename exists, then the first page (or screen) of the file will be displayed; if the file does not exist, then an empty file and screen are created into which you may enter text.
vi filename edit filename starting at line 1
vi -r filename recover filename that was being edited when system crashed
- To Exit vi Usually the new or modified file is saved when you leave vi. However, it is also possible to quit vi without saving the file.
Note: The cursor moves to bottom of screen whenever a colon (:) is typed. This type of command is completed by hitting the <Return> (or <Enter>) key.
:x<Return> quit vi, writing out modified file to file named in original invocation
:wq<Return> quit vi, writing out modified file to file named in original invocation
:q<Return> quit (or exit) vi
:q!<Return> quit vi even though latest changes have not been saved for this vi call
- more:Displays text, one screen at a time.used in case of huge scrolling.
- less:To open a file using less, type the less command followed by the file name in the UNIX or Linux prompt:less filenameLess will open the file and display the file name at the lower left portion of the terminal. To find a string in the file type forward slash followed by the string that you want to search then hit Enter.You will see that the view of the file will jump to the line where the string is located. The located string matches are highlighted for easier viewing.To search for the next instance of the string just press the “n” key. Pressing the uppercase “N” will bring you to the previous match.
- ps:The ps command lists running processes. The following command lists all processes running on your system:ps -A
The ps command lists running processes. The following command lists all processes running on your system:
This may be too many processes to read at one time, so you can pipe the output through theless command to scroll through them at your own pace:
ps -A | less
Press q to exit when you’re done.
You could also pipe the output through grep to search for a specific process without using any other commands. The following command would search for the Firefox process:
ps -A | grep firefox
- Kill a Process: with process id 3139
1kill -9 3139