Finding Files on Linux using the find Command

In this guide, you will learn how to find files on Linux using the find command in the terminal.

find command on linux

The find command is an incredibly powerful and flexible tool that is crucial in administrating your Linux system.

You can use the find tool to search for files and directories within your system using a specified expression.

You can even search for files based on their specifies properties. For example, you can search for a file based on the permissions that have been set to it, or the owner, or file size.

Even more interesting, you can use find on Linux to execute actions on every file that your expression matches against.

This guide will walk you through some of the various ways to use the find tool on your Linux system.

Table of Contents

The Syntax of the find Command

Let us start by quickly going through the syntax of the find command on Linux.

find [OPTIONS] [PATH..] [EXPRESSION]

As you can see, the syntax for the tool is relatively simple. The main two things that you need to know are the path and expression.

The expression is one of the most important parts as it is how you control what the find tool finds within the specified directory.

One thing to note is that the order of your expression does matter. For example, if you wanted to execute a command, it should be located at the end of your expression. Otherwise, it will fire before a match event occurs.

Basic usage of find on Linux

The most basic usage for the find command is to only specify the directory for the tool to search through.

find DIRECTORY

When you use find and only specify the directory, it will list out every file and directory contained within it.

There aren’t many use cases for doing this as there are better alternatives for listing all files within a directory.

For example, the tree command can list out the files and folders while also giving you an idea of where they sit.

Example of Using Find on a Directory

To show you this behavior, let us show you what happens when you use the find command and only specify the directory.

For our example, if we wanted to search a directory located at “/home/pimylifeup/example” we would use the following command.

find /home/pimylifeup/example/

Using this, you will be greeted with an extensive list of all of the files and folders within this directory.

Filtering the File Type with Find

We can filter the type of file that is returned by the find command.

To specify the type of file to search for, you will need to utilize the “-type” option and a letter that designates the file type.

find DIRECTORY -type FILETYPE

You can use the list below to see the different file types you can specify and the letters representing that type.

  • f – Regular File
  • d – Directory
  • l – Symbolic Link
  • P – Named pipe (FIFO)
  • b – Block (buffered) special)
  • c – Character (unbuffered) special)
  • s – Socket
  • D – Door (Solaris)

From this list, the first two are the ones you are likely to use the most, those being searching for regular files (f) and directories (d).

Only Searching for Files with Find

For our first example, we will use the -type option to list the files (f) within our directory.

find /home/pimylifeup/example -type f

Using this on our example directory, it will return all of the files inside it.

Searching for Directories with Find

For our second example, let us try the same command as our last example, but instead of searching for files, we will search for directories.

This means that instead of using the “f”  letter alongside the “-type” option, we will be using the letter “d“.

find /home/pimylifeup/example -type d

This time instead of seeing all of the files located within the example directory, you will only see the directories.

Find Files on Linux by Name

Most people will use the find command on Linux to search for files by their name.

To do this, you will need to utilize the “-name” option alongside the find command to perform a case-sensitive search.

Using the name option, you can specify the name of the file you are searching for on your system.

Below you can see the basic syntax for using the find command to search for files based on their name.

find DIRECTORYTOSEARCH -name EXPRESSION

It is also possible to use find to perform a case-insensitive search. Instead of using “-name” you would use the “-iname” option instead.

Both use the exact same syntax. One option is case-sensitive (-name), and the other option is case-insensitive (-iname).

find DIRECTORYTOSEARCH -iname EXPRESSION

The find tool even has support for wildcards (*) so you can look for files containing part of a filename.

find DIRECTORYTOSEARCH -name *EXPRESSION*

Performing a Case-Sensitive Search with Find

For our first example, we will be performing a case-sensitive filename search for a file called “pimylifeup.png“.

As we are performing a case-sensitive search, we need to use the “-name” option.

find /home/pimylifeup/example -type f -name pimylifeup.png

By running this command, the find tool will search for our directories till it finds any files with the name “pimylifeup.png“.

If there is a file called “PiMyLifeUp.png” the find command will not match it as the cases do not match.

Performing a Case Insensitive Search with Find

Alternatively, if you don’t care if the filenames match precisely, it is possible to perform a case insensitive search with the find tool on Linux.

Instead of using the “-name” option, we will be using its alternative, “-iname“.

This time we will be searching for a file called “pImYlIFeuP.png“.

We used a mixed case as we know from our example directory that no file would have that exact casing.

find /home/pimylifeup/example -type f -iname pImYlIFeuP.png

The find command will still find a file called “pimylifeup.png” despite the filename we were searching for being “pImYlIFeuP.png“.

Using a Wildcard to Perform a Filename Search

