FAQ-Linux Commands -2

The 15 Most Important UNIX commands

  1. man – show manual for a command, example: man ls hit q to exit the man page.
  2. cd – change directory, example: cd /etc/
  3. ls – list directory, similar to dir on windows. example: ls /etc, use ls -l /etc to see more detail
  4. 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
  5. mv – move a file, example: mv source dest
  6. 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
  7. cat – concatenate, or output a file cat /var/log/messages
  8. 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
  9. scp – secure copy, copies a file over SSH to another server. example: scp /local/file user@host.com:/path/to/save/file
  10. 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
  11. 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.
  12. 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
  13. 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.

    Outputs the last 100 lines of the file 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.

    This is a useful example of using tail and grep to selectively monitor a log file in real time.

  14. head – same as tail, but shows the first few lines the file
  15. 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.
  16. To Get Into and Out Of
  17. 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
  18. 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
  19. more:Displays text, one screen at a time.used in case of huge scrolling.
  20. 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.
  21. 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:
    ps -A
    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

  22. Kill a Process: with process id 3139

Comments are closed