Find -exec grep usage


We use “find” command on a daily basis. But, most of the time, it’s just for simple search like
find / -name

There are more useful stuffs that you can do with “find” command if you combine with the regular Linux ‘grep’ command.
This lets you search for text strings and regular expressions in multiple directories in a single shot.

Grep command in it’s simple usage is as follows:

grep ‘apache’ * – will search for the pattern ‘apache’ in all the files in the current directory. But, if you combine the find with grep command, you can do lot more things, like searching for a pattern in multiple directories. For eg:

find . -type f -exec grep -il "^sample$" {} \;

Here “.” means in the current directory and all it’s subdirectories
“-type f” means to search in files
“-exec” lets you execute a command, in this case the “grep” command
“-i” means case insensitive search
“-l” lists the filenames containing the pattern “sample”
“^” Begins with the character following.
“$” Ends with the character preceding.
“{} \;” means that you’re about to feed the grep command a lot of files.

Another eg: find htdocs cgi-bin -name “*.cgi” -type f -exec chmod 755 {} \;

The above command searches through the “htdocs” and “cgi-bin” directories for files that end with the extension “.cgi”. When these files are found, their permission is changed to mode 755 (rwxr-xr-x).

find . -type f \( -name “*.c” -o -name “*.sh” \)

The above command searches for multiple files with extension .c or .sh. Keep adding -o for more specific searches.

find . -mtime -5 -type f

The above command finds all files that has been modified in the last 5 days. You can search for directories by using the flag “-type d”. Omitting “-type f/d” will search for both files and directories modified in last 5 days.

find . -size +100k -a -size -500k

The above command searches files with size between 100 kilobytes and 500 kilobytes

find /home/soj/ -mtime -2 -exec ls {} \;

The above command lists all the files under /home/soj/ that has been modified within last 2 days (note the option -2)

find /home/soj/ -mtime +200 -exec ls {} \;

The above command lists all the files under /home/soj/ that are older than 200 days (note the option +200)

find /media -name ‘*.mp3’ -size -5000k

The above command finds files with extension ‘mp3’ that are less than 5MB (5000 kilobytes) under the directory ‘media’. If you want to search for files greater than 5MB, use -size +5000k in the above command

find . -name “*.txt” -exec -ok cp {} test \;

The above ‘find’ command finds files with ‘txt’ extension by substituting a file name for the brackets, and then asked for confirmation before copying the file to the ‘test’ directory

find ~soj -perm -644

The above command will match all files that have, at a minimum, the rw permission set for user AND r permission for group AND r permission set for others.

find ~soj -perm 644

The above command will match all files that exactly have the rw permission set for user AND r permission for group AND r permission set for others.

find * -mtime +100 exec rm {} \; (Try this command on YOUR OWN RISK)

The above command is DANGEROUS. Here the find command will search for the files that are older than 100 days, as mentioned in the argument for ‘mtime’. This way you can set the time to any number of days and delete files older than the time frame. But, you don’t want to do this as you might NOT KNOW what all it might delete..

What if you want to search for a particular file, say config.cfg, under your current directory and sub directories and then replace a string ‘old’ with another one ‘new’ on all the files with file name config.cfg

find . -type f -name config.cfg -exec sed -i "s/old/new/ig" {} +;
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s