Cheatography
https://cheatography.com
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 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 |
On Unix platform, tar command is the primary archiving utility.
4.ssh command examples
Login to remote host |
ssh -l jsmith remotehost.example.com |
Debug ssh client |
ssh -v -l jsmith remotehost.example.com |
Display ssh client version |
$ ssh -V OpenSSH_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/passwd | more |
Sort passwd file by 3rd field. |
10. export command examples
$ export | grep ORACLE |
To view oracle related environment variables. |
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" |
$ export ORACLE_HOME=/u01/app/oracle/product/10.2.0 |
To export an environment variable: |
14. cd command examples
Use “cd -” to toggle between the last two directories |
Use “shopt -s cdspell” to automatically 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.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 |
|
|
2.grep command examples
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 "ramesh" * |
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' thegeekstuff.txt |
Add line number for all non-empty-lines in a file |
$ sed '/./=' thegeekstuff.txt | sed 'N; s/\n/ /' |
sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file, or input from a pipeline).
8. diff command examples
# diff -w name_list.txt name_list_new.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-sensitve find) |
# find -iname "MyCProgram.c" |
Execute commands on files found by the find command |
$ find -iname "MyCProgram.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 directories 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/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.vim command examples
Go to the 143rd line of file |
$ vim +143 filename.txt |
Go to the first match of the specified |
$ vim +/search-term filename.txt |
Open the file in read only mode. |
$ vim -R /etc/passwd |
11. xargs command examples
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 |
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-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 |
|