find command on Linux

Written by
Date: 2007-05-04 10:36:30 00:00

If you need to find files in your disk, you have two command lines options (at least I know two)

  • Find
  • Locate

We now will look at Find Description Files files based on a search criteria.


find [directory-list] [expression]
Directory List
specifies find which directories to look for files you can specify more than one direcotory i.e.: find /home/jhon /etc -name file.txt This way it will look at /home/jhon/ and also at /etc/ and all its subdirectories, find always look at the subdirectories, if you do not specify any directory list it will look in the working directory
The expression is made up of options, tests and actions. If the expression contains no actions -print is executed and all files (on the specified directory list) will be listed. You can separate two options in the expression and will a boolean and if you want an or use -o separating them. Use a ! if you want to negate the result of any of the options.
  • -anewer file File was last accessed more recently than file was modified. If file is a symbolic link and the -H option or the -L option is in effect, the access time of the file it points to is always used.
  • -atime n File was last accessed n*24 hours ago. When find figures out how many 24-hour periods ago the file was last accessed, any fractional part is ignored, so to match -atime +1, a file has to have been accessed at least two days ago.
  • -cnewer file File’s status was last changed more recently than file was modi? fied. If file is a symbolic link and the -H option or the -L option is in effect, the status-change time of the file it points to is always used.
  • -ctime n File’s status was last changed n*24 hours ago. See the comments for -atime to understand how rounding affects the interpretation of file status change times.
  • -follow When specified and find encounters a symbolic link, it will follows the link
  • -group gname File belongs to group gname (numeric group ID allowed).
  • -ilname pattern Like -lname, but the match is case insensitive. If the -L option or the -follow option is in effect, this test returns false unless the symbolic link is broken.
  • -iname pattern Like -name, but the match is case insensitive. For example, the patterns ‘fo*’ and ‘F??’ match the file names ‘Foo’, ‘FOO’, ‘foo’, ‘fOo’, etc. In these patterns, unlike filename expansion by the shell, an initial ’.’ can be matched by ’*’. That is, find -name *bar will match the file ‘.foobar’. Please note that you should quote patterns as a matter of course, otherwise the shell will expand any wildcard characters in them.
  • -inum n File has inode number n. It is normally easier to use the -samefile test instead.
  • -links n File has n links.
  • -name pattern Base of file name (the path with the leading directories removed) matches shell pattern pattern. The metacharacters (‘*’, ‘?’, and ‘[]’) match a ‘.’ at the start of the base name (this is a change in findutils-4.2.2; see section STANDARDS CON? FORMANCE below). To ignore a directory and the files under it, use -prune; see an example in the description of -wholename. Braces are not recognised as being special, despite the fact that some shells including Bash imbue braces with a special meaning in shell patterns. The filename matching is performed with the use of the fnmatch(3) library function. Don’t forget to enclose the pattern in quotes in order to protect it from expansion by the shell.
  • -user name The file meets this criterion if it belongs to the user with that username.

You can check man find for all other options. Example:

find /home/user/ -name '*.txt'

Will look for all files with extension txt in the directory /home/user/, this is maybe the most used way.