find Command in Linux: Search Files and Directories

By 

Updated on

9 min read

Using the find command to search for files and directories in Linux

When you need to locate a configuration file buried somewhere under /etc, clean up old logs by date, or change permissions on hundreds of files at once, the find command does the job. It walks a directory tree and matches files based on name, type, size, date, permissions, ownership, and more. It can also run a command on every match.

You can combine find with other tools such as grep , sed , or xargs for more complex operations.

This guide covers the most common find use cases with practical examples.

find Command Syntax

The general syntax for the find command is as follows:

txt
find [options] [path...] [expression]
  • The options attribute controls the treatment of the symbolic links, debugging options, and optimization method.
  • The path... attribute defines 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.

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

Here is an example that puts these parts together:

Terminal
find -L /var/www -name "*.js"
  • 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 "*.js" tells find to search files ending with .js (JavaScript files).

If you want to limit how deep find searches, use -maxdepth. For example, to search only the top level of /var/www:

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

Find Files by Name

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

For example, to search for a file named document.pdf in the /home/linuxize directory, you would use the following command:

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

To run a case-insensitive search, change 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 path, use -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 example, to find all files ending with .log.gz inside the /var/log/nginx directory, you would type:

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

You must either quote the pattern or escape the asterisk * with a backslash \ so that the shell does not interpret it as a glob.

To find all files that do not match a pattern, use the -not option. For example, to find all files that do not end in *.log.gz:

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 files, directories, or symlinks. In Linux, everything is a file.

To search for files based on their type, use 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 instance, to 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 Directories by Name

To search for directories instead of files, combine -type d with -name:

Terminal
find / -type d -name "config"

This returns every directory named config anywhere on the filesystem. To limit the search depth, add -maxdepth:

Terminal
find /etc -maxdepth 3 -type d -name "nginx"

The command above searches no more than three levels deep under /etc.

Find Files by Size

To find files based on the file size, pass 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 example, we 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 1MB, then 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 modification, access, or change time.

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

Suppose you modified one of the dovecot configuration files a few days ago but cannot remember which one. You can 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 example, to find all files with permissions of exactly 644 inside the /var/www/html directory, you 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 prefix, then at least one category (user, group, or 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 user, group, or others.

If minus - 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 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 group, use the -user and -group options.

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

Terminal
find / -user linuxize

For example, to find all files owned by the user www-data and change their ownership to nginx:

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

Find Files by Content

The find command locates files by their metadata (name, size, date), not by what is inside them. To search within file contents, combine find with grep :

Terminal
find /etc -type f -name "*.conf" -exec grep -l "listen" {} \;

This searches every .conf file under /etc and prints the path of each file that contains the word “listen.” The -l flag tells grep to print only the filename, not the matching line.

When you are searching file contents rather than file metadata, ripgrep is a faster alternative that combines recursive search, filename output, and ignore-file handling.

For better performance on large directory trees, pipe the results through xargs instead of -exec:

Terminal
find /var/log -type f -name "*.log" -print0 | xargs -0 grep -l "error"

The -print0 and -0 pair handles file names that contain spaces or special characters.

Find and Delete Files

To delete all matching files, append 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 spaces, use -print0 and xargs -0:

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

For example, to 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 first, the command will delete everything below the starting points you specified.

When it comes to directories, find can delete only empty directories, same as rmdir .

Quick Reference

For a printable quick reference, see the find cheatsheet .

CommandDescription
find /path -name "*.log"Find files by name
find /path -iname "readme*"Case-insensitive name search
find /path -type d -name "config"Find directories by name
find /path -type f -size +100MFind files larger than 100 MB
find /path -mtime -7Files modified in the last 7 days
find /path -perm 644Files with exact permissions
find /path -user johnFiles owned by a user
find /path -name "*.conf" -exec grep -l "term" {} \;Search file contents
find /path -name "*.tmp" -deleteDelete matching files
find /path -maxdepth 2 -type fLimit search depth

FAQ

What is the difference between find and locate?
find walks the directory tree in real time, so it always returns current results but can be slow on large filesystems. locate queries a pre-built database (updated by updatedb), which makes it much faster but may return stale results if the database has not been refreshed recently. Use find when accuracy matters and locate when speed is the priority.

How do I find empty files or directories?
Use the -empty option. To find empty files: find /path -type f -empty. To find empty directories: find /path -type d -empty. You can combine this with -delete to clean them up.

How do I find files modified in the last N minutes instead of days?
Use -mmin instead of -mtime. For example, find /var/log -type f -mmin -30 finds files modified in the last 30 minutes.

Can I combine multiple search conditions?
Yes. By default, find treats multiple conditions as AND. To use OR logic, add -o between conditions: find /path -name "*.log" -o -name "*.txt". Use parentheses (escaped as \( and \)) to group expressions.

Conclusion

The find command handles everything from simple name lookups to complex searches combining size, date, permissions, and content. For related tools, see the guide on finding large files and the xargs command for processing find output efficiently.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page