Saturday, September 17, 2011

Find command examples


To exclude  directories while searching using find command
For example i want to prevent the find command from moving into the directories hhh and ggg while searching in my home directory

use this example:

find /home/karthik -name hhh -prune -name ggg -prune -o -type f

This will display all files in  my other directories in my home except moving into my ggg and hhh directories

To grep mutiple pattern in the find command

find /home/karthik -type f  | xargs egrep  "(hello)\|(great)|(awesome)"
In this case i am trying to search multiple word hello,great,awesome in the files searched by the find command.

To serach only in the current directory without moving into the sub-directories use:

find  /home/karthik maxdepth -1 -type f

Here all the files in the current directory is displayed without searching for files in any of the sub-directories
max-depth sets the maximum directory level the search can traverse.

To set the directory levels for searching the files you can use:
maxdepth and mindepth options

mindepth -to set the minimum directory level

maxdepth-to set the maximum directory level

In case if you want to search in sub-directory level 2 to 3
Set it as
set the  mindepth as 2 and maxdepth as 3.

find /home/karthik -mindepth 2 -maxdepth 3 -type f

It will search all the the files  from the second directory level to the third directory level.i.e
for example it searches in /home/karthik/fff/   or /home/karthik/fff/kkkk  and not in /home/karthik


To find only ASCII textfiles in your directory excluding all other binary files  use this:

find /home/karthik -type f |xargs file |grep -i ASCII | cut -d: f1

This uses file command which displays the type of file. And grep only text files from it and by cut command displays the filename from the search.


To find files which belong to a specific user :
Use this

find /home/karthik   -user  divya

In this case the find command searches for all the files which belong to divya in the /home/karthik directory.


To find files which is of specific size:


find /home/karthik  -type f  -size  +100c

Here it will search for all the files above 100 bytes

+100c  here   + - more than
If you want files less than 100 bytes specify it as -100c

If you want files of exact size of 100 bytes keep it as 100c


To find empty files in your directory:
for this use -empty option


find /home/karthik -type f -empty

This will list all the empty files in this directory.

To list all the non-empty files you can simply do:


find /home/karthik -type f   ! -empty

"!" just reverses the meaning of -empty

To find all the files of a provided permission


find /home/karthik -perm 777

It list all the files with the permission 777,
if you provide +444  it lists all the files above this permission.

To search in multiple directories you can use this:


find /home/karthik/ /tmp/ -type f

To search in multiple directory simply put it as said before.This will search in multiple directory /home/karthik/ and /tmp/


To grep pattern in the find list:

find /home/karthik/ -type f | xargs grep "hello"
This "xargs grep hello" command searches for hello across all the files returned by the "find /home/karthik/ -type -f"