Sep 18, 2023 6 min read

How to Use Find Command in Linux

Use Find Command in Linux with our step-by-step tutorial. The find command is a powerful tool for Linux system administrators.

Use Find Command in Linux
Table of Contents

Before we begin talking about how to use find Command in Linux, let's briefly understand - What is a Find Command?

One of the most powerful tools in a Linux system administrator's armory is the find command. It searches a directory structure for files and directories matching a user-specified expression and can conduct user-specified actions on each matched file.

The find command can be used to look for files and directories based on their permissions, type, date, ownership, size, and other factors. It can also be used in conjunction with other tools like grep or sed.

In this tutorial, you will use find Command in Linux. We will also address a few FAQs on how to use find Command in Linux.

find Command Syntax

Below is the syntax for find command:

find [options] [path...] [expression]
  • The symbolic link treatment, debugging options, and optimization strategy are all controlled by the options property.
  • The path... property specifies the directory or directories from which find will look for files.
  • Options, search patterns, and actions are separated by operators in the expression attribute.

The user issuing the find command must have read permissions on the directory in order to look for files within it.

Take a look at the following scenario:

find -L /var/www -name "*.js"
  • The -L (options) instructs find command to follow symbolic links.
  • The /var/www (path...) indicates the directory to search.
  • The (expression) -name "*.js" instructs find to look for files that end in .js (JavaScript files).

Find Files by Name

The most typical usage of the find command is to locate files by name. Use the -name option followed by the name of the file you're looking for to find it by name.

For example, you could use the following command to look for a file named document.pdf in the /home/vegastack directory:

find /home/vegastack -type f -name document.pdf

Change the -name option to -iname, to run a case-insensitive search.

find /home/vegastack -type f -iname test.pdf

The command above will match "test.pdf", "TEST.pdf" .. etc.

Find File by Extension

The process of looking for files by extension is similar to looking for files by name. For example, to discover all files in the /var/log/nginx directory that ends in .log.gz, type:

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

It's worth noting that you must either quote the pattern or escape the asterisk * sign with a backslash \ when using the wildcard character so that the shell doesn't interpret it.

The -not option can be used to find all files that do not match the regex *.log.gz. To discover all files that don't end in *.log.gz, for example, type:

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

Find Files by Type

You may need to look for specific file types, such as normal files, directories, or symlinks, from time to time. Everything is a file in the Linux operating system.

Use the -type option and one of the following descriptors to define the file type while searching for files:

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

To locate all folders in the current working directory, for example, type:

find . -type d

Using the chmod command, you may recursively update the website file permissions to 644 and directory permissions to 755:

Find Files by Size

Pass the -size argument along with the size requirement to search files depending on their size. The following suffixes can be used to define the file size:

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

The following command will discover all files in the /tmp directory that are precisely 1024 bytes long:

find /tmp -type f -size 1024c

You may also use the find command to look for files that are larger or smaller than a certain size.

In the example below, we'll look for all files in the current working directory that are less than 1MB in size. Keep in mind that the size value is preceded by a minus - symbol:

find . -type f -size -1M

If you want to search for files larger than 1MB, use the plus + symbol:

find . -type f -size +1M

You can even look for files in a specific size range. The following command will find all files between 5 and 3MB:

find . -type f -size +5M -size 3M

Find Files by Modification Date

The find command can also be used to look for files based on when they were last modified, accessed, or changed.

Use the plus and minus characters for "more than" and "less than," just as you would when searching by size.

Let's imagine you changed one of the dovecot configurations files a few days ago, but can't remember which one. You may quickly filter all files ending in .conf that have been edited in the last five days under the /etc/dovecot/conf.d directory:

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

Here's another example of using the -daystart option to filter files depending on their modification date. The following command will list any file in the /home directory that has been edited in the last 30 days:

find /home -mtime +30 -daystart

Find Files by Permission

You can use the -perm option to search for files based on their permissions.

To discover all files in the /var/www/html directory with permissions of exactly 644, for example, type:

find /var/www/html -perm 644

The numeric mode can be prefixed with minus - or slash /.

For a file to match when the prefix is a slash /, at least one category (user, group, or others) must have at least the relevant bits set.

Check the below command as an example:

find . -perm /444

All files having read rights set for either user, group, or others will be found with the above command.

If negative - is used as the prefix, at least the given bits must be set for the file to match. The following command will look for files that are readable by other users and have read and write permission for the owner and group:

find . -perm -664

Find Files by Owner

Use the -user and -group parameters to find files owned by a certain user or group.

For instance, to find all files and directories owned by the user vegastack, type:

find / -user vegastack

Here's an example from real life. Let's imagine you want to find all files held by the user www-data and change www-ownership data's to nginx:

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

Find and Delete Files

Add the -delete option to the end of the match expression to remove all matching files.

Use this option only if you are certain that the result corresponds to the files you want to remove. Before using the -delete option, it's a good idea to print the matched files.

To delete all files ending in .temp from /var/log/, for example, you would use:

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

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

FAQs to Use Find Command in Linux

Can I search for files based on their names using the "find" command? 

Yes, you can search for files by specifying their exact name or use wildcards such as "*" or "?" for partial matches.

Is it possible to search for files with specific extensions using the "find" command? 

Yes, you can filter files by their extensions using the "-name" option, such as "find /path -name "*.txt"" to find all files with a .txt extension.

Can I search for files based on their size using the "find" command? 

Absolutely, you can search for files based on size using the "-size" option with modifiers like "+", "-", or "c" (for bytes).

How do I search for files modified within a specific time frame using the "find" command? 

You can search for files modified within a time range using the "-mtime" or "-mmin" options along with the desired time parameter.

Can the "find" command be used to search for empty directories? 

Yes, you can search for empty directories using the "-empty" option with the "find" command.

How can I combine multiple conditions while using the "find" command? 

You can combine conditions using logical operators such as "-and", "-or", and "-not" to refine your search based on specific criteria.

Is it possible to execute commands on the files found by the "find" command? 

Yes, you can use the "-exec" option with the "find" command to execute commands on the files found, such as deleting or moving them.

Conclusion

We hope this detailed guide helped you understand how to use the find command.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.