Home > Articles

  • Print
  • + Share This

Finding Files and Directories

Although navigating your disk's directories and listing directory contents can be helpful for finding needed files, Linux comes with several search utilities that will work much faster. The following sections demonstrate how to use several of these commands to quickly and efficiently find files or directories.

Finding Files with the find Command

The find command is used to search all mounted file systems for the name or partial name of a file or directory. This powerful command can be used to do much more than simply find files. To search for files or directories, specify a search path and search pattern on the command line, like so:

$ find  /usr -name *emacs -xdev

As shown in the following, this search of the /usr directory for the emacs editor and other files locates the emacs program and its manual page. But be warned! Searching a file system, especially in a network environment with remotely mounted file systems, can take a long time. The -xdev option specifies that you only want to search your local machine. I should also note that -xdev will not search across other file systems that you have mounted on your local machine:

/usr/bin/emacs 
/usr/bin/xemacs 
/usr/lib/emacs 
/usr/share/emacs 

The find command reports files by type, date, time, size, or search pattern. Use the -atime command-line option to find new or little-used programs. The -type f flag lets find know that the type of item you are searching for is a file (as opposed to a directory, symlink, device, and so on). For example, the following command line locates programs accessed in the last 100 days:

$ find /usr/bin -type f -atime +100 -print

The next command line locates programs that are one or fewer days old:

$ find /usr/bin -type f -atime -1 -print

To search by size, use the -size option, followed by a number in blocks (512 bytes), bytes, or kilobytes (1024 bytes). For example, the following command locates all programs in the /usr/bin directory that are larger than 500,000 bytes:

$ find /usr/bin -type f -size +500k -print

Use the -exec option to act on found files. For example, the following command line deletes all core dumps found in your Linux file system. Without the -xdev option, Linux system managers can use this approach to also clean up other file systems.

$ find / -name core -xdev -exec rm ´{}' ´;' 

This command line works by starting a search at the /, or root directory. The find command descends through all your directories, looking for any file that is named core. If a file is found, the rm command is used to delete the file.

For more details about the find command's many different options and search specifications, see its manual page.

Finding Files and Directories with the locate Command

Use the locate command to quickly locate files or directories on your system. This program works very quickly because it uses a database of filenames instead of searching your hard drives, as the find program does. Wildcards can be used to either expand or narrow a search. For example, to look for any icons of the emacs text editor, use the locate command like this:

$ locate *icon*emacs* 
/usr/share/icons/emacs_3d.xpm 
/usr/share/icons/lemacs.xpm 
/usr/share/icons/xemacs.xpm 

The locate command searches a database called locatedb. This database is created with the updatedb command. The format of the locatedb database is documented in the locatedb manual page. System managers generally use the cron daemon and an updatedb crontab entry to keep the locate command's database current and accurate.

Finding Programs and Manual Pages with the whereis Command and the which Command

The whereis command is used to list the locations of program binaries, related files, and manual pages. Use this command to verify manual pages and to determine the pathnames of programs or their source files. For example, type the following:

This command yields the following result:

vi: /bin/vi    /usr/man/man1/vi.1

The whereis command works a lot faster than the find command because the paths searched are built into the program. To search only for manual pages, use the -m option. Use the -b option for binary searches and the -s option to search for sources.

The which command is more rudimentary than the whereis command but is also designed to help you locate a file or application. The which command simply checks to see whether the program specified is located anywhere within your path. It prints to screen the first instance it finds and then stops, as in the following example:

$ which vi 
/bin/vi 
  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.