Wednesday, February 29, 2012

Linux frequently used commands

Linux frequently used commands

grep -r "/opt/webhost/logs/tandl/tomcat/tnlBasic.log" /opt/webhost
grep -r "tnlBasic.log" /opt/webhost
grep -r "tandl" /opt/webhost/paytteme/tomcat/webapps/test/WEB-INF/classes/log4j.properties

tar -cvf /var/tmp/ToCopy2.tar ./config ./ghrms ./log
tar -xvf ROOT.tar

scp shekharreddy@shekhar.houston.com:/opt/webhost/tomcat/webapps/servlet_app/WEB-INF/lib/tnl.jar /opt/webhost/tomcat/webapps/jsp_app/WEB-INF/lib

find /opt/webhost/ -name "daily_time_data_msg_ENG.js"
find /home/webhost/ -name "hibernate.cfg.xml"
find / -type d -name "java*"
find /opt/webhost/paytteme/apache/conf/ -type f -name "mod_autoindex.so"
find / -type f -name "mod_autoindex.so"

chmod 777 -R ROOT


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



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" *


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


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



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/ /'
More sed examples: Advanced Sed Substitution Examples

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
More vim examples: How To Record and Play in Vim Editor

8. diff command examples
Ignore white space while comparing.

# diff -w name_list.txt name_list_new.txt

2c2,3
< John Doe --- > John M Doe
> Jason Bourne


9. sort command examples

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

10. export command examples

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

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
More ls examples: Unix LS Command: 15 Practical Examples

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.

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

No comments: