Guide to the Linux find Command


Introduction

Linux is powerful operating system it provides a variety of command-line tools to managing files or directories. One of most useful tool is find command, it allows users to search any files and directories based on a wide range of search criteria.

In this article, you will get a comprehensive guide to using find command, including a variety of examples that demonstrate its capabilities.

Basic Usage

The basic syntax of find command is as follows −

find [path] [expression]

The path argument specifies starting directory for search, and expression argument specifies criteria for search. Here are some common options that can be used in expression argument −

  • -name − search for files with a specific name

  • -type − search for files of a specific type (e.g., regular file, directory, symbolic link)

  • -size − search for files of a specific size (in bytes, blocks, or other units)

  • -mtime − search for files that were last modified a specific number of days ago

  • -user − search for files owned by a specific user

  • -group − search for files owned by a specific group

  • -perm − search for files with specific permissions

Let's look at some examples to see how these options can be used.

Example 1

Find all files in a directory To find all files in a directory and its subdirectories, you can use following command −

find /path/to/directory -type f

This will list all regular files in specified directory and its subdirectories.

Example 2

Find all directories in a directory To find all directories in a directory and its subdirectories, you can use following command −

find /path/to/directory -type d

This will list all directories in specified directory and its subdirectories.

Example 3

Find files with a specific name To find all files with a specific name, you can use following command −

find /path/to/directory -name "filename.txt"

This will search for all files named "filename.txt" in specified directory and its subdirectories.

Example 4

Find files based on size To find files based on their size, you can use following command −

find /path/to/directory -size +1M

This will search for all files in specified directory and its subdirectories that are larger than 1 megabyte.

Example 5

Find files based on modification time To find files based on their modification time, you can use following command −

find /path/to/directory -mtime -7

This will search for all files in specified directory and its subdirectories that were modified within last 7 days.

Example 6

Find files based on ownership To find files based on their ownership, you can use following command −

find /path/to/directory -user username

This will search for all files in specified directory and its subdirectories that are owned by specified user.

Example 7

Find files based on permissions To find files based on their permissions, you can use following command −

find /path/to/directory -perm 644

This will search for all files in specified directory and its subdirectories that have specified permissions (in this case, read and write permissions for owner, and read-only permissions for everyone else).

Combining Options

In many cases, you may want to combine multiple options to create more complex search criteria. For example, you might want to find all files in a directory that were modified within last 7 days and are larger than 1 megabyte. To do this, you can use following command −

find /path/to/directory -type f -mtime -7 -size +1M

This will search for all regular files in specified directory and its subdirectories that were modified within last 7 days and are larger than 1 megabyte.

You can also use logical operators (AND, OR, NOT) to combine options. For example, if you want to find all files that are either owned by user "username" or have permission 644, you can use following command −

find /path/to/directory \( -user username -o -perm 644 \)

Note that parentheses and backslashes are necessary to properly group options.

Using -exec option

The find command also provides an -exec option that allows you to perform actions on files that are found. For example, you might want to delete all files in a directory that are larger than 1 megabyte. To do this, you can use following command −

find /path/to/directory -type f -size +1M -exec rm {} \;

This will find all regular files in specified directory and its subdirectories that are larger than 1 megabyte, and then execute rm command on each of them.

Note that {} symbol is a placeholder for name of each file that is found, and \; symbol indicates end of command that should be executed.

Using -iname option

By default, find command is case-sensitive when searching for filenames. However, you can use -iname option instead of -name to perform a case-insensitive search. For example, if you want to find all files in a directory that have extension .txt regardless of case, you can use following command −

find /path/to/directory -type f -iname "*.txt"

This will find all regular files in specified directory and its subdirectories that have extension .txt regardless of case.

Using -delete option

If you want to delete files that are found by find command without using -exec option, you can use -delete option. For example, if you want to delete all files in a directory that are smaller than 1 kilobyte, you can use following command −

find /path/to/directory -type f -size -1k -delete

This will find all regular files in specified directory and its subdirectories that are smaller than 1 kilobyte, and then delete them.

Note that -delete option is equivalent to using -exec rm {} \; option.

Using -maxdepth option

By default, find command searches for files and directories in specified directory and all of its subdirectories. However, you can use -maxdepth option to limit depth of search. For example, if you want to find all files in a directory but only search in top-level directory (not its subdirectories), you can use following command −

find /path/to/directory -maxdepth 1 -type f

This will find all regular files in specified directory, but will not search in its subdirectories.

Conclusion

The find command is a powerful and versatile tool for searching for files and directories in Linux. By combining various options and logical operators, you can create complex search criteria that meet your specific needs. Additionally, -exec option allows you to perform actions on files that are found, making find command even more useful. With examples provided in this guide, you should be able to start using find command to manage your files and directories more efficiently.

Updated on: 03-Mar-2023

183 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements