Jul 03 2007
Becoming an expert with the Find Command
Command your box from command-line-interface. This is the power of unix/linux & this is the power of find.
——————————————————————————–
Find command is just like driving a car to the destination. If you can read & follow the instructions provided, you are destined to reach at proper address.
Location director boardings at signals & landmarks are the inputs/options provided by the users/yourself & finally the roads are the hierarchy/paths to follow to reach the desired destination.
For example : I had to reach rahul’s home but i dont remember the exact address, but i had some clues like, it was on 94th street & it was a red colour building, further on there were two trees situated infront of his house. That was enough for me & indeed i reached his home.
But if you really find searching & locating files a bit tedious job for yourself, then you hould learn & get used to of FIND from scratch.
——————————————————————————–
Description of find command according to man page:
Find searches the directory tree rooted at each given file name by evaluating the given expression from left tto right, according the rules of precendence, until the outcome is known (the left hand side is false for and operations, true for or), at which point find moves on to the next file name.
The first arguement that begins with ‘-’. ‘(’. ‘)’, ‘,’ or ‘!’ is taken to be the beginning of the expression;
any arguements before it are paths to search, and any arguements after it are the rest of the expression.
If no paths are given, the current directory is used. If no expression is given, the experssion ‘-print’ is used.
Also make a note that find exits with status 0 if all the files are processed successfully, greater than 0 if error occurs.
——————————————————————————–
Basic Find command layout
FIND ..PATH(s)-to-search.. ..expressions..
……………………………………./\
…………———————————————
…………|………………………….|………………………..|
………options…..operators….tests…..operators….actions
Now we’ll start from the top in hierarchy & discuss each of them in detail later on.
List of OPTIONS available:
-daystart
-depth
-follow
-help
-maxdepth levels
-mindepth levels
-mount
-noleaf
-version
-xdev
List of TESTS available:
+n
-n
n
-amin n
-anewer file
-atime n
-cmin n
-cnewer file
-ctime n
-empty
-false
-fstype type
-gid n
-group
-ilname pattern
-iname pattern
-inum n
-ipath pattern
-iregex pattern
-links n
-lname pattern
-mmin n
-mtime n
-name pattern
-newer file
-nouser
-nogroup
-path pattern
-perm mode
-regex pattern
-size n[cwbkMG]
-true
-type [bcdpflsD]
-uid n
-used n
-user uname
-xtype c
-context scontext
List of ACTIONS available:
-exec command
-fls file
-fprint file
-fprint0 file
-fprintf file-format
-ok command
-print true
-print0
-printf format
-prune
-ls
List of OPERATORS available:
(expr)
!expr
-not expr
expr1 expr2
expr1 -a expr2
expr1 -and expr2
expr1 -o expr2
expr1 -or expr2
expr1 , expr2
* you can refer man page for detailed info regarding any of the options, actions, tests & operators mentioned above.
Examples
1. Find file with file name ‘foo’
find /-name foo
find /-name fo*
-name
——————————————————————————–
2. Find file with file name ‘FoO’
find / -iname foo
-iname
Same way -lname & -ilname variable works.
-lname
-ilname
——————————————————————————–
3. Finding all ‘conf’ files at / or at mentioned path.
find / -name *.conf
——————————————————————————–
4. Backing-up/copying all the conf files found in last example over to a seperate folder.
find / -name *.conf exec cp { } /
-exec
” \;” needs to be there as it tells the end of arguements provided to -exec variable.
——————————————————————————–
5. Find all the files owned by a user.
find / -user
find / -uid
-user
——————————————————————————–
6. Find all the files owned by a group.
find / -group
find / -gid
-group / -gid
——————————————————————————–
7. Find all the files which doesnt belongs to any user or group.
find / -nouser
find / -nogroup
——————————————————————————–
8. Finding files with exact permission flags/bits.
find / -perm 777
-perm
——————————————————————————–
9. Finding files with wildcard match to the permission flags/bits.
find / -perm -700
find / -perm -007
-700 is equivalent to : rwx******
-007 is equivalent to : ******rwx
Some more examples :
-006 is equivalent to : ******rw-
-604 is equivalent to : rw-***r–
——————————————————————————–
10. Finding files with nos of links pointing to them.
find / -links
-links
——————————————————————————–
11. Finding files from the file size.
a. Finding files under 5 KB.
find / -size -5000c
find / -size -5k
——————————————————————————–
b. Finding files under 5 GB.
find / -size -5000000000c
find / -size -5000000k
find / -size -5000M
find / -size -5G
——————————————————————————–
c. Finding files between two file sizes, like finding files which are more than 1 MB but smaller than 10 MB.
find / -size +1M -and -10M
——————————————————————————–
d. Finding only folders name using -size ‘test’ variable.
Tip : All the folders directories are comprises of exact 4K(4076 Bytes)size only.
find / -size 4k
The above mentioned command is equivalent to dir /ad is MSDOS environment.
And also equivalent to find / -type d unix command.. explained in next example.
——————————————————————————–
12. Finding files from its type.
find -type [bcdpflsD]
b - block (buffered) special.
c - character (unbuffered) special.
d - directory.
p - named pipe.
f - regular file.
l - symbolic link.
s - socket.
D - door (solaris).
——————————————————————————–
13. Finding files whose status was changed in last 2 days.
find / -ctime -2
-ctime
n : exact n*24 hours ago.
-n : under n*24 hours.
+n : over n*24 hours.
Also :
find / -cmin
-cmin
——————————————————————————–
14. Find files those were accessed in last 10 minutes.
find / -amin -10
-amin
n : exact n minutes ago.
-n : under n minutes.
+n : over n minutes.
Also :
find / -atime
-atime
——————————————————————————–
15. Find files those were not modified in last 3 days.
find / -mtime +3
-mtime
n : exact n*24 hours ago.
-n : under n* 24 hours.
+n ; over n*24 hours.
Also :
find / -mmin
-mmin
——————————————————————————–
16. Finding files which were either modified or last accessed or file status was last changed more recently than specified file.
First of all we’ll create a new blank file with a time stamp to suit our needs.
touch temp -t 200511200001
Here we have created a new file ‘temp’ & assigned a timestamp of 20th november 2005 00:00 hrs.
Now we’ll with refrence to this file will find files either modified or last accessed or file status last changed more recently than this file.
find / -anewer temp
find / -cnewer temp
find / -newer temp
-anewer
-cnewer
-newer
Also to mentioned specified file in
——————————————————————————–
17. Finding files only on known filesystem types of unix & skipping other filesystems like fat & others.
find / -mount
find / -xdev
-mount : Don’t descend directories on other filesystems. An alternate name for -xdev, for compatibility with some other versions of find.
——————————————————————————–
18. Finding empty files.
find / -empty
-empty : List all the files which are empty & is either a regular file or a directory.
Tip: could be usefull for cleaning some temp & empty files is pre-defined/user folders.
——————————————————————————–
19. Finding all the movie *.avi files but excluding the /usr folder for the search.
find / -path /usr -prune -or -name *.avi
-prune : If depth is not given by -depth action variable, it does not descent into specified or current directory. If depth is given, it has no effect.
-or : logical ‘or’ operator.
——————————————————————————–
20. Finding all the files of your home directory.
find $HOME
——————————————————————————–
Now we move to some complicated queries.
21. Finding all the word files in a common shared folder (/shared) which donot have rw- access to others & then changing the permission bits to 666.
find /shared -name *.doc -and ! -perm -006 -exec chmod 666 { } \;
Explanation : What we have done here is that we have searched for all the files with *.doc extension & those who doesnt have permission of **6 & finally we have changed the permission bits of all those files to 666 permission bits with chmod.
——————————————————————————–
22. Finding files in a specified directory only & not searching any further in depth. We presume that, we have a /shared directory & we do have many user created directories in it. But we are going to search only /shared root & our search should not go to any of other existing folder in /shared.
find /shared -maxdepth 1
-maxdepth : How much level of directories to descend while searching for files or specified criteria.
-maxdepth 1 means only search the specified path & do not descend any deep.
-maxdepth 0 means only apply the options & tests to command line arguements.
——————————————————————————–
23. (dated : 4th May ‘06.)
Cheek, Mark”
Hello Amit,
I enjoyed your tutorial on the find command. I have a query for you to try, if interested. Harder than it looks:
How about finding all the files that were created between two dates/times - for instance, find all files created between Apr 14 11:30 and Apr 26 16:30?
Thanks as always,
-Mark
Mark..
Thanks for going through this find command tutorial.. & here’s the solution for your query..
find /data/ -cnewer temp -and ! -cnewer ntemp
prior to that..
touch temp -t 200604141130
touch ntemp -t 200604261630
Make sure you are in /data directory while running those touch commands otherwise you have to specify their complete path while running the find command.
——————————————————————————–
24. (dated : 5th July)
Finding files with different but grouped criterias; like searching for jpg/tiff/eps/bmp file types & then deleting them from the shared folder.
find /shared/ -not -[color=blue]type d \( -iname *.jpg -or -iname *.tif -or -iname *.eps -or -iname *.bmp \) -exec rm -f {} \;
Please pay your attention to starting ” \( ” & trailing ” \) ” in the above command for groupping our search criteria as one. As we have placed an “or ” operator, we can see either one of them or all them combination or any combination will make them be processed to be deleted by exec option.
——————————————————————————–
This tutorial is also available at www.amitsharma.linuxbloggers.com in a graphical version.
