50 Most Frequently Used UNIX / Linux Commands

  1. Create a new tar archive
    $ tar cvf archive_name.tar dirname/
  2. Extract an existing tar archive
    $ tar xvf archive_name.tar
  3. View an existing tar archive
    $ tar tvf archive_name.tar
  4. Search for a given string in a file (case in-sensitive search).
    $ grep -i "the" demo_file
  5. Print the matched line, along with the 3 lines after it.
    $ grep -A 3 -i "example" demo_text
  6. Search for a given string in all files recursively (pertaining to)
    $ grep -r "ramesh" *
  7. Find files using file-name ( case in-sensitve find)
    # find -iname "MyCProgram.c"
  8. Execute commands on files found by the find command
    $ find -iname "MyCProgram.c" -exec md5sum {} \;
  9. Find all empty files in home directory
    # find ~ -empty
  10. Login to remote host
    ssh -l jsmith remotehost.example.com
  11. Debug ssh client
    ssh -v -l jsmith remotehost.example.com
  12. Display ssh client version
    $ ssh -V
  13. Print only specific field from a file
    $ awk '{print $2,$5;}' employee.txt
  14. Go to the 143rd line of file
    $ vim +143 filename.txt
  15. Go to the first match of the specified
    $ vim +/search-term filename.txt
  16. Open the file in read only mode
    $ vim -R /etc/passwd
  17. Ignore white space while comparing
    # diff -w name_list.txt name_list_new.txt
  18. Sort a file in ascending order
    $ sort names.txt
  19. Sort a file in descending order
    $ sort -r names.txt
  20. Sort passwd file by 3rd field
    $ sort -t: -k 3n /etc/passwd | more
  21. To export an environment variable:
    $ export ORACLE_HOME=/u01/app/oracle/product/10.2.0
  22. Copy all images to external hard-drive
    # ls *.jpg | xargs -n1 -i cp {} /external-hard-drive/directory
  23. Search all jpg images in the system and archive it.
    # find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz
  24. Download all the URLs mentioned in the url-list.txt file
    # cat url-list.txt | xargs wget –c
  25. Display filesize in human readable format (e.g. KB, MB etc.,)
    $ ls -lh
  26. Order Files Based on Last Modified Time (In Reverse Order) Using ls -ltr
    $ ls -ltr
  27. Visual Classification of Files With Special Characters Using ls -F
    $ ls -F
  28. To create a *.gz compressed file:
    $ gzip test.txt
  29. To uncompress a *.gz file:
    $ gzip -d test.txt.gz
  30. Display compression ratio of the compressed file using gzip -l
    $ gzip -l *.gz
  31. To create a *.bz2 compressed file:
    $ bzip2 test.txt
  32. To uncompress a *.bz2 file:
    bzip2 -d test.txt.bz2
  33. To extract a *.zip compressed file:
    $ unzip test.zip
  34. View the contents of *.zip file (Without unzipping it):
    $ unzip -l jasper.zip
  35. Shutdown the system and turn the power off immediately
    # shutdown -h now
  36. Shutdown the system and turn the power off immediately
    # shutdown -h now
  37. Shutdown the system after 10 minutes
    # shutdown -h +10
  38. Reboot the system using shutdown command
    # shutdown -r now
  39. Force the filesystem check during reboot.
    # shutdown -Fr now
  40. View crontab entry for a specific user
    # crontab -u john -l
  41. Check the status of all the services
    service --status-all
  42. Restart a service
    # service ssh restart
  43. To view current running processes
    $ ps -ef | more
  44. To view current running processes in a tree structure. H option stands for process hierarchy.
    $ ps -efH | more
  45. This command is used to display the free, used, swap memory available in the system.Typical free command output. The output is displayed in bytes.
    $ free
  46. check how many GB of RAM your system has use the -g option. -b option displays in bytes, -k in kilo bytes, -m in mega bytes
    $ free -g
  47. If you want to see a total memory ( including the swap), use the -t switch, which will display a total line as shown below.
    $ free -t
  48. Use kill command to terminate a process. First get the process id using ps -ef command, then use kill -9 to kill the running Linux process
    • $ ps -ef | grep vim
    • ramesh 7243 7222 9 22:43 pts/2 00:00:00 vim

    $ kill -9 7243
  49. Get confirmation before removing the file.
    $ rm -i filename.txt
  50. Following example recursively removes all files and directories under the example directory. This also removes the example directory itself.
    $ rm -r example
  51. Copy file1 to file2 preserving the mode, ownership and timestamp.
    $ cp -p file1 file2
  52. Copy file1 to file2. if file2 exists prompt for confirmation before overwritting it.
    cp -i file1 file2
  53. Rename file1 to file2. if file2 exists prompt for confirmation before overwritting it.
    $ mv -i file1 file2
  54. You can view multiple files at the same time.
    $ cat file1 file2
  55. To mount a file system, you should first create a directory and mount it as shown
    # mkdir /u01

    # mount /dev/sdb1 /u01
  56. Use ifconfig command to view or configure a network interface on the Linux system.
    $ ifconfig -a
  57. shows all files in the system that contains the word crontab in it.
    $ locate crontab
  58. Print the last 10 lines of a file by default.
    $ tail filename.txt
  59. Print N number of lines from the file named filename.txt
    $ tail -n N filename.txt
  60. View the content of the file in real time using tail -f. This is useful to view the log files, that keeps growing. The command can be terminated using CTRL-C
    $ tail -f log-file
  61. less is very efficient while viewing huge log files, as it doesn’t need to load the full file while opening.
    $ less huge-log-file.log
  62. Ping a remote host by sending only 5 packets.
    $ ping -c 5 gmail.com
Author
larry
ID
325470
Card Set
50 Most Frequently Used UNIX / Linux Commands
Description
examples for 50 most frequently used commands in Linux / UNIX
Updated