The find tool on Linux allows you to use wildcards (*) within the filename. This is especially useful if you want to find files by their file extension.

For this example, let us find all files within our example that are a “.txt“. To perform this search, we will use the asterisk (*) followed by “.txt“.

find /home/pimylifeup/example -type f -name *.txt

From running this command with our sample data, you can see that it found and listed all files with the “.txt” extension.

Using Find to Search by Size

Another ability of the find tool on Linux is to search for files based on a specified size.

To specify a file size, you will need to use the “-size” option. It has a straightforward syntax and can be used alongside other options.

find DIRECTORY -size FILESIZE

This option is very simple to use and even has suffixes you can use, so you don’t have to specify the size just in bytes.

  • b – 512-byte Blocks (Default if nothing is specified)
  • c – Bytes
  • w – Two-byte Blocks
  • k – Kilobytes
  • M – Megabytes
  • G – Gigabytes

You can even search for sizes greater than (+), or less than (- your specified value by using the minus or positive symbols in front of your number.

Finding by a Specific Size

To use the find command on Linux to search for a file with a specific size, all you need to do is use the -size option followed by the file size.

For this example, we want to find a file with the size of 22 Megabytes. As we are using megabytes, we will be using the letter M suffix.

find /home/pimylifeup/example -size 22M

Find Files Greater Than Size

Using the plus symbol (+) as a prefix to your file size, you can tell the find tool to find files greater than the size.

For example, if we wanted to find all files larger than 13 Megabytes, we would use the size “+13M“.

find /home/pimylifeup/example -size +13M

Find Files Lesser Than Size

Likewise, there is also the functionality to check for files smaller than the specified size. To do this, you will need to utilize the minus symbol (-) as a prefix.

For example, if we want to use the find command to search for all files that are less than 13 Megabytes, we would down the size as “-13M“.

find /home/pimylifeup/example -size -13M

Find Files In Size Range

You can string multiple size parameters together so that you can find files within a certain size range.

For example, if we wanted to find all files between 12 Megabytes and 22 Megabytes, we need to use two size options.

The first option should be the smallest size we are looking for. So in our example, this will be set to “+12M“.

The second option needs to be the largest file size you are searching for. This means, for our example, we will set this option to “-22M“.

find /home/pimylifeup/example -size +12M -size -22M

Find Files by their Modification, Access, or Change Time

Another useful function of the Linux find command is to search for files based on when they were last modified, accessed, or changed.

In Linux, there are three different time stamps for every file. These are used to record the last time someone performed an operation on the file.

The option differs slightly depending on what operation you want to check for. Below we have listed out the three options and examples of how they are used.

  • -atime – This option lets you find files based on their last access time.

    A file is considered to be accessed whenever its contents have been read.

  • -mtime – By using this option, you can use find to search for files based on when they were last modified.

    Linux considers files to be modified once the contents of the file have been changed.

  • -ctime – We can use this final option to find files based on when the file’s attributes have been modified.

    The changed time should not be confused with the modification time. A file is considered changed when that file’s metadata has been modified.

    For example, when you change the file’s permissions, the changed timestamp will be updated.

As all of these options are used in the same way, we will be focusing on using find to search for files based on their modification date.

The basic syntax for these options is the following. The value for these options is the exact number of days since that file was modified, changed, or accessed.

find DIRECTORY -mtime DAYSSINCEMODIFIED

This option also supports the less than (-) and greater than (+) operators.

Using these operators, you can find files that were last modified later or earlier than the number of days you specified.

Finding Files Modified on Certain Day

The simplest way to use the modified options is to specify the exact number of days it has been since that operation occurred.

Of course, you will need to know how many days ago this action occurred for this to work.

For example, if we wanted to find a file that was modified 7 days ago, we will use the “-mtime” option alongside the number 7.

find /home/pimylifeup/example -mtime 7

After using this find command on your Linux system, it will search the directory you specified, looking for any file that has a modified date 7 days ago.

While simple to use, knowing exactly when a file has been operated on isn’t the most straightforward task. Luckily the find command allows us to have a bit more control.

Find File Modified After

To find files modified after the specified number of days, you will need to use the plus sign (+) prefix.

For example, if we wanted to use the find command to search for files modified later than five days ago, we would use “+5“.

find /home/pimylifeup/example -mtime +5

This command on Linux will search the specified folder, finding all files with a modified timestamp later than five days ago.

Find File Modified Before

Likewise, you can also find files that have been modified before a certain number of days by using the minus symbol (-).

To show you this, let us use this to find files created in the last 4 days. We need to use the number four, prefixed with the minus symbol like “-4“.

find /home/pimylifeup/example -mtime -4

Find File Modified in Day Range

With the find command on Linux, you can combine multiple options to find files modified between two separate dates.

For example, if we wanted to find a file that was created between 12 and 30 days ago, we can string these together.

For files created later than 12 days ago, we would need to use the option “-mtime +12“. If we only want files created before 30 days ago, we will use the option “-mtime -30“.

find /home/pimylifeup/example -mtime +12 -mtime -30

Find Files by Owner

Using the find command on your Linux system, you can specify what user or group owns the files you are looking for.

To search for files based on the user that owns it, you will need to use the “-user” option followed by the username.

find DIRECTORYNAME -user USERNAME

Finding a file based on the group that owns it is very similar. You need to use “-group” followed by the name of the group.

find DIRECTORYNAME -group GROUPNAME

Find Files Based on Set Permissions

The find command has the functionality to search for files that have particular permissions set to it.

There are numerous cases where this could be very useful, especially when it comes to ensuring someone hasn’t set the folder’s or file’s permissions to 777.

To find files based on their set permissions, you will need to use the “-perm” option, followed by the numerical permission.

find DIRECTORY -perm NUMERICALPERMISSION

To give you more fine-grained control, you have two operators that you can prefix to your permission.

The first is the forward-slash symbol (/). The slash symbol tells the find command that only one of the three permission bits must be set for a file to be matched.

find DIRECTORY -perm /NUMERICALPERMISSION

The second is the minus symbol (-). With the minus symbol set, then at least the permission bits specified must be set.

find DIRECTORY -perm -NUMERICALPERMISSION

For example, if you search for “-111“, all permission groups must have the execute permission for the file to be matched.

Execute Command on Found Files

One of the most powerful features of the find command on Linux is to execute a command on the found files.

To execute a command on every found file, we need to use the “-exec” option followed by the command you want to execute.

find DIRECTORY -exec COMMAND RESULTPLACEHOLDER DELIMITER

The exec option also requires

Results Placeholder

To pass in the full pathname to the command you are executing, you will need to use two curly brackets (“{}“).

The find command will automatically replace these braces with the pathname of the found file. You can use this placeholder as much as you want within a single command.

For example, if we were to run “chmod” on all files within our directory, we can use a command like we have below.

find /home/pimylifeup -type f -exec chmod 644 {} \;

From this example, you can see that we pass the full pathname of the found file by using the two brackets.

Delimiters

The end of the execute option must have a delimiter. The delimiter tells the find command where the -exec option ends and how it should handle the returns results.

The find command on Linux has two different types of delimiters that you can utilize, both with slightly different use cases.

The Semicolon Delimiter

The first is the semicolon (;). The find tool will execute the “-exec” command on each individual found file separately when using the semicolon.

This delimiter needs to be escaped with a forward slash (\), so the interpreter doesn’t misinterpret it.

For example, if we wanted to echo out the pathname for every found file, we would use a command like the following.

find /home/pimylifeup/example -type f -exec echo {} \;

You can see how this command works and how the echo command is fired individually for each found file.

/home/pimylifeup/example/subdir1/treeexample.png
/home/pimylifeup/example/subdir1/subsubdir2/tutorial.js
/home/pimylifeup/example/subdir1/subsubdir2/subdir3/raspberrypi.png
/home/pimylifeup/example/subdir1/subsubdir2/subdir3/helloworld3.cpp

The Plus Sign Delimiter

The alternate delimiter you can use with the find command on Linux is the plus symbol (+).

This symbol will tell the find command to join together all of the results from the find command before executing the command.

This means that the execute command will only be fired once and sent a list of the found files, each filename being separated by a space.

For example, let us try our echo command again, but this time using the plus symbol (+) delimiter.

find /home/pimylifeup/example -type f -exec echo {} +

You can see the difference in behavior between the two delimiters with the example results that we have below.

/home/pimylifeup/example/subdir1/treeexample.png /home/pimylifeup/example/subdir1/subsubdir2/tutorial.js /home/pimylifeup/example/subdir1/subsubdir2/subdir3/raspberrypi.png /home/pimylifeup/example/subdir1/subsubdir2/subdir3/helloworld3.cpp

Conclusion

Hopefully, you now have a solid understanding of how you can use the find command on your Linux system.

The find command is one of the most powerful tools in your arsenal when managing your device.

Not only can it be used to search your system for files, but it can also be used to run commands on every file it finds.

For example, you can use this as an easy way to fix up permissions for files and directories.

If you have had any issues with the find command, please leave a comment below.

Be sure to check out some of our other Linux tutorials to see what else you can do on your system.

Leave a Reply

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