find command in Linux with examples

In this guide, we’ll be learning how to use the find command from within a Linux terminal.

In my previous article, I had explained How to use grep command in Linux and I would request you to read that guide.

The find command is a powerful tool that allows us to quickly and easily scan through our file system to find files and directories that meet certain criteria and we can even perform actions on those results that we get back.

Now the reason I wanted to walk through how to use this tool is because sometimes I see people who write scripts that are more complex than they need to be whether that’s in Python or another language when really what they are trying to do can sometimes be done a lot more easily using these command line tools.

So, let’s go ahead and look at some examples.

So just like any of these commands if you look at the manual page by typing in man and then the name of the command in this case it’s find.

Now this will show us all of the options and different ways that we can use this command.

# man find

But we’re going to go over a lot of these here in this guide.

You must follow the syntax given below to use the find command.

find [options] [path...] [expression]

1. Find all the files and directories

So right now, I’m currently in a data directory where I’ve created some sample files and directories.

Now if I wanted to find all the files and directories starting in this current directory then I can just say find and then a dot(.) and that dot signifies the current directory.

# find .
.
./file1.txt
./file2.txt
./file3.txt
./file4.txt
./file5.txt
./data1
./data2
./data3
./data4
./data5

As you can see that we get all of the files and directories.

2. Find all files and directories within a specific directory

Here now if I wanted to find all the files and directories within a specific directory then I can just replace that dot(.) with a directory name.

For example, you can run the following command to find all files and directories within the data1 directory.

# find data1/
data1/
data1/myfile1.txt
data1/myfile2.txt
data1/myfile3.txt
data1/myfile4.txt
data1/myfile5.txt
data1/documents

As you can see that it returned all the directories and files within that data1 directory.

Okay so now let’s start filtering out some of the results.

3. Find only the directories

Let’s say that I wanted to find only the directories and exclude the files.

I’m going to do this in my current directory.

So, I’m going to say find within my current directory and I’m just going to say -type d and this will find all the directories and no files.

# find . -type d
.
./accounts
./accounts/files
./accounts/documents

So, there you can see that I got a result of all my directories.

You can use the following suffixes to specify the file type:

  • c – Character
  • d – directory
  • p – named pipe (FIFO)
  • f – regular file
  • l – symbolic link
  • s – socket
  • D – door (Solaris)

4. Find only the files

Now if I wanted to find all the files and no directories then I can just replace that d with an f.

Now if I run this then you’ll see that it returns all the files and no directories.

# find . -type f
./accounts/documents/data.txt
./accounts/file1.txt
./accounts/file2.txt
./accounts/file3.txt
./accounts/file4.txt
./accounts/file5.txt

And if you want to find only hidden files, then run the following command.

# find  . -type f name ".*"

5. Find and Delete Files

You can find and delete files by using the -delete option with the find command.

# find data/ -name "*.txt" -delete

6. Find a file with a specific name

Okay so now let’s say that I wanted to find a file with a specific name.

Now I know that there’s a file in my current directory called mydata1.txt but I can’t remember exactly where that is.

So, let’s go ahead and search within the current directory for this file and it will search in all the sub directories as well.

So, we want to do a find within the current directory which is that dot(.), a -type with an f which is going to find all the files and then I’m going to do an option of -name and that name was mydata1.txt.

# find . -type f -name "mydata1.txt"
./accounts/documents/mydata1.txt

So, you can see when I search for that exact filename that it returned this one result within this /accounts/documents/ directory.

7. Usage of Wildcard with find command

Okay but sometimes you don’t know the entire file name exactly.

What if I knew the file started with the word “my” but I couldn’t remember the rest of it.

So, if I wanted to find all the files that started with the word “my” and you can use an asterisk (*) as a wild card.

So, instead of searching for the specific file name here I’m just going to say “my” and then that asterisk (*) there to do a wild card.

Now if I run that then you can see that it returns all of these files that start with the word “my“.

# find . -type f -name "my*"
./accounts/documents/mydata1.txt
./mydata2.txt
./mydata3.txt

8. Case-insensitive file searching with find

Now there are some files that start with the word “My” that it didn’t return and that’s because some of them have capital letters and this -name option here is case sensitive.

Now if I want it to be case insensitive then instead of typing -name I can use -iname.

So, I’ll do an -iname search instead and if I run that now you can see that it gets files that start with “My” and it’s case insensitive.

It returns the ones that have capital letters in them also.

