How to Locate Any File in Linux

December 12, 2022

How to Locate Any File in Linux

This guide is part of the “Linux Commands” series. This series is focused on providing an in-depth overview of Linux commands and tools, in an easy-to-follow manner!


Linux includes a variety of tools for searching for files, and two of the most commonly used are the find and locate commands.

The find command is a powerful tool that allows users to search for files based on a wide range of criteria, such as the file's name, size, ownership, and permissions. find also allows users to execute actions on the files that are found, making it a versatile tool for managing files in a Linux system. However, find can be slower to execute than locate, especially when searching through a large file system.

In contrast, the locate command is a faster option for searching for files by name as it uses a pre-built database of files on the system. That said, locate is less flexible than find, as it cannot search for files based on other criteria and does not have the ability to execute actions on the files that it finds.


How to use the find command

To use the find command, open a terminal and type find followed by the options and arguments that specify the search criteria. For example, the following command searches for files in the current directory with the .txt extension:

find . -name "*.txt"

# This command searches for files in the current directory with the .txt extension and prints their names to the terminal.

Search for files with a specific name:

find . -name "myfile.txt"

# This command searches for files with the name "myfile.txt" in the current directory and all its subdirectories, and prints the names of the files that are found.

Search for files of a specific type:

find . -type f

# This command searches for regular files in the current directory and all its subdirectories, and prints the names of the files that are found. The -type option can also be used to search for other file types, such as directories (-type d) or symbolic links (-type l).

Search for files with a specific size:

find . -size +100M

# This command searches for files in the current directory and all its subdirectories that are larger than 100 megabytes, and prints the names of the files that are found. The -size option can also be used to search for files with a specific size range, such as files between 10 and 100 megabytes (-size +10M -size -100M).

Search for files owned by a specific user or group:

find . -user john

Execute a command on the files that are found:

find . -name "*.tmp" -exec rm {} \;

# This command searches for files in the current directory and all its subdirectories with the .tmp extension, and then executes the rm command on each file to delete it. The -exec option allows you to execute any command on the files that are found, and the {} placeholder is used to specify the name of the file that the command should be executed on.

The find command can also be used to search for files based on their modification time, which is the time when the file's contents were last changed. This can be useful if you want to find files that were recently modified, or files that have not been modified in a long time.

find . -mtime 1

# This command searches for files in the current directory that were modified exactly 24 hours ago, and prints the names of the files that are found.

The -mtime option can also be used with a plus or minus sign to specify a range of modification times. For example, the following command searches for files in the current directory that were modified within the last three days:

find . -mtime -3

# This command searches for files in the current directory that were modified within the last three days, and prints the names of the files that are found.

In addition to the -mtime option, the find command also has the -atime and -ctime options, which allow you to search for files based on their access time (the time when the file was last accessed) and their status change time (the time when the file's metadata was last changed), respectively.


How to use the locate command

Unlike the find command, which searches the file system in real time, locate uses a pre-built database of files to quickly search for files by their exact name.

To use the locate command, open a terminal and type locate followed by the name of the file you want to search for. For example, the following command searches for files with the name "myfile.txt":

locate myfile.txt

# This command searches for files with the name "myfile.txt" in the file system and prints the names and paths of the files that are found.

To update the locate database, you can use the updatedb command:

sudo updatedb

# This command updates the locate database, which allows the locate command to return the most up-to-date results when searching for files. 

Search for files in a specific directory:

locate -d /var/log myfile.txt

Search for files that contain a specific string:

locate -i "hello world"

Search for files using a regular expression:

locate -r ".*\.txt$"

# This command searches for files with names that match the regular expression ".*.txt$", which matches all files that have the .txt extension. The -r option allows you to use regular expressions to search for files.

Search for files and print the results in a specific format:

locate -l 5 myfile.txt

# This command searches for files with the name "myfile.txt" in the file system and prints the names and paths of the first five files that are found.

Summary

In summary, the find and locate commands are both useful tools for searching for files in a Linux file system. find is a more powerful and flexible tool, while locate is faster but less versatile. The best choice of which command to use often depends on the specific needs of your situation.