Helpful Commands For Linux Command Line

We bring to you command line examples of useful commands, which will be good enough to give you a kickstart in Linux!

  1. tar

    This is a Tape Archive and is useful for several file formats and for their extraction.

    Create a new tar archive

    tar cvf archive_name.tar dirname/

    Extract from an existing tar archive.

    tar xvf archive_name.tar

    View an existing tar archive.

    tar tvf archive_name.tar
  2. grep

    You use the grep command in order to find lines in a particular file that match a given string or words.

    Search for a given string in a file (case in-sensitive search).

    grep -i "the" demo_file

    Print the matched line, along with the 3 lines after it.

    grep -A 3 -i "example" demo_text

    Search for a given string in all files recursively

    grep -r "atithya" *
  3. find

    Use this command when you need to search for files in a particular directory. It starts from the parent directory and then moves to the sub directories. The -name option makes the search case sensitive, while the -iname option searches irrespective of the case.

    Find files using file-name (case in-sensitve find)

    find -iname "MyCProgram.c"

    Execute commands on files found by the find command

    find -iname "MyCProgram.c" -exec md5sum {} \;

    Delete files modified older than specified time (half a day: 0.5 * 24h)

    find /tmp -mtime +0.5 -delete;

    List files modified newer than specified time (an hour: 60 minutes)

    find /opt/projects -mmin -60;

    Find all empty files in home directory

    find ~ -empty

    Run last executed find command


    Find all the files and directories which hold the 777 permission

    find . -perm 644

    Delete temporary files using find command

    find . -name "*.tmp" -print | xargs rm –f

    Find all text file which contains word Exception

    find . –name "*.txt" –print | xargs grep "Exception"

    Finding files only in the current directory not searching on subdirectories

    find . -maxdepth 1 -type f -newer first_file
    find . -type f -cmin 15 -prune

    Find files based on size

    find . -size +1000c -exec ls -l {} \;
    find . -size +10000c -size -50000c -print

    Find files some days older and above a certain size

    find . -mtime +10 -size +50000c -exec ls -l {} \;

    Find and AWK

    find . -type l -print | xargs ls -ld | awk '{print $10}'
  4. ssh

    Connects you to another computer and lets you use its terminal as if you were sitting right in front of it.

    Login to remote host

    ssh -l jsmith

    Debug ssh client

    ssh -v -l jsmith

    Display ssh client version

    ssh -V
    OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003
  5. sed

    When you copy a DOS file to Unix, you could find \r\n in the end of each line. This example converts the DOS file format to Unix file format using sed command.

    sed 's/.$//' filename

    Print file content in reverse order

    sed -n '1!G;h;$p' thegeekstuff.txt

    Add line number for all non-empty-lines in a file

    sed '/./=' thegeekstuff.txt | sed 'N; s/\n/ /'
  6. awk

    Remove duplicate lines using awk

    awk '!($0 in array) { array[$0]; print }' temp

    Print all lines from /etc/passwd that has the same uid and gid

    awk -F ':' '$3==$4' passwd.txt

    Print only specific field from a file.

    awk '{print $2,$5;}' employee.txt
  7. sort

    Sort a file in ascending order

    sort names.txt

    Sort a file in descending order

    sort -r names.txt

    Sort passwd file by 3rd field.

    sort -t: -k 3n /etc/passwd | more
  8. export

    To view oracle related environment variables.

    export | grep ORACLE
    declare -x ORACLE_BASE="/u01/app/oracle"
    declare -x ORACLE_HOME="/u01/app/oracle/product/10.2.0"
    declare -x ORACLE_SID="med"
    declare -x ORACLE_TERM="xterm"

    To export an environment variable:

    export ORACLE_HOME=/u01/app/oracle/product/10.2.0
  9. xargs

    Copy all images to external hard-drive

    ls *.jpg | xargs -n1 -i cp {} /external-hard-drive/directory

    Search all jpg images in the system and archive it.

    find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz

    Download all the URLs mentioned in the url-list.txt file

    cat url-list.txt | xargs wget -c
  10. ls

    Display filesize in human readable format (e.g. KB, MB etc.,)

    ls -lh
    -rw-r----- 1 atithya team-dev 8.9M Jun 12 15:27 arch-linux.txt.gz

    Order Files Based on Last Modified Time (In Reverse Order) Using ls -ltr

    ls -ltr

    Visual Classification of Files With Special Characters Using ls -F

    ls -F
  11. pwd

    pwd is Print working directory. What else can be said about the good old pwd who has been printing the current directory name for ages.

  12. cd

    Use "cd -" to toggle between the last two directories

    Use "shopt -s cdspell" to automatically correct mistyped directory names on cd

  13. gzip

    To create a *.gz compressed file:

    gzip test.txt

    To uncompress a *.gz file:

    gzip -d test.txt.gz

    Display compression ratio of the compressed file using gzip -l

    gzip -l *.gz
             compressed        uncompressed  ratio uncompressed_name
                  23709               97975  75.8% asp-patch-rpms.txt

    To keep original file while using gzip:

    gzip -c input.file > output.file.gz
  14. bzip2

    To create a *.bz2 compressed file:

    bzip2 test.txt

    To uncompress a *.bz2 file:

    bzip2 -d test.txt.bz2
  15. unzip

    To extract a *.zip compressed file:


    View the contents of *.zip file (Without unzipping it):

    unzip -l
      Length     Date   Time    Name
     --------    ----   ----    ----
        40995  11-30-98 23:50   META-INF/MANIFEST.MF
        32169  08-25-98 21:07   classes_
        15964  08-25-98 21:07   classes_names
        10542  08-25-98 21:07   classes_ncomp
  16. vim

    Go to the file's 143rd line.

    vim +143 filename.txt

    Go to the first found match of the file specified:

    vim +/search-term filename.txt
  17. diff

    Ignoring white spaces when comparing files:

    diff -w name_list.txt name_list_new.txt
  18. man

    The man command is used as the manual pager for the system. It brings online documentation for a particular command.

  19. ps

    This is the process command, which shows you the status of all the processes that are being run by a unique id, known as the PID.

  20. kill

    This command is used in order to kill a process that is not responding or is not being used. All you need is to known the process ID or PID. To find the process id, you need to run ps-A with the grep command (ps-A | grep processname).

  21. whereis

    When you need to locate the binary, sources and the manual page of a command you use the whereis command.

  22. service

    This is the command that is used in order to control the start, stop or restart function of a particular service. You do not have to restart your system in order to start, stop or restart the services.

  23. alias

    This is a built in shell command which is used in order to assign the name for a long command or for a frequently used command.

  24. df

    Use this command when you want to report the disk usage of a file system. It is quite useful for the user and also for the system admin.

  25. rm

    This command is used in order to remove complete files and directories from your system.

  26. lsblk

    This command stands for List blocked devices and print blocked devices.

  27. md5sum

    The md5sum command is short for Compute and Check MD5 Message Digest and it is used in order to verify the integrity of files that may have changed after a faulty file transfer process, malicious interference or disk error.

  28. dd

    The dd command is used to convert and copy a file. It is most often used in order to copy an iso file to an USB device that can be made into a bootable USB disk.

  29. uname

    This command stands for Unix Name and it gives you detailed information on the operating system, kernel and the machine name.

  30. history

    The name of this command is pretty self explanatory. It shows you a long list of all the executed commands from the command line. If you press Ctrl+R and then search for the commands, then it turns on the auto completion feature of Linux.

  31. sudo

    What a lot of people don't know is that sudo stands for super user do. It grants the you superuser privileges.

  32. mkdir

    This command is pretty self explanatory, it stands for 'Make Directory' and that is what it does as well.

  33. touch

    No, it doesn't suddenly turn your computer into a touch screen device! This command is used to update the access and modification times of each file to the current time. This command will create the file only if it doesn't already exist. If it does exist then it will update the timestamp and won't do anything to its contents.

  34. chmod

    The chmod command is short for 'Change File Mode Bits' and it changes the file mode, that is the permission of each given file, script or folder. Three types of permissions exist: read, write and execute. For read, you give a value of 4, while for write and execute you give values of 2 and 1. If you want to give read and write permission then you will add the two and give a value of 6.

  35. chown

    The chown command on Linux is used to change the file owner and group.

  36. apt

    Use the apt command for the Advanced Package Tool. This is an advance package manager for Debian-based systems like Ubuntu, Kubuntu and others.

  37. pipes

    Class of tools that help you send the output of one command to another command.

  38. wget

    Gives you some serious control over your downloads.

  39. ifconfig

    Shows you the IP address of your computer, the MAC address of your Ethernet and Wi-Fi cards etc.

  40. top

    Gives you a list of every program running on your system, as well as how much of your system's resources they're taking up.


Popular posts from this blog