Friday, 10 February 2012

10 Frequently Used UNIX / Linux Commands

This short post provides practical examples for ten most frequently used commands in Linux/UNIX. The purpose of this list is to give you a jump-start on some of the common Linux commands.
Tar
#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
Grep
#Search for a given string in a file (case insensitive)
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” *
Find
#Find files using file-name (case insensitive)
find –iname “MyProgram.c”

#Execute commands on the files found by the find command
find –iname “MyProgram.c” –exec md5sum {} \;

#Find all empty files in home directory
find ~ -empty
SSH
#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
Sed
#Convert the DOS file format to Unix file format
sed ‘s/.$//’filename

#Print file content in reverse
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/ /’
Awk
#Remove duplicate lines using awk
awk ‘!($0 in array) { array[$0]; print }’ temp

# Print all lines from /etc/passwd that have the same uid and gid
awk –F ‘:’ ‘$3 == $4’ passwd.txt

# Print only a specific field from a file
awk ‘{print $2, $5}’ employee.txt
Vim
#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 file in read-only mode
vim –R /etc/passwd
Diff
#Ignore white space while comparing
diff –w name_list.txt name_list new.txt
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  
Export

#View oracle related environment variables
export | grep ORACLE

#Export an environment variable
export ORACLE_HOME = ‘/u01/app/oracle/product/10.2.0’

No comments:

Post a Comment