comp.org.uk

Networking | Programming | Security | Linux | Computer Science | About

Finding files on Linux

Before using the file management commands mentioned in the preceding section, you must know the locations of the files involved. The fastest method to search for files in the Linux directory tree is to use the locate command. For example, to view all of the files underneath the root directory with the file name “inittab” or with “inittab” as part of the filename, you can simply type locate inittab at a command prompt:

locate inittab

The locate command looks in a premade database that contains a list of all the files on the system. This database is indexed much like a textbook for fast searching, yet can become outdated as files are added and removed from the system, which happens on a regular basis. As a result, the database used for the locate command (/var/lib/mlocate/mlocate.db) is updated each day automatically and can be updated manually by running the updatedb command at a command prompt.

You can configure the directories that are searched by the updatedb command by editing the /etc/updatedb.conf file.

As the locate command searches all files on the filesystem, it returns too much information to display on the screen. To make the output easier to read, you can use the more (or less) command to pause the output, as in locate inittab | more. To prevent the problem entirely, you can do more specific searches.

A slower yet more versatile method for locating files on the filesystem is to use the find command. The find command does not use a premade index of files; instead, it searches the directory tree recursively, starting from a certain directory for files that meet a certain criterion. The format of the find command is find -criteria

For example, to find any files named “inittab” underneath the /etc directory, you can use the command:

find /etc –name inittab

You can also use wildcard metacharacters with the find command; however, these wildcards must be protected from shell interpretation, as they must only be interpreted by the find command. To do this, ensure that any wildcard metacharacters are enclosed within quote characters.

An example of using the find command with wildcard metacharacters to find all files that start with the letters “host” underneath the /etc directory is shown in the following output:

find /etc -name "host*"

Although searching by name is the most common criteria used with the find command, many other criteria can be used with the find command as well. To find all files starting from the /var directory that have a size greater than 4096K (kilobytes), you can use the following command:

find /var -size +4096k

As well, if you want to find all the directories only underneath the /boot directory, you can type the following command:

find /boot -type d

Although the find command can be used to search for files based on many criteria, it might take several minutes to complete the search if the number of directories and files being searched is large. To reduce the time needed to search, narrow down the directories searched by specifying a subdirectory when possible. It takes less time to search the /usr/ local/bin directory and its subdirectories, compared to searching the /usr directory and all of its subdirectories.


Published on Wed 08 April 2009 by Larry Epson in Linux with tag(s): locate find linux