16 Practical Examples of Linux Find Command for Beginners

Find is one of the most frequently used Linux commands, and it offers a plethora of features in the form of command line options. In this tutorial, which is aimed at beginners, we will discuss the basic usage of the find command as well as some of the useful command line options it offers.

NOTE: Unless otherwise specified, we will be using the following files for all our find command-related examples in this tutorial.

find example files

1. How to list all files in current directory and its subdirectories

The find command lets you quickly list all the files in current directory and its subdirectories. For this, all you have to do is to run the command without any arguments or options.

find

Here is the output in our case:

list all files in current directory and its subdirectories

Of course, you'll have to provide the complete path if the directory whose contents you want to list is not your current directory.

2. How to search file by name

If you want, you can use the find command to search for a specific file by its name. The -name command line option lets you do this. Here's the syntax:

find [dir-path] -name [filename]

For example, the following command will search the current directory for a file named 'testfile1.txt.'

find . -name testfile1.txt

Here is the output

search file by name

Similarly, you can search for the file in another directory. If the directory is a subdirectory of your present working directory, then you don't have to do anything as the find command automatically searches in all sub-directories.

But if it's a separate directory altogether, then you'll have to provide the complete path to it. For example, the following command will search for the file in user's home directory:

find /home -name testfile1.txt

3. How to search for files of particular type

The find command also lets you search for same type of files in a directory (and its subdirectories). For example, the following command will search for all .txt files in your present working directory.

find . -name "*.txt"

search for files of particular type

In case you aren't aware, * is a wildcard character. For more information on wild card characters, head here.

By default, the find command performs a case sensitive search (it treats upper and lower case characters as different). But if you want, you can force the command to carry out case-insensitive search. This you can do by using the -iname command line option.

find -iname [filename]

For example,

find -iname testfile1.txt

Here is the output

perform case insensitive search

5. How to only display names that don’t match search pattern

If you want, you can even ask the find command to print the file names that do not match the search pattern (also known as invert search). You can access this feature using the ! or -not operator.

For example

find . -not -name “*.txt”

Here is the output

display names that don’t match search pattern

So as you can see, all files with extension other than .txt are produced in the output.

6. How to limit search to directory level(s)

The find command also allows you to limit search to a particular directory depth. One command line option that lets you do this is -maxdepth.

For example, consider the following directory structure:

limit search to directory level(s)

Now suppose, if you want find to search only up till the 'find' subdirectory (meaning it should ignore the 'howtoforge' sub-directory), then you can use the following command:

find . -maxdepth 3 -name "*.txt"

Here '-maxdepth 3' forces 'find' to go inside and search only three levels, with the first level being your top level (or the current working) directory.

Here's the output of the command:

limit search to directory level(s) output

Like maxdepth, there is another option called mindepth (usage: '-mindepth [N]'). When used, this option forces the find command to go 'N' level down before starting the search operation

For example,

find . -mindepth 3 -name "*.txt"

limit search to 3 directory levels

So, only directories 'find' and below are searched.

On similar lines, if you want to search a .txt file in subdirectories that fall between level 2 and 4, then you can use the following command.

find -mindepth 2 -maxdepth 4 -name "*.txt"

7. Display all the empty files

If you want, you can use the find command to display all the empty files in a particular directory (and its subdirectories). This can be done using the -empty option.

For example, to display all the empty files in your current working directory, use the following command:

$ find . -empty

8. How to search files belonging to particular group

The find command also lets you search for files belonging to a particular group - the -group option lets you do this. For example, the following command will list all the files – present in current working directory and its subdirectories - that belong to the 'howtoforge' group.

find . -group howtoforge -name "*.txt"

Here's the output of the above command in my case:

search files belonging to particular group

9. How to search files owned by a particular user

The find command also lets you search for files based on ownership - the -user option lets you do this. For example, the following command will display all .txt files (present in the current directory) owned by user 'himanshu':

find . -user himanshu -name "*.txt"

10. How to search recently modified files

The find command allows you to search recently-modified files. This can be done using the -mmin option. The option requires you to pass a number which will be treated as number of minutes.

So for example, if you want to search for .txt files (in your current directory) whose data was modified 1 minute ago, you can use the following command:

find . -mmin 1 -name "*.txt"

11. How to search for files that were modified more recently than a file

Yes, the find command even lets you search for files that were modified more recently that a particular file. This feature can be accessed using the -newer option that requires a file name (against which you want to compare) to be passed to it.

Here's an example:

find . -newer ./examples/find/howtoforge/old.txt -name "*.txt"

search for files that were modified more recently than a file

12. How to display only directory names in output

There's also a command line option that enables the find command to only display directory names in the output. The option in question is -type and you need to pass d as a value to it.

For example:

find -type d

Here's the output of the above command in our case:

display only directory names in output

Aside from d, there are several other letters that can be passed as value to -type option. To know about them, head to the find command's man page.

13. How to search files based on their inode numbers

You can also pass an inode number to the find command and ask it to find the corresponding file name (if any). This feature can be accessed through the -inum option which requires an inode number as a value.

Following is an example:

search files based on their inode numbers

Tip: You can find a file's inode number using the 'ls -li' command.

14. How to search files based on their last access time

The find command also lets you search for files based on when they were last accessed – you can ask the tool to display file(s) that were last accessed 'n' minutes ago. This feature can be accessed using the -amin command line option.

For example, the following command searches for .txt files in present directory that were accessed 1 min ago:

find -amin 1 -name "*.txt"

search files based on their last access time

15. using regular expressions (regex) with find

The find command supports auhc the use of regular expressions. These can be specified with the -regex command line option.

find ./ -type f -regex '\.\/test[1-2]\.txt'


This regex finds all files that start with the word test, then contain the numbers between 1 and 2, and then end with .txt. It is important to consider in the regular expression that "./" is a part of the file and not only the file name.

Linux find regex

16. search files by size with find command

With the -size command line option you can search files by size. For example, if you want to find files larger than 100 MB, use the following command:

find ./ -type f -size +100M

You can use the following size units:

K = kilobyte
M = megabyte
G = gigabyte

So you can easily find large files in Linux.

Conclusion

The examples mentioned in this tutorial should give you a good idea about how the find command works on a basic level, as well as about some of the useful command line options it offers. Do try these examples on your system, and also go through the tool's man page.

Share this page:

4 Comment(s)