4 minutes

Find Files in Linux

Jul, 24th 2023

You should already be aware of how much a search for a file can sometimes feel like looking for a needle in a haystack.

Fortunately, finding files on Linux is not that hard.

In this chapter, we will learn about two important commands in Linux that will save you a lot of time and help you find what you’re looking for in less time.

The Find Command

To search for a file on your Linux system, you can use the command find.

find  

After typing the command, you need to specify the directory where you want to search, and then you will have to provide the options for your search.

There are a lot of ways you can use this command depending on the options and arguments that you provide with it. We’ll look here at some of the most common ways to use it.

Search by name

The simplest way to use find is with the “-name” option. With this option, you should provide the name of the file that you wish to find.

In this example, I’m searching for a file called “hello” in the current directory.

You can also provide a pattern using what is called Globbing. This consists of specifying file names using wildcard characters. For instance, the most common wildcard character is the asterisk (*). This symbol matches any character.

So, to put this into practice, the following is a practical command that you can use to find and locate all pdf files in your system. (Notice that we specified ‘/’ as the path to search at, this means that find will search in the entire system starting from the root directory).

As you can see, I have executed the command as root using sudo so that I don’t get errors when find searches within directories that my normal user account doesn’t have access to. In future chapters, we’ll see how to avoid those errors without switching to root, but for the time being, you can use sudo.

The “-name” option is case-sensitive. This means that it will distinguish between uppercase and lowercase letters. For a case-insensitive search, you can use the “-iname” option instead of “-name“.

Search by Owner

If you want to find files owned by a certain user, then you can use the “-user” option.

The following example returns all files that are owned by amine on the system.

Search by Permissions

You can also look for files that have a certain set of permissions. For instance, the following command searches for all files that have read and write rights for their owners and groups and only read rights for other users.

If you’re still confused about the permissions, I invite you to go back to the previous chapter where I cover it in more detail.

Search by Type

You can also specify whether you are searching for directories or files. You can do this by using the “-type” option followed by ‘d’ for directories or ‘f’ for files.

Search by Size

If you want to find a file that has a certain size, you can use the “-size” option. It should be followed by the search criteria. The search criteria will include a numeric value and a suffix.

Below is a list of common suffixes that you can use along with the “-size” parameter:

  • c: Byte.
  • k: Kilo-Byte.
  • M: Mega-Byte.
  • G: Giga-Byte.

In this example, the find command will return all files inside the current directory that have exactly 95 Bytes.

You can also add a plus (+) or a minus (-) sign before the search criteria to find files with a size greater than or less than the provided value.

In the following example, the find command will return all files that have a size greater than 128 Kilobytes in the current directory.

Search by Date

You can also search for files by date, or more specifically by their modification date.

To do that, just add the “-mtime” option followed by a number of days.

The following command will return all files in the current directory that were modified or created during the last 10 days.

The Locate Command

Locate is another, simpler, search command on Linux that is much faster than find. However, it has one major drawback: In certain cases, when you search for something that you are 100% sure exists on your system, locate doesn’t necessarily return a result contrary to what you would expect.

The reason behind this is that the locate command searches inside a database that is kept by your system for all files that exist in the filesystem. This is what makes it very fast in returning results. At the same time, this is what prevents it from finding new files when they haven’t yet been indexed in the database.

Despite all that, Locate often comes in handy in situations where find would take a lot of time.

Using Locate is very simple. You can just type locate and then the name of the file that you want to find.

locate 

You can also add the -i option for case-insensitive searches.

You should now be capable of performing searches on a Linux system using find and locate.

Note that we haven’t covered all the possibilities that these commands offer, especially Find. I invite you to read the manual page to learn more about this command and about the features that it provides.

Quiz

1) How to search for a file 'needle' in the current directory? (+1pts)

2) How to search for files that have read, write and execute permissions for all users in the entire system? (+1pts)