How to search for files in directory in linux

1. Find name command in linux

find [man page] – search for files in a directory hierarchy

2. Find synopsis

find [-H] [-L] [-P] [path...] [expression] 
3. Frequently used options for find command
-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) 

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  

use find to find files
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 

find - create two example files with touch
By default find command searches only for exact match:

$ find /tmp -name file  

find default behaviour is to search for exact match
To make find command case insensitive we can use -iname option.

$ find /tmp -iname file  

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

$ find /tmp/ -mmin -20 

find files which have been created in past 20 minutes
When searching for files according their access time, a table below might be helpful:

Options Description
-atime -3 All 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 

find files with certain size
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 {} \; 

execute a command on every file that find command finds
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 

use find to search for symbolic links

Source taken from