# find . -type f -iname "my*"
./accounts/documents/mydata1.txt
./accounts/documents/Mydata8.txt
./accounts/documents/Mydata9.txt
./accounts/documents/Mydata10.txt
./accounts/Mydata5.txt
./accounts/Mydata6.txt
./accounts/Mydata7.txt
./mydata2.txt
./mydata3.txt
./Mydata1.txt
./Mydata2.txt
./Mydata3.txt
./Mydata4.txt

9. Find files with certain extensions

Now that wildcard that I showed you sometimes ago, that is useful in order to find files with certain extensions.

So, let’s say that I wanted to find all Python files within my current directory.

For that I can run the following command.

# find . -type f -name "*.py"
./accounts/files/file1.py
./accounts/files/file3.py
./accounts/documents/file2.py
./accounts/documents/file4.py
./file5.py

So, you can see it did return some Python files within some of these subdirectories here and this star (*) just means anything and then this .py it’s saying return anything that ends in this .py.

You can find files of different extensions simultaneously.

The following command will search for all files that have extensions .txt and .png.

# find . -regex ".*\.\(txt\|png\)$"

10. Filter files and directories based on their metadata

Now we can also filter files and directories based on their metadata and that can be extremely useful.

10.1 Find all the files that modified 10 minutes ago

So, let’s say that we wanted to find all the files modified in the last 10 minutes.

To do that type the following command.

# find . -type f -mmin -10
./accounts/files/file1.py
./accounts/files/file3.py
./accounts/documents/file2.py
./accounts/documents/file4.py
./file5.py

So when you will use this minus(-) sign right here it’s saying to find files that were modified less than 10 minutes ago and we could use the plus(+) sign to find files that were modified more than 10 minutes ago.

10.2 Find files that were modified more than 10 minutes ago

I can do find files that were modified more than 10 minutes ago by running the following command.

So, if I run that you can see that it returns a lot more here because all those were modified more than 10 minutes ago.

# find . -type f -mmin +10
./accounts/documents/data.txt
./accounts/documents/mydata1.txt
./accounts/documents/Mydata8.txt
./accounts/documents/Mydata9.txt
./accounts/documents/Mydata10.txt
./accounts/file1.txt
./accounts/file2.txt
./accounts/file3.txt
./accounts/file4.txt
./accounts/file5.txt
./accounts/Mydata5.txt
./accounts/Mydata6.txt
./accounts/Mydata7.txt
./mydata2.txt
./mydata3.txt
./Mydata1.txt
./Mydata2.txt
./Mydata3.txt
./Mydata4.txt

10.3 Find last 1-5 minutes Modified Files

Now you can actually combine multiple of those time searches together.

So, let’s say that I wanted to find a file that was modified more than one minute ago but less than five minutes ago.

To do that type the following command.

# find . -type f -mmin +1 -mmin -5
./file1.txt
./file2.txt
./file3.txt
./file4.txt
./file5.txt

As you can see that it returned all that files as per our request.

10.4 Find all files that Modified 20 days ago

Now sometimes working with minutes isn’t exactly convenient because if you have something where it’s been days that you want to search then you don’t want to calculate up all the minutes for that.

If you wanted to see files that were last modified a certain number of days ago then instead of that -mmin option, we can use this -mtime option.

Following command will find all that files that were modified less than 20 days ago.

# find . -type f -mtime -20

10.5 Find all files that Modified more than 20 days ago

Following command will find all that files that were modified more than 20 days ago.

# find . -type f -mtime +20

I’ve been using -mmin and -mtime for modified minutes and modified days.

But you can also use -amin and -atime for access minutes and access days.

Let’s take an example of each:

10.6 Find all files that accessed 15 minutes ago

To find the all the files accessed less than 15 minutes ago use -amin option with the find command.

# find /etc/ -amin -15

10.7 Find all files that accessed two days ago

Following command will find all files accessed less than two days ago.

# find /etc/ -atime -2

You can also use -cmin and -ctime for changed minutes and change days.

10.8 Find all files that changed 15 minutes ago

Find files whose status was last changed less than 15 minutes ago.

# find /etc/ -cmin -15

10.9 Find all files that changed 2 Days ago

Find files whose status was last changed less than 2 days ago.

# find /etc/ -ctime -2

11. Find by file size

Okay, so another useful option is to be able to search by file size.

So, let’s say that we had some files eating up our disk space and we didn’t know exactly where those were.

Well we could run a search and try to find all the files over a certain size.

11.1 Find all the files that are over 5 Megabytes

So, let’s say that I wanted to find all the files that are over 5 megabytes.

In order to do that you can use the -size option with the find command.

