Find command - Search for files in Linux

September 21, 2005
This article explains how to use the Linux command find to search for files.

find helps you to locate files and directories satisfying different criteria.

But the sheer number of options find has makes it simultaneously - both powerful and complex to use.

find syntax


The basic syntax for using find is as follows.

find {where-to-look} {search-criteria} {what-to-do-with-the-result}

  • {where-to-look} indicates the full path of the location to search. Use '.' to denote current directory.
  • {search-criteria} - This is a combination of various options which includes regular expressions, you can use to filter the search.
  • {what-to-do-with-the-result} - How do you want the results to be displayed ? Or do you want them to be piped to another program ?

find options


The frequently used find options are as follows.

  • -name {search-pattern} - The search is case sensitive.
  • -iname {search-pattern} - The search is case insensitive.
  • -size [+|-]n[cwbkMG] - The file(s) should be [atleast/atmost] 'n' [units] in size.
    and the units are - c {bytes}; w {two-byte words}; b {512-byte blocks}; k {Kilobytes}; M {Megabytes}; G {Gigabytes}.
  • -type [fdlcpsb] - The type of file to search for. File types can be one of the following -
    f {file}; d {directory}; l {symbolic link}; c {character}; p {named pipe - FIFO}; s {socket}; b {block device}

There are a whole lot more options for the 'find' command. To know more about the options, read the 'find' manpage.

Find examples


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 indicates it should be a file.

Find all empty files in your directory

$ find . -size 0c -type f

You can also use -empty instead of -size 0c to search for all the empty files.

Find is very powerful in that you can combine it with other commands.

Find all empty files in the current directory and delete them -

$ 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" {} \;

-s is an option of grep tool that suppresses errors about non-existent or unreadable files.

{} is a placeholder for the files found.

; is escaped using backslash so as not to be interpreted by bash shell.

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 lal and change its ownership to ravi

# find / -user lal -exec chown ravi {} \;

You can also use xargs command instead of the -exec option as shown below to get the same result.

$ 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

... and then list them.

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

Find all directories with name music_files

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

Find files of size between 700k and 1000k.

$ find . \( -size +700k -and -size -1000k \)

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

2>/dev/null means all the error messages are discarded or suppressed. See Input-Output Redirection to know more.

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.

11 comments:

  • Thank you very much! If I forget something, I can always use this article as a cheatsheet!

  • Excellent posts you have here. I've been using find, but never as extensively.Thank you :)

  • Very useful. Thanks for the tips.

  • The 'find' command in Linux is very slow. It has to scan your entire hard drive each time you do a search. I would highly recommend using slocate which creates an index of your entire hard drive so that the search almost shows up instantly. One needs to run 'updatedb' if they haven't in a while, then 'locate ' to search.

    [jung@localhost ~]# updatedb
    [jung@localhost ~]# locate install
    /sbin/installkernel
    /sbin/grub-install
    /root/install.log
    /root/install.log.syslog

  • I've noticed that the command $ find . -name a\*.html not only look for in the current directory, but either in subdirs, how to limit the search at the current without subs?

  • Ravi

    You can restrict the search to the current directory by using the -maxdepth option with the find command.
    $ find . -maxdepth 1 -name a\*.html

  • The -exec option is known to be extremely slow as find forks a new process for each instance. The best solution is to pipe the output to xargs.

    Example:
    find /home -name .rhosts -print0 | xargs -0 rm

    -print0 and -0 are used to escape filenames
    containing spaces.

    - Jeff Schroeder

  • Thank you very much, these are very useful tips!

  • Excellent post! How do you do this post after post? Thanks a lot.

    Hey, this was back in 2005. Would anyone find fault if a lazy user chooses to use Google Desktop and FORGET find? I think, we need find only for scripting? Am I wrong?

  • command to find all of the files which have been accessed within the last 10 days.

    find / -type f -atime -10 > March.files

    Thanks
    RAVI
    http://www.indianGNU.org

  • Just add updatedb to your crontab and locate command will be available to you, though it has fewer options than find.