Finding files and programs
Video 106 of 140 in Virtualization Foundations, in the unit “The shell and the filesystem tree”. 2:37, with English and Hindi audio.
You know the name of a file, but not the folder it is in. Opening folders one by one is slow, and it is easy to miss something. Linux has three tools that help: which, find and locate. Each one searches in its own way, so each suits a different question. This article shows what each tool searches, where each one is fast or slow, and how to pick the right one.
What you will learn
- which finds a program, and it searches only the folders listed in PATH.
- find walks through folders and checks each name, so it is exact and up to date, but it is slow on large trees.
- locate reads a saved index, so it is fast, but a new file may not appear in it yet.
- Choose the tool by what you know and where you expect the file to be.
- Use find with a size or a name test to narrow a search, such as log files over 100 megabytes.
Why search instead of opening folders
On a Harbor Point Logistics server, an engineer needs the settings file of the stock service. Harbor Point is a fictional logistics firm, used for training examples. The settings file is somewhere under the etc folder. Opening folders one at a time is slow, and a file is easy to miss. Search tools do that work for you.
which, for finding a program
The which command finds a program. It searches only the folders listed in PATH, which is the shell's list of places to look for programs, and it prints the one path the shell would run. It does not search for ordinary files.
find, exact and live but slow on big trees
The find command walks through the folders you name and checks each name as it goes. It is exact and up to date, but it can be slow on large trees. Give it a folder to start in, and a test to match. Here it looks under etc for a file called fstab.
find /var/log -name '*.log' -size +100MNote: Folders you are not allowed to read are skipped, and an error is printed for each one.
locate, fast but from a saved index
The locate command does not walk the folders. It reads a saved index, so it answers at once. The index is refreshed from time to time, so a file made a minute ago may not be in it yet. Use it for a quick lookup, and use find when the answer must be exact.
locate fstabTry it yourself
You need a Linux terminal. Each step asks one question about where a file or a program lives, using one of the three tools.
Ask which program the shell would run for ls.
which lsWhat you should see: The shell prints /usr/bin/ls.
Search everything under etc for a file called fstab.
find /etc -name fstabWhat you should see: The shell prints /etc/fstab.
Search the log folder under var for log files over 100 megabytes.
find /var/log -name '*.log' -size +100MWhat you should see: Matching log files are listed. Folders you cannot read are skipped, with an error for each.
Ask the saved index for fstab. It answers at once, but the index may be out of date.
locate fstabWhat you should see: Paths from the saved index are printed. The list is quick, but it may be out of date.
Terms
- Saved index
- A stored list of file paths that locate reads, instead of walking through the folders again.
- PATH
- A variable holding the folders the shell searches, in turn, when you type the name of a program.
Check yourself
Which tool should you use to find a program, and why?
which, because it searches only the folders listed in PATH, and it is made for finding programs.
Why might locate miss a file you created a minute ago?
locate reads a saved index that is refreshed from time to time, so a new file may not be in it yet.
Why is find slower than locate on a large tree?
find walks every folder under the place you give it and checks each name. locate reads a saved index, so it answers at once.
Previous: Copying, moving and deleting safely. Next: Pipes and redirection.