Wednesday, December 28, 2011

find commands in Linux


Find  a Word for a particular word in files present in multiple subdirectories
find . -type f -exec grep "dvc88601" {} \; -print

find ./ -type f -exec grep -i -H 'aaa' {} \;

if there is a escape character like “-” then use “\” for example for finding “–aaa”
find ./* -exec grep -i -H '\-aaa' {} \;

finding the occurrence in a particular format of a file: for example here we are finding it only in “.txt” files
find ./ -iname \*.txt\* -exec grep -i -H 'bbb' {} \;

Find and Replace:
this is meeting the requirement for me :
$ perl -e "s/pepsi/coke/g;" -pi $(find /home/oracle/vishal/test -type f)
syntax:
$ perl -e "s/FIND/REPLACE/g;" -pi $(find path/to/DIRECTORY -type f)

Ex:
findreplaceExurl.sh
perl -e "s/Vivek/vishal/g;" -pi $(find /home/oracle/vishal/test -type f)
perl -e "s/Pratheep/vishal/g;" -pi $(find /home/oracle/vishal/test -type f)
perl -e "s/Prateek/vishal/g;" -pi $(find /home/oracle/vishal/test -type f)
perl -e "s/Tom/vishal/g;" -pi $(find /home/oracle/vishal/test -type f)
perl -e "s/Jimmy/vishal/g;" -pi $(find /home/oracle/vishal/test -type f)
perl -e "s/Ramanujam/vishal/g;" -pi $(find /home/oracle/vishal/test -type f)
perl -e "s/Naveen/vishal/g;" -pi $(find /home/oracle/vishal/test -type f)
perl -e "s/Jophy/vishal/g;" -pi $(find /home/oracle/vishal/test -type f)
perl -e "s/Krishna/vishal/g;" -pi $(find /home/oracle/vishal/test -type f)


Find and Replace:
find ./ -type f -exec sed -i ’s/string1/string2/’ {} \;

Find and Delete:
find ./* -type f -exec sed -i 's/index.html//g' {} \;

Find a file
========
The Linux 'find' command will list files and directories that match the arguments to the command and pass specified tests. For example, to find all 'txt' files in the home directory, the following command can be used:

find ~ -name *.txt

The '~' means start at the user's home directory and the -name is a test which means list only files with that particular name. Find has many tests but some of the most useful for finding files that have been updated recently are the -mtime and -mmin tests.


Find All Files Modified in the Last 90 Minutes
==================================

find . -mmin -90

It looks as if the 'find' command has listed files and directories. If we only want to see the files, add the -type test to the command to specify files only.

find . -type f -mmin -90

The sample output would then become

       
                /home/linux/dns/mysql-outfile/dnsx.txt
                /home/linux/dns/mysql-outfile/dnsx.doc
       
      
Adding Xargs to Find
=============================
Xargs' power lies in the fact that it can take the output of one command, in this case find, and use that output as arguments to another command. So, using the basic find command above, let us pass the output of find to xargs and get xargs to issue multiple 'ls -l' commands.

find ~ -type f -mmin -90 | xargs ls -l

The sample output would then become

       
                -rw-rw-r--    1 linux    lunux    10209032 Jun 30 13:28 /home/linux/dns/mysql-outfile/dnsx.doc
                -rw-rw-rw-    1 mysql    mysql    10209032 Jun 30 12:53 /home/linux/dns/mysql-outfile/dnsx.txt
       
      
Find All Files Modified More Than 5 Years Ago
=====================================
In this example of the find and xargs command, we will use a positive time and the mtime test. The operand value to mtime is the number of 24 hour periods. It is not the number of days and the reason why will become apparent if you read 'man find'. So, to see files over 5 years old will will use +1825 as the value for mtime. We have ignored leap years in calculating 5 years as 5*365 days. The command issued is

find ~ -type f -mtime +1825 |xargs ls -l

Tell xargs when to Quit
==================
One problem with the above approach is that a complete listing of the current directory will occur if the find command does not find any files! Xargs sees a list of zero files, but still issues the 'ls -l' command. To overcome this, simply use the xargs option '-r' which means "If the standard input does not contain any nonblanks, do not run the command." So, our command now becomes:

find ~ -type f -mtime +1825 |xargs -r ls -l

Note that the '-r' is an xargs option and therefore comes after the xargs command name but before the command to be issued - in this case 'ls -l'.

No comments:

Post a Comment