Linux Server Admin Tools and GNU Benchmark Tools Directory, OpenSource Tools

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:

 

 

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.

 

 

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.

 

 

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.

 

 

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

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

 

Process each item with multiple commands (in while loop)

 

 

To find files not readable by all

 

Find dirs not accessible by all

Make archive of subset of dir/ and below

Make copy of subset of dir/ and below

 

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 :

 

Search cached index for names. This re is like glob *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 :

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: