Tips For Finding Files In Linux Part 1
One of the problems with working with files on a computer is keeping track of where they all are. It can be easy to forget what directory a file is stored in, then waste time hunting for it. While graphical user interfaces usually offer simple methods to find files, when you are staring blankly at that blinking cursor at the command line things aren’t so simple.
File Searches
The most basic way to search for files would be to use the ls command, for example:
ls file.txt
Unfortunately, that would only find the file file.txt if it were in the current working directory. While you can use ls with the -R flag to list files recursively, this can serve up too much information. Even narrowing down the exact filename using grep can still leave you without any idea where the file is without resorting to piping the output through sed or awk to reformat. While these approaches work, their complexity hints that there is probably a more straightforward way to accomplish what should be a simple task. The good news is, there is.
Locate
Introducing the locate command. This command comes in the default repositories for most Linux distributions and in many is installed by default. It consists of two commands: the locate command itself that lets you search for files, and the updatedb command that is used to scan the filesystem and fill in the database that locate uses to find files. Most distributions are configured to run the updatedb command automatically during the night, so be mindful that changes to the filesystem during the daytime won’t be picked up by locate unless you run updatedb again manually.
The locate command can be used to search for a full filename, a partial filename, or can attempt to match a regular expression to a filename. Here’s an example:
locate file.txt
This will search for a file called file.txt on the filesystem and list the full paths for each matching file found. When searching for a partial match, you only need to provide part of the filename, for example:
locate ile
The above command will search for any file with “ile” in the file name, so this would match our earlier file.txt as well as any other file that had “ile” anywhere in the file name. This can be helpful if you can only remember part of a filename.
Providing locate with multiple search terms is possible, and locate will return the matches for each of the terms provided. Just separate the terms with a space, as below:
locate ile ole
The above command searches for files where the names contain either “ile” or “ole”
Searches with locate are case sensitive, so if you can’t remember where letters may be capital or lower case in the filename you will need to use the -i flag.
locate -i ilE
If you are familiar with regular expressions and how to use them then you can use the -r flag to have locate treat the supplied search terms as regular expressions:
locate -r “ile”
Something to note is that locate will only find files that are accessible by the user calling the command. So if you are searching for system files or files owned by another user you may want to use the sudo command to have locate run as the root user.
That rounds up some basic methods for finding files using locate. In this article, you needed to know the filename or part of the file name to find the file you are looking for. In part two we’ll be looking at some more advanced file searching methods using the find command.