ubuntu find file

If you’re new to Linux, the unfamiliar filesystem structure can make finding files difficult. You download a document without thinking about the selected location, and now you have no idea where it got saved; it’s a common scenario.

Ubuntu’s search tool is very handy in such cases. There are some CLI-based search tools too like find and locate that we’ll cover in this article.

Using the GUI Search Tool

The search tool is as straightforward as it gets. Just press the Super key, enter the keyword to search, and click on the Files result. For instance, I searched Discord, and the tool listed the files and directories with Discord in their name.

You can refine the search a lot more from the dropdown menu.

  • You can specify the last modified, last used, and created dates.
  • You can specify whether to search for folders or files (criteria ranging from general file types like images to exact extensions).
  • You can also specify whether to search the keyword in the filename only or the file’s contents as well.

Using the Find Command

The find command searches for files based on the specified criteria like the name and size. The basic syntax to list all files in a directory is 

find /directory/path/

Use the iname option to search for a file in the specified directory.

find /directory/path/ -iname filename

For instance, to look for a file named Discord in the Downloads directory, the command would look like

find /home/anup/Downloads/ -iname discord

Find has a large number of options, expressions, and operators that you can use to customize your search. You can refer to the find manpage for the extensive list, but for now, we’ll just cover some of the most useful ones.  

  • name – Search by filename (case-sensitive).
  • iname – Search by filename (not case-sensitive).
  • size – Search for files based on size (smaller/larger than or exactly equal to the specified size).
  • amin, mmin, cmin – Search for files based on when they were last accessed, modified, and changed respectively (in minutes).
  • atime, mtime, ctime – Same as above but the input is measured in days.
  • perm – Search based on file permissions.

Locate Files by Filename

Now let’s look at some examples to better understand these options and their use cases. To make a case-sensitive search for a file named ‘Discord’, run

find -name Discord

As we didn’t specify the path, it’ll search in the current directory as the default behavior. If you don’t remember the case, it’d be better to use iname. You can also specify the current directory manually with a period.

find . -iname discord 

The * wildcard is very useful for locating files with limited information. For instance, if you only remember the first part of the filename, you can use

find -iname disc*

Or, if you want to list files based on their extension, you can use something like

find -iname *.png

Search Files by Directory

As mentioned earlier, using find without specifying the path runs the search in the current directory. You can list all the files in a certain location like so

find /home/anup/Downloads/March/

And you can run a search based on a keyword like so

find /home/anup/Downloads/March -iname *config*

Most importantly, you can search the entire drive by specifying root like so  

find / -iname disc*

Find Files by Date & Time

When you need to find a file that you recently created, accessed, or modified, you can use the relevant time-related option.

For instance, to search your home directory for files whose attributes were changed in the last 2 hours, you could use

find ~/ -cmin -120

To search for PNG files that you accessed in the last 15 minutes, you could use

find ~/ -iname *.png -amin -15

Similarly, to search for files whose content was modified in the last 5 days, you could use

find ~/Downloads/ -mtime -5

Advanced Ways to Find Files

You can filter files by size using the c, k, M, and G options for Bytes, Kilobytes, Megabytes, and Gigabytes respectively. For instance, to search for files sized 40 Kb, you could use

find -size 40k

To search for PNG files under 2 MB, you could use

find -iname *.png -size -2M

To search for exectuable files over 1 GB on the entire drive, you could use

find / -iname *.exe -size +1G

You can find files based on their set permissions with the perm option.

find –perm 775

You can also use the empty option to find files with no content.

find ~/ -empty

Finally, you can also combine searches using operators (and, or, not). If you use multiple find options, and is assumed by default. So, both of the following commands return the same result.

find -iname *.png -size -2M
find -iname *.png -and -size -2M

You can use or to specify multiple criteria like so

find -iname *.png -or -iname *.txt

You can even combine operators to further refine the search. The following command searches for files whose names start with disc. But it doesn’t list files named discord.

find -iname ‘disc*’ -and -not -iname ‘discord’

Search With the Locate Command

The Find command is highly useful and versatile, but as it runs the searches in real-time, searches can take very long on systems with lots of files. If Find doesn’t feel like a good fit for you, try out Locate.

Install Locate and Update the Database

sudo apt install -y plocate

Locate searches its own databases of filenames which results in much faster searches. This does mean that you need to update the database manually from time to time.

sudo updatedb

If you decide to stick with Locate, it’s a good idea to set up a cron job to run updatedb automatically. But that’s for later. Right now, let’s just look at some basic usage examples.

Locate Use Examples

locate Discord

This command runs a case-sensitive search for files with ‘Discord’ in the filename. You can use the -i flag to ignore case distinctions like so

locate -i Discord

Aside from this, Locate’s usage is mostly the same as Find. You can use the * wildcard like so.

locate -i march*

You can also specify multiple criteria for the search. For instance, this command prints PNG files with Ubuntu in the file path.

locate -i ‘*.png’ ubuntu
Anup Thapa

Senior Writer

Anup Thapa is a Linux enthusiast with an extensive background in computer hardware and networking. His goal is to effectively communicate technical concepts in a simplified form understandable by new Linux users. To this end, he mainly writes beginner-friendly tutorials and troubleshooting guides. Outside of work, he enjoys reading up on a range of topics, traveling, working out, and MOBAs.