How do you search for a file in Linux? As it turns out, the operating system has some very powerful built-in tools for making file searches. Here’s what you need to know.
How to Search For a File in Linux From the Command Line
The most powerful method of searching for files on Linux is by using the command line. Just as Windows has a find command, so does Linux. The UNIX command find can very easily locate any type of file you might be looking for. Executed the right way, find can even look inside your files to identify particular text strings, and you can narrow your search to those files modified recently.
Basic Usage
The basic usage of find looks like the following: That command would search my home directory for all files ending in .jpg. There’s much more that can be done with the command, though.
Following Symbolic Links
By default, the find command will ignore symbolic links. However, the following command will follow those links: In the above example, the command would list all HTML files in the /var/www directory tree.
Locating Empty Files
Now, let’s say you wanted to identify all empty files in a directory. The following command would do just that: The key here is the -type and -empty parameters, used instead of the -name flag in previous examples.
Further Examples
Let’s look at one more basic example, that will return files changed in the preceding 6 days. In this command, all files ending in .db get listed, ignoring text case, but only if the files have been modified within the past 6 days by a user named randomperson.
Useful Options and Optimizations for Finding Linux Files
Additional flags can control what order find applies its search tests. By default, find filters the search results according to file name first, before running any other tests. This flag would be -O1, but there’s really no reason to include it. If you pass the -O2 flag, find filters based on the file name, then file type. The third choice, -O3, automatically reorders the search based on efficient use of resources and likelihood of returning relevant results. You can change more of the default behaviors using these parameters:
-maxdepth X: searches the specified directory and its subdirectories, to a maximum depth of X.-iname: searches files, ignoring capitalization.-not: only produces results that don’t match the specified test case.-type f: looks only for files-type d: looks only for directories-mtime X: returns only those files modified in the previous X days.
Using Grep to Locate Files Based on Content
Used on its own, find will only filter your files and directories based on file names and metadata. However, it’s possible to dig even deeper and use another Linux command to search files based on what they contain. The tool for this is the tried and true grep command. Here’s how it works. This command will look through every file (-type f) within the current directory tree (noted by . in the command). Next, it runs grep “for instance” on each of those files, printing the ones that contain forinstance. In the above command, the curly braces ({}) are a placeholder for the file results returned by the find command. These are placed inside single quotes (’) to prevent sending grep a misshapen file name. The -exec command is ended by the semicolon (;), which needs an escape (;)so Linux doesn’t try to interpret it as another command or parameter.
Using Exec to Take Your Find Results One Step Further
Now that you know how to send your find results to another command using -exec, you’ll be interested to learn you can do much more than just grep your search results. In fact, that capability makes it quite easy to quickly and easily mass-modify files. Issued by the root user in the /etc directory, the above command would search for all files named rc.conf. It would change the permissions for each of those files to give the file owner read permissions (chmod o+r). Keep in mind, -exec will run from the root directory of your Linux installation. If you prefer to run your additional command from the directory containing the matching file, use -execdir instead. By default, -exec and -execdir will continue to run uninterrupted on their own. If you’d rather see prompts before the second command does anything, replace the options with -ok or -okdir, respectively. To really make the most of find, it’s crucial to read the manual entry for the command.
How to Search for a File in Linux Using the GUI
Many GUI file managers for Linux will also support searches. In Ubuntu, you can search for specific files using the Files application. Just click the folder icon in the Dock, then the magnifying glass.
This GUI tool does allow you to filter by file type and when the file was last accessed or modified. What you don’t have with this option is the ability to run other commands quickly on your search results.
Learn the Ins and Outs of Linux
Whether you’re new to the Linux operating system or you’ve been running it for years, you can always brush up your skills. Now that you know how to search file a file in Linux, it might be useful to know how to use SCP to transfer the file to another machine. If you want to limit access to files, you’ll want to list the users on Linux first. Comment Name * Email *
Δ Save my name and email and send me emails as new comments are made to this post.