Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
By amitsharma_26 at 2005-12-06 08:40
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
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 <grp-name>
-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 <pattern> : Pattern could be a exact file name or wildcard * can be used
--------------------------------------------------------------------------------
2. Find file with file name 'FoO'
find / -iname foo
-iname <pattern> : Same as -name variable but the match is case insensitive.
Same way -lname & -ilname variable works.
-lname <pattern> : File is a symbolic link whose content matches the pattern specified.
-ilname <pattern> : Same as -lname variable but the match is case insensitive.
--------------------------------------------------------------------------------
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 { } /<destination-dir> \;
-exec <command> : Executes command. The string { } replaced by the output of find command.
" \;" 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 <user-name>
find / -uid <uid-number>
-user <user-name/uid> : File is owned by username (numeric user ID also allowed)
--------------------------------------------------------------------------------
6. Find all the files owned by a group.
find / -group <group-name>
find / -gid <gid-number>
-group / -gid <group-name/gid-nos> : File is owned by group-name or group-id.
--------------------------------------------------------------------------------
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 <mode> : Find files with exact permission bits mentiond in <mode>.
--------------------------------------------------------------------------------
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 <n>
-links <n> : File has <n> nos of 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.
--------------------------------------------------------------------------------
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> : File status was last changed n*24 hours ago.
n : exact n*24 hours ago.
-n : under n*24 hours.
+n : over n*24 hours.
Also :
find / -cmin <n> or <-n> or <+n>
-cmin <n> : File status was last changed n minutes ago.
--------------------------------------------------------------------------------
14. Find files those were accessed in last 10 minutes.
find / -amin -10
-amin <n> : File was last accessed n minutes ago.
n : exact n minutes ago.
-n : under n minutes.
+n : over n minutes.
Also :
find / -atime <n> or <-n> or <+n>
-atime <n> : Files those was last accessed in n*24 days.
--------------------------------------------------------------------------------
15. Find files those were not modified in last 3 days.
find / -mtime +3
-mtime <n> : File's data was last modified n*24 hours ago.
n : exact n*24 hours ago.
-n : under n* 24 hours.
+n ; over n*24 hours.
Also :
find / -mmin <n> or <-n> or <+n>
-mmin <n> : Those files who's data was last modified n minutes ago.
--------------------------------------------------------------------------------
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 <file-name> : File was last accessed more recently than specified <file-name>.
-cnewer <file-name> : File's status was last modified more recently than specified file.
-newer <file-name> : File was modified more recently than specified file.
Also to mentioned specified file in <file-name> can have absolute path also.
--------------------------------------------------------------------------------
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.
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.
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" <mark.cheek@***.com> wrote:
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
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.
--------------------------------------------------------------------------------
find / -name foo
find / -name fo*
-name : Pattern could be a exact file name or wildcard * can be used
--------------------------------------------------------------------------------
2. Find file with file name 'FoO'
find / -iname foo
-iname : Same as -name variable but the match is case insensitive.
Same way -lname & -ilname variable works.
-lname : File is a symbolic link whose content matches the pattern specified.
-ilname : Same as -lname variable but the match is case insensitive.
--------------------------------------------------------------------------------
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 : Executes command. The string { } replaced by the output of find command.
" \;" needs to be there as it tells the end of arguements provided to -exec variable.
A quick note on this use of -name ... be SURE that there's no files
that match the globbed pattern in the directory you invoke the command
from if you don't wrap the search-string in quotes.
E.g.. if you had the following directories in the local path
In other words: the globbing of the wildcard by the shell will take
precedence :}
Cheers,
Tink
by XavierP on Sun, 2006-08-06 08:17
Please note, this Tutorial has now been updated. Points 22 onwards are new.
by sysconfig on Mon, 2006-09-11 14:43
Sometimes we need to find the file in server which we do not know where exactly it is located:
Search and list all files from current directory and down for the string ABC:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
find / -name foo
find / -name fo*
-name : Pattern could be a exact file name or wildcard * can be used
--------------------------------------------------------------------------------
2. Find file with file name 'FoO'
find / -iname foo
-iname : Same as -name variable but the match is case insensitive.
Same way -lname & -ilname variable works.
-lname : File is a symbolic link whose content matches the pattern specified.
-ilname : Same as -lname variable but the match is case insensitive.
--------------------------------------------------------------------------------
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 : Executes command. The string { } replaced by the output of find command.
" \;" needs to be there as it tells the end of arguements provided to -exec variable.
that match the globbed pattern in the directory you invoke the command
from if you don't wrap the search-string in quotes.
E.g.. if you had the following directories in the local path
precedence :}
Cheers,
Tink
Search and list all files from current directory and down for the string ABC:
find ./ -type f -print | xargs grep -H "ABC" /dev/null
egrep -r ABC *
the system: (could take a long time)
Find all world writable files:
find / -perm -2 ! -type l -ls
Could you help me out, if i want to run multiple commands in find command with -exec option, is there any way out to do this ?
Thanks in Advance
See my post here for an example.
Use single quotes to prevent globing: