How to find a file in Linux

How To Find A File in Linux
(Image credit: Future)

No matter your distribution, knowing how to find a file in Linux is an essential skill for any fan of the open-source OS. 

Whether you're using one with a graphical desktop interface like Ubuntu or Mint, or your running a command-line driven system like Debian, Linux makes it easy to find what you need quick and easy.

How to find a file on Linux using a desktop file manager

Similar in look and feel to Microsoft Windows or Apple's MacOS, many Linux distributions come with different desktop environments, like GNOME or KDE, to make them more user friendly and intuitive. While there might be slight differences between them, every Linux distribution is built on the same underlying file system.

How To Find A File On Linux Mint

(Image credit: Future)

When searching for a file on a Linux distribution with a graphical interface, the system's file manager will have a built-in search operation similar to those on Windows of macOS. Open up your file explorer (in the desktop's application menu, use the search bar and enter "Files" if you're having trouble finding) and it will open up on the user's home directory. 

At the top of the file explorer window, you'll see either an address bar with a magnifying glass icon next to it or a separate search field entirely, depending on the desktop environment you use. Either click on the magnifying glass or click into the search field and begin typing in your search term. 

As you type, the results field should start populating with partial matches, but you'll still need to type in the complete term you want, or close to it, to winnow this list down. If results do not auto-populate with partial results, hit Enter and Linux will begin searching for whatever term is in the search bar.

Ubuntu Desktop Interface

(Image credit: Future)

If you don't see a file or directory that you know should be there, chances are that you need to broaden the directory scope of the search. Since Linux only searches from the current working directory and lower for a file, you might need to navigate to an appropriate directory in which to begin a search.

If you want to expand your search, navigate up through the directory tree as high as you need to (with the root, or /, directory being the highest possible) and then perform your search. 

Different interfaces handle file system navigation differently, so how to move up the trees might not be as intuitive as moving down the tree through subdirectories. Look for an up arrow next to the address bar or a side panel with essential file system locations. 

Conversely, if you want to narrow your search results, start moving down through the subdirectories most likely to contain what you are looking for before searching. 

This is especially helpful when using very broad search terms or if you want to find all files of a specific type (i.e. jpgs) since these tend to return larger-than-intended lists of possible matches.

This is because the default behavior for Linux's is a so-called "greedy" search, which will err on the side of returning files that might be the file you're looking for, even if it barely qualified as a match. For example, searching for the word "term" will return "term", "terms", "terminate", "terminal", "determine", and so on.

If you don't know the exact file or directory name you're looking for, you can use wildcards characters to search for partial or incomplete matches to your search term. These lists can be unintentionally large, so don't make the search term so broad as to make the results list meaningless.

Ubuntu Desktop Interface With An Open Terminal Window

(Image credit: Future)

Finding a file using the Linux command-line interface

The Linux command-line has an intimidating reputation, especially for computer users who've only ever used PCs with a graphical user interface like Windows or macOS. The truth is it's no more complicated than text messaging with your phone, you just need to know the commands and how to use the different options available.

The Linux command you need to know is find and to use it, you need to tell it where to begin searching and what to search for, like this:

find directory -name filename

How To Find A File On A Linux Command Line

(Image credit: Future)

The directory doesn't need to be the current working directory, it can start a search beginning in any directory from any directory. If you open a terminal window, you'll begin in the same home directory as with the desktop file explorer, but rather than navigate to root to begin a search, you can simply tell find to start in the root folder:

sudo find / -name filename

When searching from root, you'll need to have root privileges to even read the contents of certain directories, so you'll need to prefix your command with sudo

The pathname for the directory you pass to the command can be absolute or relative, so that the above command can also be written like this and it will work the same:

sudo find ../../ -name filename

You can also use the same wildcard characters for partial filenames or to find all files of a certain type and files with similar names. And you can do more with the files you do find from a command-line search than with a GUI file explorer search.

The command line allows you to pass the results of a search directly into another Linux command as input. For example, you can find all files of a certain type and feed them into a shell script that can renames them all to make them easier to organize or deletes unnecessary duplicates.

TOPICS
John Loeffler
Components Editor

John (He/Him) is the Components Editor here at TechRadar and he is also a programmer, gamer, activist, and Brooklyn College alum currently living in Brooklyn, NY. 

Named by the CTA as a CES 2020 Media Trailblazer for his science and technology reporting, John specializes in all areas of computer science, including industry news, hardware reviews, PC gaming, as well as general science writing and the social impact of the tech industry.

You can find him online on Threads @johnloeffler.

Currently playing: Baldur's Gate 3 (just like everyone else).