How To Search For a File In Linux

Search PC

If you’re trying to search for a file on Linux, there are a few ways to do it, including using the terminal or on-screen apps. This guide will show you how.

No matter how careful you are about file management, you’ll eventually find yourself wondering where a particular document is. This is true on Mac, Windows, and even Linux.

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:

find /home/jeff -name “*.jpg”

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:

find -O3 -L /var/www/ -name “*.html”

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:

find . -type f -empty

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.

find /home -user randomperson -mtime 6 -iname "*.db"

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.

find with mtime

This execution returns all files in the given directory modified within the past 11 days

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.

find . -type f -exec grep "forinstance" '{}' \; -print

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.

find with exec grep

In this example, the find command returns any files containing the text “Hello”

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.

find . -name "rc.conf" -exec chmod o+r '{}' \;

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.

find with ok flag and prompts

Using the -ok flag, find will prompt you before taking any action on the files matching your search parameters

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.

how to search for a file in linux using the GUI

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.

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *


To Top