How to Search for a File in Linux

Linux gives us a variety of ways for finding files on our system. By learning the basics of the find and locate commands, we can quickly retrieve the path to any file we need. In this tutorial, we will learn how to search for a file in Linux.

Using find Command

The find command is the most used tool used to search for files throughout a Linux system. It can search for files by name, size, type, etc.

Example 1. To search for a file by its name within the /path/to/search directory:

$ find /path/to/search -name file_name

Or to search the current directory and its subdirectories, use a . character as the directory path, or omit this argument entirely:

$ find . -name file_name
$ find -name file_name

Searching a file with find command

Example 2. When using find, it will output both files and directories that match the given keyword. We can use the option -type followed by -d to search only for directories and -f to search only for files.

$ find -name file_name -type f

Using find command to search for a file or directory

Example 3. By default, searches are case-sensitive. To ignore uppercase and lowercase during the search:

$ find -iname file_name

Example 4. To find all files with the a certain extension (in this example, .txt):

$ find -name "*.txt"

Example 5. The find command can also execute commands on the files that match your search criteria. To find a file and delete it, use the -delete option:

$ find -name file_name -delete

Searching for a file and deleting it with find command

Example 6. To find files by size, simply add the -size option followed by the size of the file. Size units can be specified in K, M, or G for kilobytes, megabytes, and gigabytes, respectively.

$ find / -size 3K
$ find / -size 3M
$ find / -size 3G

Example 7. The next commands will find the files whose sizes are greater than 20 kilobytes, megabytes, and gigabytes:

$ find / -size +20K
$ find / -size +20M
$ find / -size +20G

Using locate Command

The locate command is faster than the find command due to its ability of searching through the local system’s database instead of scanning the filesystem.

Use the corresponding command below to install locate depending on your Linux distribution:

$ sudo apt install plocate # Debian, Ubuntu, Linux Mint
$ sudo dnf install plocate # Red Hat, Fedora, AlmaLinux, CentOS
$ sudo pacman -S plocate # Manjaro, Arch Linux

The locate command takes an inventory of all files on your system, and stores them in a database for quick lookup. The database is updated daily, so recent file creations and deletions may not be synchronized to the database yet, unless we manually update the database.

We can manually update locate’s file database by running:

$ sudo updatedb

Example 1. To search for all the files that contain a given string:

$ locate file_name

Searching for files with locate command

Example 2. To search for an exact file name with locate:

$ locate -br ^file_name$

Searching for an exact file name using locate

In the command above, the -b option searches only for file names and ignores path names. The -r option activates regular expression syntax, and the ^ and $ characters search for anything that begins and ends with the given string, respectively.

Example 3. To output the total number of files with that match the search string, use the -c option:

$ locate -c .txt

Searching and counting total number of files with locate

This command shows the total number of .txt files on our system.

Example 4. We can use the -i option to search for a file ignoring the lowercase and uppercase:

$ locate -i File_Name

Example 5. Limit the number of results in the search output by adding the -l argument followed by the number of results we want to see:

$ locate file_name -l 3

Showing less output with locate

Using GUI to Search for Files

Using the GUI to search for files is very easy to do, but the process will be a little different depending on your desktop environment. For GNOME on Ubuntu, we open the Files app and hit the search icon to find any file of our desire.

Searching for a file on Linux via GUI

Leave a Comment

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