# find . -size +5M
./myfile1.txt
./myfile2.txt

The following suffixes can be used specify the file size:

  • c – for bytes
  • w – for two-byte words
  • k – for Kibibytes
  • M – for Mebibytes
  • G – for Gibibytes

Just to prove that the files are over 5 Megabytes, I can use the ls command to check their size.

# ls -lah myfile*
-rw-r--r--. 1 root root 24M Mar 12 07:25 myfile1.txt
-rw-r--r--. 1 root root 10M Mar 12 07:25 myfile2.txt

Read Also: ls command in Linux with Examples

11.2 Some more examples related to finding files by size

I am going to run all these commands in my current working directory Which is denoted by a dot (.).

Example #1 Find all the files that are less than 8 Megabytes.

# find . -type f -size -8M
./myfile2.txt
./myfile3.txt
./myfile1.txt
./myfile5.txt

Example #2 Find all the files that are over 15 Megabytes.

# find . -type f -size +15M
./myfile4.txt
./myfile6.txt
./myfile7.txt

Example #3 Find all the files that are over 5 MB and less than 50 MB.

# find . -type f -size +5M -size -50M
./myfile3.txt
./myfile1.txt
./myfile4.txt
./myfile6.txt
./myfile7.txt

12. Find empty files and directories using find Command

Okay, so another common search that I like to perform is finding any files that I have created that are currently empty.

If you’ve created a bunch of test files that are just lingering around that don’t actually have any data.

12.1 Find empty Files

So, we can just find all of the files using -empty option with the find command.

# find . -empty 
./file1.txt
./file2.txt
./file3.txt
./file4.txt
./file5.txt

You can also find empty files using the following command.

# find . -type f -empty

Note: dot(.) signifies the current directory.

As you can see that a lot of these files are empty because I just created these to do this walkthrough.

12.2 Find empty Directories

Type the following command to find empty directories.

# find . -type d -empty 
./data1
./data2
./data3
./data4
./data5

13. Find based on permissions

Okay, so one of the last filters that we’re going to look at is how to search based on permissions.

And I do this a lot especially when working with websites and checking to make sure that certain permissions are what they should be.

We could do that using the -perm option with the find command.

13.1 Find all files and directories with 777 Permissions

Following command will find all files and directories that have permissions of 777.

# find . -perm 777
./file1.txt
./file2.txt
./file3.txt
./file4.txt
./file5.txt
./myfiles

You can use the ls command to check the permissions of a file and directory.

# ls -l
total 0
-rwxrwxrwx. 1 root root 0 Mar 12 06:52 file1.txt
-rwxrwxrwx. 1 root root 0 Mar 12 06:52 file2.txt
-rwxrwxrwx. 1 root root 0 Mar 12 06:52 file3.txt
-rwxrwxrwx. 1 root root 0 Mar 12 06:54 file4.txt
-rwxrwxrwx. 1 root root 0 Mar 12 06:54 file5.txt
drwxrwxrwx. 2 root root 6 Mar 12 07:50 myfiles

Okay so now that we’ve looked at several different ways that we can filter our results.

Let’s look at how we can actually perform some actions on our results.

So, this is where this becomes extremely useful.

Let’s say that we found a lot of permissions here within our data1 directory that we’re incorrect and that we wanted to change these.

So currently everything is set to 777 which gives anyone read write or execute permissions.

So, first we want to change the user and group for every directory and file in our folder and then we want to set all of the directories to have a permission level of 775 and all the files to have a permission level of 664.

Now that might sound like a lot of work and if you did this manually then it could take an extremely long time now this is sometimes where I see people who write overly complicated scripts to do something like this.

But we don’t have to write something like a Python script for this if we know how to use these terminal commands properly.

So, we’re going to do this everything that we want in three easy steps.

13.2 Set Owner and Group for file and directory

So, first of all we wanted to change the user and group for every file and directory within our data1 directory.

Remember find will return every file and directory by default.

If we run the following command, then this will return every file and directory within that folder.

# find data1
data1
data1/file1.txt
data1/file2.txt
data1/file3.txt
data1/file4.txt
data1/file5.txt
data1/myfiles

So now if I want to set the user and group on all those results then we could use -exec option in here to execute a command on those results.

Now the command that I want to execute is chown and that will change the owner of each result.

So now if I run this then you can see that it looks like not much happen, but we also didn’t get any errors.

# find data1/ -exec chown linuxadmin:developer {} +

So now if I do an ls -la on our data1 directory then you can see that our user and group were changed for all these files and folders.

