Show Menu
Cheatography

50 Most Frequently Used UNIX / Linux Commands Cheat Sheet (DRAFT) by

This article provides practical examples for 50 most frequently used commands in Linux / UNIX. This is not a comprehensive list by any means, but this should give you a jumpstart on some of the common Linux commands. Bookmark this article for your future reference.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

1.tar command examples

Create a new tar archive.
$ tar cvf archiv­e_n­ame.tar dirname/
Extract from an existing tar archive.
$ tar xvf archiv­e_n­ame.tar
View an existing tar archive.
$ tar tvf archiv­e_n­ame.tar
On Unix platform, tar command is the primary archiving utility.

4.ssh command examples

Login to remote host
ssh -l jsmith remote­hos­t.e­xam­ple.com
Debug ssh client
ssh -v -l jsmith remote­hos­t.e­xam­ple.com
Display ssh client version
$ ssh -V OpenSS­H_3.9p1, OpenSSL 0.9.7a Feb 19 2003
ssh (SSH client) is a program for logging into a remote machine and for
executing commands on a remote machine

9. sort command examples

$ sort names.txt
Sort a file in ascending order
$ sort -r names.txt
Sort a file in descending order
$ sort -t: -k 3n /etc/p­asswd | more
Sort passwd file by 3rd field.

10. export command examples

$ export | grep ORACLE
To view oracle related enviro­nment variables.
declare -x ORACLE­_BA­SE=­"­/u0­1/a­pp/­ora­cle­"
declare -x ORACLE­_HO­ME=­"­/u0­1/a­pp/­ora­cle­/pr­odu­ct/­10.2.0­"
declare -x ORACLE­_SI­D="m­ed"
declare -x ORACLE­_TE­RM=­"­xte­rm"
$ export ORACLE­_HO­ME=­/u0­1/a­pp/­ora­cle­/pr­odu­ct/­10.2.0
To export an enviro­nment variable:

14. cd command examples

Use “cd -” to toggle between the last two direct­ories
Use “shopt -s cdspell” to automa­tically correct mistyped directory names on cd

15. gzip command examples

To create a *.gz compressed file:
$ gzip test.txt
To uncompress a *.gz file:
$ gzip -d test.t­xt.gz
Display compre­ssion ratio of the compressed file using gzip -l
$ gzip -l *.gz
compressed
uncomp­ressed ratio uncomp­res­sed­_name
23709
97975 75.8% asp-pa­tch­-rp­ms.txt
 

2.grep command examples

Search for a given string in a file (case in-sen­sitive search).
$ grep -i "­the­" demo_file
Print the matched line, along with the 3 lines after it.
$ grep -A 3 -i "­exa­mpl­e" demo_text
Search for a given string in all files recurs­ively
$ grep -r "­ram­esh­" *
The grep command is used to search text or searches the given file for lines containing a match to the given strings or words. By default, grep displays the matching lines

5. sed command examples

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' thegee­kst­uff.txt
Add line number for all non-em­pty­-lines in a file
$ sed '/./=' thegee­kst­uff.txt | sed 'N; s/\n/ /'
sed is a stream editor. A stream editor is used to perform basic text transf­orm­ations on an input stream (a file, or input from a pipeline).

8. diff command examples

# diff -w name_l­ist.txt name_l­ist­_ne­w.txt
2c2,3 < John Doe --- > John M Doe > Jason Bourne
Ignore white space while comparing.

13. pwd command

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.
 

3.find command examples

Find files using file-name ( case in-sen­sitve find)
# find -iname "­MyC­Pro­gra­m.c­"
Execute commands on files found by the find command
$ find -iname "­MyC­Pro­gra­m.c­" -exec md5sum {} \;
Find all empty files in home directory
# find ~ -empty
The Linux find command is very powerful. It can search the entire filesystem to find files and direct­ories according to the search criteria you specify

6.awk command examples

Remove duplicate lines using awk
$ awk '!($0 in array) { array[$0]; print }' temp
Print all lines from /etc/p­asswd that has the same uid and gid
$awk -F ':' '$3==$4' passwd.txt
Print only specific field from a file.
$ awk '{print $2,$5;}' employ­ee.txt

7.vim command examples

Go to the 143rd line of file
$ vim +143 filena­me.txt
Go to the first match of the specified
$ vim +/sear­ch-term filena­me.txt
Open the file in read only mode.
$ vim -R /etc/p­asswd

11. xargs command examples

Copy all images to external hard-drive
# ls *.jpg | xargs -n1 -i cp {} /exter­nal­-ha­rd-­dri­ve/­dir­ectory
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-li­st.txt file
# cat url-li­st.txt | xargs wget –c

12. ls command examples

Display filesize in human readable format (e.g. KB, MB etc.,)
$ ls -lh -rw-r----- 1 ramesh team-dev 8.9M Jun 12 15:27 arch-l­inu­x.t­xt.gz
Order Files Based on Last Modified Time (In Reverse Order) Using ls -ltr
$ ls -ltr
Visual Classi­fic­ation of Files With Special Characters Using ls -F
$ ls -F