Read in 6 minutes

last updated 

How to Find Files in Linux Using the Command Line

The Linux Find command is one of the most powerful tools in the Linux system administrators arsenal.

Find is a command-line utility which allows you to search for files and directories in a directory hierarchy based on a user given expression and applies a user specified action on each matched file.

You can use the find command to search for files based on the file permissions, type, date, ownership, size and more. It also can be used in combination with other tools such as grep or sed to perform operations on those files.

Linux Find Command Syntax

Before going into how to use the find command, let’s start by reviewing the basic syntax.

The find utility expressions take the following form:

find [options] [path...] [expression]
  • The options attribute controls the treatment of the symbolic links, debugging options and optimization method.
  • The path... attribute define the starting directory or directories where find will search the files.
  • The expression attribute is made up of options, search patterns and actions separated by operators.

Let’s take a look at the following command:

find  -L /home/projects/ -name "*.js" -exec chmod 644 {} \;

This command includes a parameter -L (options) which allows find to follow symbolic links, searches the entire directory tree beneath /home/projects/ (path…) for all files ending with .js (expression) and set the permissions of all matched files to 644.

Find Files by Type

To specify the type of the files you want to find use the -type parameter.

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

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

For instance, if you want to find all directories in the current directory just run:

find . -type d

Or if you want to list all character devices on your system type:

find / -type c

To change all file permissions to 644 and all directory permissions to 755 recursively run the following command:

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

Find Files by Name

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

To search for a file called document.pdf in the /home/linuxize directory you would use the following command:

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

If you want to do case-insensitive search then change the -name option with -iname:

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

The command above will match Document.pdf, DOCUMENT.pdf ..etc.

Find Files by Extension

Searching for files by extension is same as searching for files by name. For example if you want to find all files ending with .log.gz inside /var/log/nginx directory you can use:

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

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

If you want to find all files that don’t match the regex *.log.gz you can use the -not parameter, for example to find all files don’t end in *.log.gz you would use:

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

Find Files by Size

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

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

Let’s assume you are searching for all files of exactly 1024 bytes inside the ‘/tmp’ directory. You will have to run something like:

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 example we are searching for all files less than 1MB inside the current working directory, notice the minus - symbol before the size value:

find . -type f -size -1M

If you want to search for files with size greater than than 1MB, then you need to use the plus + symbol:

find . -type f -size +1M

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

find . -type f -size +1M -size 21M
Advertisement

Find Files by Modification Date

The find command can also search for files based on the last modification, access or change time.

Same as we did when searching by size, we can also use the plus and minus symbols to specify greater than or less than.

Let’s say that a few days ago, you modified a dovecot configuration file but you forgot the file name. You can easily filter all files in the /etc/dovecot/conf.d/ directory that end with .conf and have been modified in the last 5 days with:

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

Here is an another example of filtering files based on the modification date using the -daystart option. List all of the files in the /home directory that were modified one month or longer ago:

find /home -mtime +30 -daystart

Find Files by Permissions

If you want to use the find command to filter files based on the file permissions then you need to use the -perm parameter.

To find all files with permissions of exactly 775 inside the /var/www/html directory you would use:

find /var/www/html -perm 644

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

If a / is used as the prefix, then at least one category (user, group or others) must to have at least the respective bits set for a file to match. Consider the following example command:

find . -perm /444

The above command will match all files with read permissions set for either user, group or others.

If a - is used as the prefix then for the file to match at least the specified bits must be set.

The following command will search for files which have read and permission for their owner, but which the group and other users can read but not write to:

find . -perm -664

Find Files by Owner

To find files owned by a particular user or group you can use the -user and -group options.

For example, to search for all files and directories owned by the user linuxize, just run:

find / -user linuxize

Here is more advanced 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:

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

Find and Delete Files

To delete all matching files add the parameter -delete to the end of the match expression.

Make sure you are using this option only when you are certain that the results only match the files that you want to delete. It is always a good idea to print the files before using the -delete option.

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

find /var/log/ -name `*.temp` -delete

Conclusion

In this tutorial, you learned how to use the Linux find utility based on various criteria and now you have 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 question or remark, please leave a comment below.