Find


From Linuxconfig.org

Jump to: navigation, search

.. back to the list of Linux Commands

Name

find - search for files in a directory hierarchy

Synopsis - man page

find [-H] [-L] [-P] [path...] [expression]

Frequently used options

-iname pattern
       Like -name, but the match is case insensitive.
-name pattern
      Base  of  file  name  (the  path  with  the  leading directories
      removed) matches  shell  pattern  pattern.
-mmin n
      File's data was last modified n minutes ago.
-size n[cwbkMG]
      File uses n units of space.  The following suffixes can be used:
-exec command ;
      Execute command; true if 0 status is  returned.   All  following
      arguments to find are taken to be arguments to the command until
      an argument consisting of `;' is encountered.
-type c
      File is of type c:

      b      block (buffered) special

      c      character (unbuffered) special

      d      directory

      p      named pipe (FIFO)

      f      regular file

      l      symbolic link; this is never true if the -L option or the
             -follow  option is in effect, unless the symbolic link is
             broken.  If you want to search for symbolic links when -L
             is in effect, use -xtype.

      s      socket

      D      door (Solaris)

Examples

Execution of find command can grow to very high complexity. One would be able to write a whole book in regards to number of options and arguments which can be used with find command. First we can try to search for file. I forgot where the resolv.conf file is hidden. We can attempt to find it in / directory. In this example I'm going to redirect standard error to /dev/null, feel free to run find command without it. The syntax goes like this, find (actual command) where ( / ) and what ( -name resolv.conf ):

$ find / -name resolv.conf 
Image:find01.gif

Lets assume that we have two files in /tmp directory: file and File. Unix system are case sensitive so file and File are two different files. touch command can help us to create those files:

$ touch /tmp/file /tmp/File
Image:find02.gif

By default find command searches only for exact match:

$ find /tmp -name file 
Image:find03.gif

To make find command case insensitive we can use -iname option.

$ find /tmp -iname file 
Image:find04.gif

To find files which have been created in past 20 minutes from now we can use -mmin option:

$ find /tmp/ -mmin -20
Image:find05.gif

When searching for files according their access time table below might be helpful:

OptionsDescription
-atime -3All files that were last accessed less than 3 days ago
-atime +5 All files that were last accessed more than 5 days ago
-atime 4 All files that were last accessed exactly 4 days ago

lets find files with certain size. In this case we search for files in /var/log with size of 8K:

$ find /var/log/ -size 8k
Image:find06.gif

We can also instruct find command to execute certain commands on each file it finds. Lets change permissions for both files, file and File in /tmp directory to chmod 777.

$ find /tmp -iname file -exec chmod 777 {} \;
Image:find07.gif

find can also search for certain types of files. For example if we are looking for symbolic links we can run find command with -type option:

$ find / -maxdepth 1 -type l
Image:find08.gif

.. back to the list of Linux Commands

Personal tools
Linux Commands