Find Command in Linux (Find Files and Directories)

Updated on

7 min read

Find Files in Linux Using the Command Line

The find command is one of the most powerful tools in the Linux system administrators’ arsenal. It searches for files and directories in a directory hierarchy based on a user-given expression and can perform a user-specified action on each matched file.

You can use the find command to search for files and directories based on their permissionstypedateownershipsizeand more. It can also be combined with other tools such as grep or sed .

find Command Syntax

The general syntax for the find command is as follows:

sh
find [options] [path...] [expression]
  • The options attribute controls the treatment of the symbolic linksdebugging optionsand optimization method.
  • The path... attribute defines the starting directory or directories where find will search the files.
  • The expression attribute is made up of optionssearch patternsand actions separated by operators.

To search for files in a directorythe user invoking the find command needs to have read permissions on that directory.

Let’s take a look at the following example:

Terminal
find -L /var/www -name "*."
  • The option -L (options) tells the find command to follow symbolic links.
  • The /var/www (path…) specifies the directory that will be searched.
  • The expression -name "*." tells find to search files ending with . (JavaScript files).

If you want to limit how deep find searchesuse -maxdepth. For exampleto search only the top level of /var/www:

Terminal
find /var/www -maxdepth 1 -type f -name "*."

Find Files by Name

Finding files by name is probably the most common use of the find command. To find a file by its nameuse the -name option followed by the name of the file you are searching for.

For exampleto search for a file named document.pdf in the /home/linuxize directoryyou would use the following command:

Terminal
find /home/linuxize -type f -name document.pdf

To run a case-insensitive searchchange the -name option with -iname:

Terminal
find /home/linuxize -type f -iname document.pdf

The command above will match “Document.pdf”“DOCUMENT.pdf”etc.

To match part of the pathuse -path:

Terminal
find /var/www -type f -path "*/cache/*"

Find Files by Extension

Searching for files by extension is the same as searching for files by name. For exampleto find all files ending with .log.gz inside the /var/log/nginx directoryyou would type:

Terminal
find /var/log/nginx -type f -name '*.log.gz'

It is important to mention that you must either quote the pattern or escape the asterisk * symbol with backslash \ so that it doesn’t get interpreted by the shell when you use the wildcard character.

To find all files that don’t match the regex *.log.gz you can use the -not option. For exampleto find all files that don’t end in *.log.gz you would use:

Terminal
find /var/log/nginx -type f -not -name '*.log.gz'

Find Files by Type

Sometimes you might need to search for specific file types such as regular filesdirectoriesor symlinks. In Linuxeverything is a file.

To search for files based on their typeuse the -type option and one of the following descriptors to specify the file type:

  • f: a regular file
  • d: directory
  • l: symbolic link
  • c: character devices
  • b: block devices
  • p: named pipe (FIFO)
  • s: socket

For instanceto find all directories in the current working directory you would use:

Terminal
find . -type d

A common example is to recursively change the website file permissions to 644 and directory permissions to 755 using the chmod command:

Terminal
find /var/www/my_website -type d -exec chmod 0755 {} \;
find /var/www/my_website -type f -exec chmod 0644 {} \;

Find Files by Size

To find files based on the file sizepass the -size parameter along with the size criteria. You can use the following suffixes to specify the file size:

  • b: 512-byte blocks (default)
  • c: bytes
  • w: two-byte words
  • k: Kilobytes
  • M: Megabytes
  • G: Gigabytes

The following command will find all files of exactly 1024 bytes inside the /tmp directory:

Terminal
find /tmp -type f -size 1024c

The find command also allows you to search for files that are greater or less than a specified size.

In the following examplewe search for all files less than 1MB inside the current working directory. Notice the minus - symbol before the size value:

Terminal
find . -type f -size -1M

If you want to search for files with a size greater than 1MBthen you need to use the plus + symbol:

Terminal
find . -type f -size +1M

You can even search for files within a size range. The following command will find all files between 1 and 2MB:

Terminal
find . -type f -size +1M -size -2M

Find Files by Modification Date

The find command can also search for files based on their last modificationaccessor change time.

Same as with sizeuse the plus and minus symbols for “greater than” or “less than”.

Let’s say that a few days agoyou modified one of the dovecot configuration filesbut you forgot which one. You can easily filter all files under the /etc/dovecot/conf.d directory that end with .conf and have been modified in the last five days:

Terminal
find /etc/dovecot/conf.d -name "*.conf" -mtime -5

Here is another example of filtering files based on the modification date using the -daystart option. The command below will list all files in the /home directory that were modified 30 or more days ago:

Terminal
find /home -mtime +30 -daystart

Find Files by Permissions

The -perm option allows you to search for files based on the file permissions.

For exampleto find all files with permissions of exactly 644 inside the /var/www/html directoryyou would use:

Terminal
find /var/www/html -perm 644

You can prefix the numeric mode with minus - or slash /.

When slash / is used as the prefixthen at least one category (usergroupor others) must have at least the respective bits set for a file to match.

Consider the following example command:

Terminal
find . -perm /444

The above command will match all the files with read permissions set for either usergroupor others.

If minus - is used as the prefixthen for the file to matchat least the specified bits must be set. The following command will search for files that have read and write permission for the owner and group and are readable by other users:

Terminal
find . -perm -664

Find Files by Owner

To find files owned by a particular user or groupuse the -user and -group options.

For exampleto search for all files and directories owned by the user linuxizeyou would run:

Terminal
find / -user linuxize

Here is a real-world example. Let’s say you want to find all files owned by the user www-data and change the ownership of the matched files from www-data to nginx:

Terminal
find / -user www-data -type f  -exec chown nginx {} \;

Find and Delete Files

To delete all matching filesappend the -delete option to the end of the match expression.

Ensure you are using this option only when you are confident that the result matches the files you want to delete. It is always a good idea to print the matched files before using the -delete option.

When you need to handle file names with spacesuse -print0 and xargs -0:

Terminal
find . -type f -name "*.log" -print0 | xargs -0 rm -f

For exampleto delete all files ending with .temp from the /var/log/you would use:

Terminal
find /var/log/ -name '*.temp' -delete
Warning
Use the -delete option with extreme caution. The find command is evaluated as an expression and if you add the -delete option firstthe command will delete everything below the starting points you specified.

When it comes to directoriesfind can delete only empty directoriessame as rmdir .

Conclusion

We have shown you how to use the find command with various options and criteria.

This article should give you a fundamental understanding of how to locate files on your Linux systems. You may also visit the find man page and read about all other powerful options of the find command.

If you have any questions or remarksplease leave a comment below.