# ls -la data1
-rwxrwxrwx.  1 linuxadmin developer    0 Mar 12 06:52 file1.txt
-rwxrwxrwx.  1 linuxadmin developer    0 Mar 12 06:52 file2.txt
-rwxrwxrwx.  1 linuxadmin developer    0 Mar 12 06:52 file3.txt
-rwxrwxrwx.  1 linuxadmin developer    0 Mar 12 06:54 file4.txt
-rwxrwxrwx.  1 linuxadmin developer    0 Mar 12 06:54 file5.txt
drwxrwxrwx.  2 linuxadmin developer    6 Mar 12 07:50 myfiles

So now let’s say that we wanted to set all the directory permissions to 775 and all the file permissions to 664.

And this time I’m not going to go as in-depth just so you can see how quickly we can run these types of commands.

13.3 Set permissions for all the directories

The following command will set 775 permissions for all the directories available in data1.

# find data1/ -type d -exec chmod 775 {} +

So, now if I wanted to see if that worked, I can type the following command.

# find data1/ -perm 775
data1/
data1/myfiles

13.4 Set permissions for all the files

The following command will set 664 permissions for all the files available in data1.

# find data1/ -type f -exec chmod 664 {} +

Okay so just like we did with our directories now let’s search for the permissions of 664 and make sure that all of those were set correctly.

# find data1/ -perm 664
data1/file1.txt
data1/file2.txt
data1/file3.txt
data1/file4.txt
data1/file5.txt

And you can see that when I run that, it returned all of our files underneath that data1 directory, so they all have those new permission levels of 664.

13.5 Find files by Owner

Let’s say that I wanted to find files by Owner. To do this you can user the -user option with the find command.

# find data/ -user linuxadmin
data/mydata2.txt
data/mydata3.txt
data/Mydata1.txt
data/Mydata2.txt
data/Mydata3.txt
data/Mydata4.txt

13.5 Find files by Group

Similarly let’s say that I wanted to find files by Group. To do this you can user the -group option with the find command.

# find . -group developer
./myfile2.txt
./myfile3.txt
./myfile1.txt
./myfile5.txt
./myfile4.txt
./myfile6.txt
./myfile7.txt

13.6 Find files with special permissions applied

You can also find files with special permissions (SUID, SGID) applied. Here are some examples:

The following command will find all files with the SUID permission applied.

# find . -perm /u=s

Similarly, following command will find all files with the SGID permission applied.

# find . -perm /g=s

14. Limiting search to a specific directory

Okay, so now let’s look at a slightly more complicated example.

Let’s say that I wanted to delete all the image files in my current directory that ended with a .png extension.

I’m going to do a find within my current directory to list all available .png files. To do this you can run the following command.

# find . -type f -name "*.png"
./myfiles/image7.png
./myfiles/image8.png
./myfiles/image9.png
./image1.png
./image2.png
./image3.png
./image4.png
./image5.png

Now see this is important here because if you’re doing something like the leading or modifying files then you should always run the find command to see what your results are before doing anything to those results.

Because you might not be removing the files or folders that you expect.

Running the find command first is like a dry run that allows you to see the results that you’ll be working with.

So you can see when I ran that command that our results aren’t what we wanted.

Because I only wanted to remove all the files in the current directory, I don’t want to delete anything in the subdirectories, and you can see here that find command returned all of the PNGs from our myfiles subdirectory as well.

Now this is a mistake that people make sometimes so it’s important to be careful.

To list all the .png files in the current directory issue the following command.

# find . -type f -name "*.png" -maxdepth 1

So, to remove all the .png files in the current directory issue the following command.

# find . -type f -name "*.png" -maxdepth 1 -exec rm {} +

Now if I do an ls within this current directory then it deleted all of those PNG images within the current directory.

# ls
file1.txt  file2.txt  file3.txt  file4.txt  file5.txt  myfiles

You can visit at following websites to get more information on find.

Conclusion

Like I said there’s a ton of different things that you can do with this find command if you just take the time to play around with it.

Now if you get good with commands like this then it’s going to save you a lot of time.

So, like I said instead of writing complicated scripts to do these exact same things you can just run a quick terminal command to do exactly what you want.

I hope you have learned something from this article and you may have found that find is a very important command in Linux.

I have tried my best to include all the features of find command in this guide.

Now I’d like to hear your thoughts.

Was this guide useful to you?

Or maybe you have some queries.

Have I not included any command in this guide?

Leave a comment below.

If you like our content, please consider buying us a coffee.

Buy Me A Coffee

We are thankful for your never ending support.

Leave a Comment