jump to navigation

The find command on Linux November 14, 2010

Posted by Tournas Dimitrios in Linux.
trackback

A previous article introduced the find command . Lets recap the basics before presenting its capabilities through many practical examples .

The find command is used to search the filesystem for files that meet  specified criteria.Almost any feature of a file can be specified, such as its name , its size ,the last time it was accessed even its inode number . I repeat myself so that it will well understood , ONLY file’s features , the file’s content can be accessed through other excellent tools like : grep , awk or sed . The syntax of the find command takes a little time to get used , but ones learned , is very usable . A find command is expressed as follows :

find (directory path) (criteria) (action)

The default directory path is “.” , the default criteria is “every file ” , and the default action is “print” (the filename) , so running the find command without arguments will simply descend the current directory , printing every filename . usually , however , the find command is given criteria to refine its search , in the form of command line switches . For example ,the -size command line switch is used to find files with the given size . The man page shows an overwhelming selection of criteria’s . The following table summarizes some of the most common search criteria .

Search Criteria for the find Command:

switch specification
-empty The file is a directory or regular file, and is empty.
-group gname The file is group owned by gname.
-inum n The file has an inode number n.
-links n The file has n links.
-mmin n The file was last modified n minutes ago.
-mtime n The file was last modified n days ago.
-name pattern The file’s name matches the file glob pattern.
-newer filename The file was modified more recently than filename.
-perm mode The file’s permissions are exactly mode.
-perm –mode All of the permission bits mode are set for the file.
-perm +mode Any of the permission bits mode are set for the file.
-size n The file has a size of n.
-type c The file is of type c, where c is “f” (regular file), “d” (directory), or “l” (symbolic link). See the man page for more details.
-user uname File is owned by the user uname.

You can also specify what actions should be taken if the specified creteria are met .By default , if no actions is specified , the file name is printed to the monitor screen (standard out) , one file per line . The most used actions that can be specified are listed in the following table . Of course you should explore the man page for a detailed reference .

Action  for the find Command:

Switch Action
-exec command ; Execute command on matching files. Use {} to indicate where filename should be substituted.
-ok command ; Like -exec, but prompt for each file
-ls Print file in ls -dils format.

Again, others actions exist. Consult the find(1) man page.

Now lets practice :

under construction 🙂

Basic operations with “find”
This example finds all files with name — testfile.txt  in the current directory and all it’s sub-directories. 

find -name “testfile.txt”

This example finds all files with name testfile.txt (ignoring the case) in the current directory and all it’s sub-directories. 

find -iname “testfile.txt”

Find the passwd.txt  file under all sub-directories starting from root directory. 

find / -name “passwd.txt”

Find the passwd file under root and one level down. (i.e root — level 1, and one sub-directory — level 2) 

find -maxdepth 2 -name “passwd”

Find the passwd file under root and two levels down. (i.e root — level 1, and two sub-directories — level 2 and 3 )

find / -maxdepth 3 -name “passwd”

Shows the files or directories whose name are not testfile.txt .Since the maxdepth is 1, this will look only under current directory. 

find -maxdepth 1 -not -iname “testfile.txt”

Using the -size option you can find files by size. 

Find files bigger than the given size  : find ~ -size +100M

Find files smaller than the given size : find ~ -size -100M

Find files that matches the exact given size :  find ~ -size 100M

Note: – means less than the give size, + means more than the given size, and no symbol means exact given size.

Find Files Based on file-type using option -type 

Find only the socket files. : find . -type s

Find all directories :  find . -type d

Find only the normal files : find . -type f

Find all the hidden files :  find . -type f -name “.*”

Find all the hidden directories : find -type d -name “.*”

Find all empty files (zero byte file) in your home directory and it’s subdirectory 

List all the empty files only in your home directory : find . -maxdepth 1 -empty

List only the non-hidden empty files only in the current directory : find . -maxdepth 1 -empty -not -name “.*”

Shows the files or directories whose name are not testfile.txt  .Since the maxdepth is 1, this will look only under current directory. 

find -maxdepth 1 -not -iname “testfile.txt”

There are times when you really need to find files under some criteria, and erase them, we will use find and rm to achieve this.

find . -type f -name “criteria-to-find” -exec rm -f {} \;

If you want to also delete directories, and not only files, you may use this one. 
find . -name “criteria-to-find”-exec rm -rf {} \;

Take care with the find command .In this example we will list all *.txt files, once you are sure those files are the ones you want to delete, run the second command. 
find . -type f -name “*.txt” -exec ls {} \;

find . -type f -name “*.txt” -exec rm -rf {} \;

T
g
g

Comments»

1. Cebola - February 11, 2013

looks wonderful


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s