If you need to search for one or more particular files, Linux systems have a few powerful methods for locating them, such as the find and locate commands. Searching for a file with a specific name can be done, but you can also search for files that follow certain naming patterns. This can be broadened all the way to finding files based on file size, file extension, or a lot of other options.

It's also possible to find a particular directory or search for files based on their contents, such as finding all files containing a specific text, but we cover those topics in separate guides.

In this tutorial, you'll learn how to find a file in Linux by using the command line and GUI. Let's get started.

In this tutorial you will learn:
  • How to find a file in Linux via command line
  • How to find a file in Linux via GUI
How to find file in Linux
How to find file in Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux disto
Software find, locate, GUI file explorer
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # - requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ - requires given linux commands to be executed as a regular non-privileged user

Find a file with via command line

Most of this section will revolve around the find command. When it comes to finding a particular file or set of files, the find command is your best friend on Linux. So all you really need to know is how to use the command effectively. Through the following examples and explanations, you'll learn how to use it.

Search your present working directory and its subdirectories for a particular file:

$ find . -name "example.txt"

Find all .png image files in the /home directory and its subdirectories:

$ find /home -name "*.png"

Consider using the type -f option to only search for files (ignore directories), and the -iname option to make your search case insensitive:

$ find /home -type f -iname "example.txt"

SUBSCRIBE TO NEWSLETTER
Subscribe to Linux Career NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.


Find all .conf files that have been modified in the last seven days, are owned by user linuxconfig, and exist in that user's home directory:

$ find /home/linuxconfig -type f -user linuxconfig -mtime -7 -name "*.conf"

If you don't want the find command to traverse too deeply into subdirectories, you can specify a limit with the -maxdepth option. For example, this command will limit find to a depth of two subdirectories:

$ find . -type f -maxdepth 2 -name "example.txt"

The find command can automatically delete files it finds if you specify the -delete option. Be very careful with this option, and be sure to first run the find command without it so you know exactly what it plans to delete.

$ find . -type f -name "*.tmp" -delete

The find command's functionality can be further extended with the -exec option. Using this option allows you to execute a command on every file that find finds. For example, let's change the file permissions to 750 for every file found:

$ find . -type f -name "*.conf" -exec chmod 750 '{}' \; -print

In the above command, '{}' is a placeholder for the files that are found with find. The -exec option is terminated with a semicolon, which must be escaped, hence the \;. The -print option will output all the file names and paths to your terminal.

Finding a file on Linux
Finding a file on Linux

The locate command

The locate command works similarly to find, but it's not installed by default on every Linux distro. It searches the file system and stores a list of file names and locations inside of a database. Then it queries this database whenever you search for a file.



This results in locate being a lot faster than find. However, locate's database is only refreshed daily, so you should only use it to find files that are a couple days old. Stick to find when searching for recent files or when you want to specify the handy options we've shown you above. The locate command is much more limited as far as options go.

The command syntax is very simple. Just specify the file you'd like to find.

$ locate example.txt

To update the cache for locate, you can run:

$ sudo updatedb

Find a file with GUI

All GUIs look a little different but they all surely have a file explorer. We're using GNOME on our test machine, but searching for files on any GUI is pretty much the same, regardless of the desktop environment you're using.

On GNOME, we just need to open the file browser, click the magnifying glass (maybe it says 'search' on your GUI), and type the name of the file we wish to find.

Finding a file via file explorer GUI
Finding a file via file explorer GUI

Conclusion

Finding a file on Linux is very easy, but it's surprising how complex the search query can be when you need to find something very specific. The find and locate commands, as well as the GUI method, are all easy to learn when performing a basic search. Once you get comfortable with some of the find command's more complex capabilities, it becomes an extremely viable tool for finding and manipulating files in one go.

FIND LATEST LINUX JOBS on LinuxCareers.com
Submit your RESUME, create a JOB ALERT or subscribe to RSS feed.
LINUX CAREER NEWSLETTER
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.

You may also be interested in:



Comments and Discussions