Finding Files in Linux

Find command


Find is a tool(comes with  findutils) which looks for files on a filesystem. find has a large number of options which can be used to customise the search (refer to the manual/info pages). Here are few of them work with regular expressions.

Basic example:


  • # find / −name file


This would look for a file named “file” and start at the root directory (it will search all directories including those that are mounted filesystems). The ‘−exec‘ option is one of the more advanced find operations. It executes a command on the files it finds. For example.


  • # find / −name ‘*.doc’ −exec cp ‘{}’ /tmp/ ‘;’


The above command would find any files with the extension ‘.doc‘ and copy them to your /tmp directory, obviously this command is quite useless, it’s just an example of what find can do. Note that the quotation marks are there to stop bash from trying to interpret the other characters as something.


  • # find / −path ‘/mnt/win_c’ −prune −o −name “string” −print


This example will search your entire directory tree (everything that is mounted under it) excluding /mnt/win_c and all of the subdirectories under /mnt/win_c. When using the −path option you can use wildcards.


To search ‘expr’ in this dir and below.


  • # find -name ‘*.[ch]’ | xargs grep -E ‘expr’


To search all regular files for ‘example’ in this dir and below

  • # find -type f -print0 | xargs -r0 grep -F ‘example’

To search all regular files for ‘example’ in this dir


  • # find -maxdepth 1 -type f | xargs grep -F ‘example’

Process each item with multiple commands (in while loop)


  • # find -maxdepth 1 -type d | while read dir; do echo $dir; echo cmd2; done


To find files not readable by all

  • # find -type f ! -perm -444


Find dirs not accessible by all

  • # find -type d ! -perm -111

Make archive of subset of dir/ and below

  • # find dir/ -name ‘*.txt’ | tar -c –files-from=- | bzip2 > dir_txt.tar.bz2

Make copy of subset of dir/ and below

  • # find dir/ -name ‘*.txt’ | xargs cp -a –target-directory=dir_txt/ –parents


slocate / Locate tools


Mlocate includes both the slocate and locate binaries. Slocate (secure locate) is a replacement for locate, both have identical syntax. On most distributions locate is an alias to slocate.

Syntax :

  • # slocate string
  • # locate string


Search cached index for names. This re is like glob *file*.txt


  • # locate -r ‘file[^/]*\.txt’


This won’t work unless You need to run either updatedb (as root) or slocate −u (as root) for slocate to work.


Whereis tool


whereis (comes with the util-linux packs) locates the binary, source, and manual page for a particular program, it uses exact matches only, if you only know part of the name use slocate.

Syntax :

  • # whereis program_name

Which tool


Virtually the same as whereis (comes with which package), except it only finds the executable (the physical program). It only looks in the PATH (environment variable) of a users shell.
Use the −a option to list all occurrences of the particular program_name in your path (so if theres more than one you can see it).

Command syntax:

  • # which program_name