Tuesday, August 2, 2011

Useful find commands

 

find . -mtime 0   # find files modified between now and 1 day ago
                  # (i.e., within the past 24 hours)
find . -mtime -1  # find files modified less than 1 day ago
                  # (i.e., within the past 24 hours, as before)
find . -mtime 1   #
# (i.e., within the past 24 hours)
find . -mtime -1  # find files modified less than 1 day ago
                  # (i.e., within the past 24 hours, as before)
find . -mtime 1   # find files modified between 24 and 48 hours ago
find . -mtime +1  # find files modified more than 48 hours ago

find . -mmin +5 -mmin -10 # find files modified between
# 6 and 9 minutes ago
 find files modified between 24 and 48 hours ago
find . -mtime +1  # find files modified more than 48 hours ago
find / -mmin -10  # files modified less than 10 minutes ago
find . -perm -o=w # to find files with certain permissions.

This was taken from http://content.hccfl.edu/pollock/unix/findcmd.htmween
                          # 6 and 9 minutes ago

xargs vs exec in find command


If you analyse the both will do the same functionality
xargs executes the command once and puts the arguments all together, whereas -exec executes the command multiple times, once per each file.

find . -type f | xargs grep "pattern"


find . -type f -exec grep pattern {}


But in the case of exec it will create a separate process for each file returned by the find.So the xargs is said to be more efficient when you are going to do for large
file as it will save time and process memory.

This example may also make you understand better
For example if you want to search a pattern in the first line of the file returned by find command.
For exec it has to be used like this:
find . -type f -perm -700 -exec awk 'NR == 1 && /perl/ {print FILENAME}' {} \;
In exec we can use NR since for each file the awk command will be efxecuted and the NR will point the first line of the file.

For xargs used like this:
find . -type f -perm -700 | xargs awk 'FNR == 1 && /perl/ {print FILENAME}'
In xargs case the find command will be executed first and then for each argument awk will be executed so NR will point to the first line only for the first file and for the second file and so on it will continue to point to the first file.So in this FNR has to be used denoting the first line in each file.

This example was referred from
http://www.linuxquestions.org/questions/showthread.php?s=71bbab884c811443dde9e2f8131bb818&p=4431861#post4431861


word boundary in grep shell

In the case of the perl we use "\b" to specify the word boundary.i.e. in the case of perl
if we want to match a word hello we can use /\bhello\b/ which matches word hello as a separate word or when it occurs as the first word without being combined with someother word.

In Shell if we want to match a word in a word boundary using grep we can use "-w" option of grep which matches the word in a word context.
Using
grep -w "hello" filename 

is same as

grep "\<hello\>" filename

\<  - starts a word boundary.
\>  - ends a word boundary.

grep "\<hello" filename will search for hello,hellobuddy,helloname everyname starting with hello but not Maahello since it starts with a word boundary.