from Linux 101 Hacks - Free eBook --------------------------------------- get the total number of lines that contains the text John in /etc/passwd file # grep -c John /etc/passwd search a text by ignoring the case # grep -i john /etc/passwd search all subdirectories for a text matching a specific pattern # grep -ri john /home/users ---------------------------------------- look for all the files under /etc with mail in the filename # find /etc -name "*mail*" find files that are greater than 100MB # find / -type f -size +100M ---------------------------------------- suppress standard output and error # cat file.tex > /dev/null view only the standard output # ./shell-script.sh 2> /dev/null --------------------------------------- john command combines lines from two files In the example below, we have two files – employee.txt and salary.txt. Both have employee-id as common field. So, we can use join command to combine the data from these two files using employee-id as shown below. $ cat employee.txt 100 Jason Smith 200 John Doe 300 Sanjay Gupta 400 Ashok Sharma $ cat bonus.txt 100 $5,000 200 $500 300 $3,000 400 $1,250 $ join employee.txt bonus.txt 100 Jason Smith $5,000 200 John Doe $500 300 Sanjay Gupta $3,000 400 Ashok Sharma $1,250 ----------------------------------------- xargs is a very powerful command that takes output of a command and pass it as argument of another command. Following are some practical examples on how to use xargs effectively. 1. When you are trying to delete too many files using rm, you may get error message: /bin/rm Argument list too long - Linux. Use xargs to avoid this problem. # find ~ -name "*.log" -print0 | xargs -0 rm -f 2. Get a list of all the *.conf file under /etc/. There are different ways to get the same result. Following example is only to demonstrate the use of xargs. The output of the find command in this example is passed to the ls -l one by one using xargs. # find /etc -name "*.conf" | xargs ls -l 3. If you have a file with list of URLs that you would like to download, you can use xargs as shown below. # cat url-list.txt | xargs wget -c 4. Find out all the jpg images and archive it. # find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz 5. Copy all the images to an external hard-drive. # ls *.jpg | xargs -n1 -i cp {} /external-harddrive/directory