find (Finding files)


This is very powerful command and most widely used for searching files in directory hierarchies. The steps followed by find command, first it searches continuously for the file in different directories, after finding file it matches with the conditions provided and then it takes the actions if any actions given with the command. 


Syntax:

find  directory_path  selection_condition  actions


here, most of the times we use "." or "/" as directory path where "." symbolizes current directory and "/" is the root directory. We can also mention the full path starting from root or current directory to save command execution time.







The syntax for the "find" may be little lengthy to a beginner but it is very effective in searching files. Files can be searched according to its:

  • names
  • type of file
  • file size
  • file permission
  • i-node number
  • time of creation, accessed or modified



Options to be used with this command:


  • "-name" : gives the option for searching the file with its name, if file is found it will be printed on the screen.


$pwd
/home/pinku/my_dir
$find . -name worker.txt
./worker.txt
$


Also, we can give full path with the command in place of only "." 


$find /home/pinku/my_dir -name worker.txt
/home/pinku/my_dir/worker.txt
$


  • "-type" : file type can be a ordinary file "-type f" , directory file "-type d" or a link file "-type l" so we have to give the options accordingly.


$ls -l
total 12
drwxr--r--  3 pinku  guest  512 Dec 24 02:48 A1
-rwxr--r--  1 pinku  guest    3 Dec 24 02:52 file1.txt
-rwxr--r--  1 pinku  guest    3 Dec 24 02:53 file2.txt
-rwxr--r--  1 pinku  guest    0 May 15  2015 myfile2.txt
$
$
$find . -type f
./myfile2.txt
./file1.txt
./file2.txt
$
$find . -type d
.
./A1
./A1/A2
$
$find . -type l
$








  • "-size" : gives you option to search file according to its file size.


$find . -size 0
./myfile2.txt
$

$find . -size -10
.
./myfile2.txt
./file1.txt
./A1
./A1/A2
./file2.txt
$


for file size less than 10, we mentioned "-10", if the size of file searching is greater than 10 we can mention like "+10" with "- size" option. By default, it takes the size in Byte if we have to search for the file size in KB, MB or GB, please mention k, M or G with the size. here in this case like "-1k" for KB :


$find . -type f -size -1k
./.a
./A_dir/myfile2.txt
./A_dir/file1.txt
./A_dir/file2.txt
./B_dir/myfile2.txt
./C_dir/file6.txt
...............

.....


  • "-perm" : it searches the file according to its given file permission (rwx to Owner, group and users). please refer this link for file permission with chmod command page.


$ls -l
total 12
drwxr--r--  3 pinku  guest  512 Dec 24 02:48 A1
-rwxr--r--  1 pinku  guest    3 Dec 24 02:52 file1.txt
-rwxr--r--  1 pinku  guest    3 Dec 24 02:53 file2.txt
-rwxrwxrwx  1 pinku  guest    0 May 15  2015 myfile2.txt
$


we are searching for the file which is having full permission in this location:


$find . -perm 777
./myfile2.txt
$


  • "-inum" : we can search the file with corresponding files i-node number. Please refer "ln" command for details of i-node.


$ls -i
2979926 A1              2979925 file1.txt       2979938 file2.txt       2983491 file3.txt       2979929 myfile2.txt
$
$find . -inum 2979938
./file2.txt
$


  • "-ctime" , "-mtime", "-atime" : These options are coming under searching file according to given time and the given time would be the number of days. Here, "c" stand for "file created", "m" stands for "file modified" and "a" stands for file accessed.


$ls -ltr
total 12
-rwxrwxrwx  1 pinku  guest    0 May 15  2015 myfile2.txt
drwxr--r--  3 pinku  guest  512 Dec 24 02:48 A1
-rwxr--r--  1 pinku  guest    3 Dec 24 02:52 file1.txt
-rwxr--r--  1 pinku  guest    3 Dec 24 02:53 file2.txt
$


we are searching for the files which are modified within approx 6 months (-180 days)


$find . -type f -mtime -180
./file1.txt
./file2.txt
$

and for files modified older than 6 months (+180 days):


$find . -type f -mtime +180
./myfile2.txt
$


similarly, we can give the same command for "-ctime" and "-atime", there is no change in syntax or something else.


  • "-mmin", "-cmin", "-amin" : These options are also coming under the file search according to time. The difference is here it represents number of minutes. So if we have to give condition 1 hour meaning 60 minutes with the command.


$ls -ltr
total 16
-rwxrwxrwx  1 pinku  guest    0 May 15  2015 myfile2.txt
drwxr--r--  3 pinku  guest  512 Dec 24 02:48 A1
-rwxr--r--  1 pinku  guest    3 Dec 24 02:52 file1.txt
-rwxr--r--  1 pinku  guest    3 Dec 24 02:53 file2.txt
-rw-r--r--  1 pinku  guest   32 May 10 14:14 file3.txt
$

 

Here we have to search for the file which is mod just modified 5 mins back, second is older than 5 mins:


$find . -type f -mmin -5
./file3.txt
$
$find . -type f -mmin +5
./myfile2.txt
./file1.txt
./file2.txt
$

some more practices with

Finding Files​​

NEXT->​

Cracking IT Interview