Skip to content

January 6, 2011

Locating files using the find command with 15 examples


Find is a versatile tool which can be used to locate files and directories satisfying different user criteria. But the sheer number of options for this command line tool makes it at the same time both powerful and encumbering for the user. Here I will list a few combinations which one can use to get useful results using find command.

Find all HTML files starting with letter ‘a‘ in your current directory (Case sensitive)

$ find . -name a\*.html

Same as above but case insensitive search.

$ find . -iname a\*.html

Find files which are larger than 5 MB in size.

$ find . -size +5000k -type f

Here the ‘+’ in ‘+5000k’ indicates greater than and k is kilobytes. And the dot ‘.’ indicates the current directory. The -type option can take any of the following values:

    f - file
    d - directory
    l - symbolic link
    c - character
    p - named pipe (FIFO)
    s - socket
    b - block device

Find all empty files in your directory

$ find . -size 0c -type f

… Which is all files with 0 bytes size. The option -size can take the following:

    c - bytes
    w - 2 byte words
    k - kilo bytes
    b - 512 byte blocks

Note: The above command can also take the -empty parameter.

Find is very powerful in that you can combine it with other commands. For example, to find all empty files in the current directory and delete them, do the following:

$ find . -empty -maxdepth 1 -exec rm {} \;

To search for a html file having the text ‘Web sites‘ in it, you can combine find with grep as follows:

$ find . -type f -iname \*.html -exec grep -s "Web sites" {} \;

The¬† -s option in grep suppresses errors about non-existent or unreadable files. And {} is a placeholder for the files found. The semicolon ‘;’ is escaped using backslash so as not to be interpreted by bash shell.

Note: You can use the -exec option to combine any command in Linux with the find command. Some of the useful things you can do with it are as follows:

Compress log files on an individual basis

$ find /var -iname \*.log -exec bzip {} \;

Find all files which belong to user pravi and change its ownership to pcuser

# find / -user pravi -exec chown pcuser {} \;

Note: You can also use xargs command instead of the -exec option as follows:

$ find /var -iname \*.log | xargs bzip –

Find all files which do not belong to any user:

$ find . -nouser

Find files which have permissions rwx for user and rw for group and others :

$ find . -perm 766

List the output of the above command  find . -perm 766

$ find . -perm 766 -exec ls -l {} \;

Find all directories with name bug_files

$ find . -type d -iname \*bug_files\*

Suppose you want to find files of size between 800k and 900k, do the following:

$ find . \( -size +800k -and -size -900k \)

And how about getting a formatted output of the above command with the size of each file listed ?

$ find . \( -size +700k -and -size -1000k \) -exec du -Hs {} \; 2>/dev/null

Here, the ‘2>/dev/null’ means all the error messages are discarded or suppressed.

You can also limit your search by file system type. For example, to restrict search to files residing only in the NTFS and VFAT filesystem, do the following:

$ find / -maxdepth 2 \( -fstype vfat -or -fstype ntfs \) 2> /dev/null

These are the most common uses of the find command. You can see additional uses by reading the find manual.


Leave a comment

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments