Hello friends, I have written a mini book on Cyber Security awareness for you, named "Hacking into Hackers' Head". This book uses common terms to explain things, and I'm sure you will get benefitted from it in your day to day life. It's available on Kindle and Google Play. Grab your copy now.

Tuesday, January 13, 2015

Most useful and frequently used LINUX/UNIX commands with examples

Hi Geeks, This article will provide the most useful ad frequently used LINUX / UNIX commands along-with the example.

If I miss any command then don't forget to write them in comment.



  1. grep command: Searches for given string in files
    1. grep -i "search_text" file_name
    2. grep -ilrn "search_text" *             (To search in all files)
      Attributes: i for ignore case, l for list names of files, r for searching recursively, n for line number where text is matched in the file
  2. find command: Finds files using file-name
    1. find -iname "MyTextFile.c"
    2. find -iname "MyTextFile.c" -exec md5sum {} \;    :Executes commands on files found by the find command
    3. find ~ -empty                     :Finds all  empty files in home directory
  3. pwd command: pwd is Present working directory. It prints the current directory.
  4. cd command: It is used to change the directory.
    1. Use “cd -” to toggle between the last two directories
    2. Use “shopt -s cdspell” to automatically correct mistype directory names on cd
  5. diff command: compares two files
    1. diff -w file1.txt file2.txt
  6. tar command examples:
    1. tar cvf archive_name.tar dirname/     :Creates a new tar archive
    2. tar xvf archive_name.tar   : Extracts from existing tar archive
    3. tar tvf archive_name.tar   : view an existing tar archive
  7. gzip command examples
    1. gzip file.txt : creates a *.gz compressed file
    2. gzip -d file.txt.gz  : Uncompress a *.gz file
    3. gzip -l *.gz  : Displays the compression ratio of the compressed file
  8. bzip2 command examples
    1. bzip2 file.txt  : creates a *.bz2 compressed file
    2. bzip2 -d file.txt.bz2  : uncompresses a *.bz2 file
  9. unzip command examples
    1. unzip test.zip   : Extracts the zipped file
    2. unzip -l test.zip  : views the content of zipped file without unzipping
  10. ssh commands: It is used to work remotely
    1. ssh -l kamal remotehost.com     : Login to remote gost
      ssh -l kamal 10.201.42.12
    2. ssh -v -l kamal remotehost.com   : Debug ssh client
    3. ssh -V : Display ssh client version
  11. ftp command examples
    1. ftp IP/hostname    : connects to a remote server
    2. ftp> mget *.html  : Download file from that server
    3. ftp> mls *.html -   : views the file names located on the remote server
  12. wget command :
    1. wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.1.tar.gz : to download any file from internet
    2. wget -O taglist.zip http://www.vim.org/scripts/download_script.php?src_id=7701  :  Download and store it
  13. vim command examples
    1. vim +14 file.txt   : Go to the 14th line of file
    2. vim +/search_text file.txt  : goto the first match of the specified search text
    3. vim -R /etc/passwd  : Opent he file in the read only mode
  14. sort command examples
    1. sort nameList.txt :  Sorts a file in ascending order
    2. sort -r nameList.txt :  Sorts a file in descending order
    3. sort -t: -k 3n /etc/passwd | more :  Sorts passwd file by third field
  15. xargs command examples
    1. ls *.jpg | xargs -n1 -i cp {} /external-drive/directory    : Copy all images to external drive
    2. find / -name *.jpg -type f -print | xargs tar -cvzf allImages.tar.gz    : Search all jpg images in the system and archive it.
    3. cat url-list.txt | xargs wget –c  :  Downloads all the URLs mentioned in the url-list.txt file
  16. ls command examples
    1. ls -lh :  Displays filesize in KB / MB
    2. ls -ltr  :  Orders Files Based on Last Modified Time
    3. ls -F   : Visual Classification of Files
  17. sed command examples
    1. sed 's/.$//' filename  :Converts the DOS file format to Unix file format (removes \r or \n)
    2. sed -n '1!G;h;$p' file.txt   : Prints file content in reverse order
    3. sed '/./=' file.txt | sed 'N; s/\n/ /'  :  Adds line number for all non-empty-lines in the file
  18. awk command examples
    1. awk '!($0 in array) { array[$0]; print }' temp   : Removes duplicate lines
    2. awk -F ':' '$3==$4' passwd.txt   ;  Prints all lines from /etc/passwd that has the same uid and gid
    3. awk '{print $2,$5;}' file.txt   : Prints only specific field from a file.
  19. shutdown command examples
    1. shutdown -h now   : Shutdown the system and turn the power off immediately.
    2. shutdown -h +10   : Shutdown the system after 10 minutes.
    3. shutdown -r now    : Reboot the system
    4. shutdown -Fr now  : Force the filesystem check during reboot.
  20. crontab command examples
    1. crontab -u kamal -l  : View crontab entry for a specific user
  21. service command examples : Service commands are used to run the system V init scripts. i.e Instead of calling the scripts located in the /etc/init.d/ directory with their full path, we can use the service command.
    1. service ssh status  : checks the service status
    2. service --status-all : Check the status of all the services.
    3. service ssh restart :  Restart a service
  22. ps command :  It is used to display information about the processes running in the system.
    1. ps -ef | more  : view the current running processes
    2. ps -efH | more  :  To view current running processes in a tree structure. H means process hierarchy.
  23. free command: It is used to display the free, used, swap memory available in the system.
    1. free
    2. free -g :  If you want to quickly 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.
    3. free -t  : use this if you want to see a total memory ( including the swap)
  24. top command: It displays the top processes in the system, by default sorted by cpu usage. To sort top output by any column, press O (upper-case O) , which will display all the possible columns that you can sort by.
    1. top
    2. top -u oracle : To display only the processes that belong to a particular user use -u option. This command will show only the top processes that belongs to oracle user.
  25. df command: 
    1. df -k  : displays the file system disk space usage. By default df -k displays output in bytes.
    2. df -h :  displays output in human readable form. i.e size will be displayed in GB’s.
    3. df -T : display type of file system.
  26. kill command: It is used to terminate a process. First get the process id using ps -ef command, then use kill -9 to kill the running LINUX process. You can also use killall, pkill, xkill to terminate a unix process.
    1. ps -ef | grep vim
      kill -9 7243
  27. rm command : Removes a file
    1. rm -i filename.txt  :  Get confirmation before removing the file.
    2. rm -i file*  :  Print the filename and get confirmation before removing the file.
    3. rm -r example  : It recursively removes all files and directories under the example directory. This also removes the example directory itself.
  28. cp command : Used for copying files from source to destination
    1. cp -p file1 file2 : Copy file1 to file2 preserving the mode, ownership and timestamp.
    2. cp -i file1 file2 : Copy file1 to file2. if file2 exists prompt for confirmation before overwritting it.
  29. mv command: used to rename a file / folder
    1. mv -i file1 file2 :  Rename file1 to file2. if file2 exists prompt for confirmation before overwritting it.
    2. mv -f file1 file2 : Rename file1 to file2. if file2 exists overwrite it without prompting for confirmation
    3. mv -v file1 file2 : It will print what is happening during file rename, verbose output
  30. cat command  : used to view the file
    1. cat file1
    2. cat file1 file2  : view multiple files at the same time. It prints the content of file1 followed by file2 to stdout.
    3. cat -n /etc/test.txt  :  It will prepend the line number to each line of the output while displaying the file.
  31. mount command
    1. To mount a file system, we should first create a directory and mount it:
      1. mkdir /newDir
      2. mount /dev/sdb1 /newDir
    2. We can also add this to the fstab for automatic mounting. i.e Anytime system is restarted, the filesystem will be mounted.
      1. /dev/sdb1 /newDir ext2 defaults 0 2
  32. chmod command: chmod command is used to change the permissions for a file or directory.
    1. chmod ug+rwx file.txt   ; Give full access (read, write and execute) to user and group on a specific file.
    2. chmod g-rwx file.txt     : Revoke all access (read, write and execute) for the group on a specific file.
    3. chmod -R ug+rwx file.txt   :   Apply the file permissions to all the files in the sub-directories.
  33. chown command: change the owner and group of a file
    1. chown oracle:dba dbora.sh  :  To change owner to oracle and group to db on a file. i.e Change both owner and group at the same time.
    2. chown -R oracle:dba /home/oracle   : change the owner recursively
  34. passwd command : used to change the password through command line
    1. passwd : User can change their password using this command. It will ask for current password.
    2. passwd USERNAME : Super user can use passwd command to reset others password. This will not prompt for current password of the user.
    3. passwd -d USERNAME  :  Remove password for a specific user. Root user can disable password for a specific user. Once the password is disabled, the user can login without entering the password.
  35. uname command: It displays important information about the system such as — Kernel name, Host name, Kernel release number, Processor type, etc.
    1. uname -a
  36. su command
    1. su - USERNAME  : Switch to a different user account using su command. Super user can switch to any other user without entering their password.
    2. su - raj -c 'ls' :  Execute a single command from a different account name. In the following example, john can execute the ls command as raj username. Once the command is executed, it will come back to john’s account.
    3. su -s 'SHELLNAME' USERNAME :  Login to a specified user account, and execute the specified shell instead of the default shell.
  37. mkdir command: to create directories
    1. mkdir ~/myDir  : creates a directory called myDir under home directory.
    2. mkdir -p dir1/dir2/dir3/dir4/   :  It creates nested directories using. If any of these directories exist already, it will not display any error. If any of these directories doesn’t exist, it will create them.
  38. ifconfig command: used to view or configure a network interface on the Linux system, same as ipconfig in windows.
    1. ifconfig -a  :  View all the interfaces along with status.
    2. Start or stop a specific interface using up and down command as below.
      1. ifconfig eth0 up
      2. ifconfig eth0 down
  39. ping command :
    1. ping google.com : Ping a remote host.
    2. ping -c 5 google.com  :  Ping a remote host by sending only 5 packets.
  40. whereis command
    1. whereis ls    :  When wewant to find out where a specific Unix command exists (for example, where does ls command exists?), we can execute this command.
    2. whereis -u -B /tmp -f lsmk   :   When you want to search an executable from a path other than the whereis default path, you can use -B option and give path as argument to it. This searches for the executable lsmk in the /tmp directory, and displays it, if it is available.
  41. whatis command : It displays a single line description about a command.
    1. whatis ls
    2. whatis ifconfig
  42. man command : Displays the man page (manual page) of a specific command
    1. man crontab
  43. locate command:  Using locate command we can quickly search for the location of a specific file (or group of files). Locate command uses the database created by updatedb.
    1. locate crontab
  44. tail command
    1. tail file.txt    :   Print the last 10 lines of a file by default.
    2. tail -n N file.txt   :   Print N number of lines from the file named file.txt
    3. tail -f log-file  :  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
  45. less command: less is very efficient while viewing huge log files, as it doesn't need to load the full file while opening. After opening file through less, following command is very useful:
    1. CTRL+F – forward one window
    2. CTRL+B – backward one window
  46. fg command : This command resumes execution of a suspended process
  47. date command : 
    1. date -s "01/13/2015 13:55:14"   :  Set the system date
    2. Once you’ve changed the system date, you should syncronize the hardware clock with the system date as shown below.
      1. hwclock –systohc
      2. hwclock --systohc –utc
  48. mysql command
    1. mysql -u root -p -h 192.160.10.2  : To connect to a remote mysql database. This will prompt for a password.
    2. mysql -u root -p  :  To connect to a local mysql database. If you want to specify the mysql root password in the command line itself, enter it immediately after -p (without any space).
  49. yum command
    1. yum install httpd : To install apache using yum.
    2. yum update httpd  :  To upgrade apache using yum.
    3. yum remove httpd :  To uninstall/remove apache using yum.
  50. rpm command
    1. rpm -ivh httpd-2.2.3-22.0.1.el5.i386.rpm   :  To install apache using rpm.
    2. rpm -uvh httpd-2.2.3-22.0.1.el5.i386.rpm  :  To upgrade apache using rpm.
    3. rpm -ev httpd  :  To uninstall/remove apache using rpm.

Collected from different internet sources and re-organized.

    No comments:

    Post a Comment

    We appreciate your valuable comments.

    Scroll to Top