How to Find Files on Ubuntu using the Terminal

This tutorial will show you how to find files on your Ubuntu system using the command line.

Ubuntu Find Files

If you are using a desktop version of Ubuntu finding files is straightforward as opening up the Files application and using the search bar.

However, the process is slightly more complicated if you want to find a file using the terminal. You will have to utilize either the “find” or “locate” apps to find files within the terminal on Ubuntu.

The find command is an integral part of Ubuntu and is one of the best ways to find files on your system. The locate package allows you to perform the same searches quickly but requires a database to be refreshed regularly.

Over the following few sections, you will learn how to use either of these tools to find files on your Ubuntu system.

Using the Find Command on Ubuntu

One of the easiest ways to find files on an Ubuntu system using the terminal is the find tool.

This tool is a crucial part of the Linux system, meaning it should always be available regardless of what flavor of Ubuntu you use.

While we have a tutorial that covers using the find command on Linux, we will give a quick overview of some of the key usages for an Ubuntu system.

Find Files within a Directory on Ubuntu

For this first method, let us explore finding a file within a specific directory on Ubuntu. This is useful when you know roughly where your file should be but can’t find its exact location.

By specifying a directory, the find tool will search that directory and all of its sub-directories.

To search using this tool, you will want to use “find“, followed by the directory you want to search, followed by “-iname” and then the name of the file you want to find.

find /path/to/directory/ -iname FILENAME

If you want the search to be case-sensitive, swap out “iname” with “name“.

Example of Finding Files within a Directory

For example, we would use the following command if we wanted to find a file called “nginx.conf” within the “/etc/” directory.

find /etc/ -iname nginx.conf

Below you can see that the find command found our file within the “/etc/nginx/” directory on our Ubuntu device.

/etc/nginx/nginx.conf

Search for Files with only a Partial Name

You can still find files on Ubuntu even if you only know part of the file’s name. We can achieve this by using the wildcard symbol (*) within the filename string.

You can use the wildcard at either the start or end of the filename. Using it at the start means the string must end with that name. The string must start with that filename if used at the end.

If you use the wildcard at both ends, it will find files that contain that specified name.

find /path/to/directory/ -iname [*]FILENAME[*]

Example of Using Wildcards when Finding Files on Ubuntu

For example, you could use the following command if you wanted to find all “.conf” files within the “/etc/nginx/” directory on Ubuntu.

Since we wanted to find files that used the “.conf” extension, we prefaced it with the asterisk symbol (*).

find /etc/nginx/ -iname *.conf

A sample of the data returned from the above example is below.

/etc/nginx/fastcgi.conf
/etc/nginx/modules-enabled/50-mod-http-image-filter.conf
/etc/nginx/modules-enabled/50-mod-stream.conf
/etc/nginx/modules-enabled/50-mod-http-xslt-filter.conf
/etc/nginx/modules-enabled/50-mod-http-geoip.conf

How to Search the Whole Ubuntu System using Find

The last thing we will show you is how to use the find command to search the entire Ubuntu system. This works like the previous methods we have shown you. The only difference is the path we are using.

By setting the path to “/“, the find tool will start searching from the root directory and then search the entire system.

To perform a search like this, you must use a superuser such as a root user or by using sudo.

find / -iname FILENAME

For example, if you wanted to search your Ubuntu system for a file called “nginx.conf“, you could use the following command.

find / -iname nginx.conf

Using the Locate Command to Find Files on Ubuntu

An alternative to using the find command on Ubuntu is the Locate command. This command is far simpler to use but much more taxing on your Ubuntu system.

The advantage of the locate command on Ubuntu is that it can instantly find files on your system. It does this by building a list of every file on the system and its location.

The downside is that it will only find files created before the list was updated, which is only updated every 24 hours.

Installing Locate on Ubuntu

The locate tool is not a pre-installed part of the Ubuntu system and must be installed before we can use it.

1. To install the locate package on our system, we must first update the package list cache.

We can update this cache by using the following command within the terminal.

sudo apt update

2. Once the update completes, we can proceed to install the Locate package to our Ubuntu device by using the following command.

sudo apt install locate

3. You can ensure that the Locate database is up to date by running the command below.

If you are looking for newly created files, you should always run this before performing a search on Ubuntu with Locate.

sudo updatedb

Finding Files on Ubuntu using Locate

Once the locate packages database has been created, finding files on Ubuntu is a super speedy process.

To find files using locate, all you need to do is use “locate” followed by the name of the file you want to search for on your system.

locate FILENAME

Like the find command, this search can also be case insensitive by using the “-i” option.

locate -i FILENAME

Example of using Locate to Find a File

To showcase how this works, let us use the locate package to find a file called “php.ini” on our Ubuntu system.

To search for this file within our system, we need to use the following command.

locate php.ini

After running the above command, you will instantly get a list of every file with the name “php.ini” on your system. An example of what was found on our system is the following.

/etc/php/8.1/cli/php.ini
/etc/php/8.1/fpm/php.ini
/usr/lib/php/8.1/php.ini-development
/usr/lib/php/8.1/php.ini-production
/usr/lib/php/8.1/php.ini-production.cli

Using Locate to Find Multiple Files on Ubuntu

The locate command also allows you to find multiple files within one command. You need to specify the names one after another, leaving a space in between.

For example, if we wanted to find a file on our Ubuntu system called “nginx.conf” and another called “php.ini“, we could use the following.

locate nginx.conf php.ini

Below you can see how this command found both the “nginx.conf” and “php.ini” files on our system.

/etc/nginx/nginx.conf
/etc/php/8.1/cli/php.ini
/etc/php/8.1/fpm/php.ini

Conclusion

At this point in the tutorial, you should now have an idea of how to find files on Ubuntu while using the command line.

The two easiest ways to search for files within the terminal are to use the find command or the locate package.

The find tool is slightly more complicated to use on Ubuntu, but other than that has very little downsides. The Locate package will search Ubuntu super-fast but requires a database to be updated for files to be findable.

Please comment below if you have any issues finding files on your Ubuntu system.

If this tutorial helped you, check out our many other Ubuntu projects. We also have many guides showing you the general usage of Linux.

Leave a Reply

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