How to Use Find Command in Linux [12 Best Uses]

Written by

Reviewed by

Last updated: July 2, 2024

Expert verified

SVG Image

TL;DR

To learn to use the find command in Linux, you can try these methods:

  1. Locating Files by Name: Use the Find command with the -name option.
  2. Finding Files by File Type: Utilize the Find command with the -type option.
  3. Searching for Files by Size: Employ the Find command with the -size option.
  4. Filtering Files by Time: Apply the Find command with time-based predicates.

Explore the guide below to learn to use the find command in Linux, advanced techniques, and common errors that can occur when using the command.

Finding files on your Linux system doesn’t have to be a hassle. Whether you’re new to Linux or a seasoned pro, the find command can simplify this task. This powerful tool lets you search for files based on criteria like name, type, size, and permissions. In this post, I’ll guide you through the basics and some advanced techniques of using the find command. You’ll also learn best practices to optimize your searches, helping you manage your files more efficiently. Let’s dive in and make file management simpler!

What is the Find Command?

The find command in Linux helps you locate files and directories. You can search based on different criteria like name, type, size, permissions, and more. It’s a powerful tool for managing files, especially when dealing with large file systems or complex directory structures.

Importance in System Administration

For system administrators, the find command is essential. It allows them to:

  1. Locate Files Quickly: Easily find files scattered across different directories.
  2. Manage Permissions: Identify files with specific permissions to maintain security.
  3. Perform Maintenance: Clean up old or unnecessary files, freeing up disk space.
  4. Audit Systems: Check for files owned by specific users or groups, ensuring compliance with policies.
  5. Automate Tasks: Combine find with other commands to automate repetitive tasks like backups or log file management.

How to Use Find Command in Linux

To use the find command in Linux, open your terminal and enter find followed by the directory path and search criteria. For example, find /home -name "*.txt" searches for all .txt files in the /home directory. You can also search by file type with -type, size with -size, and permissions with -perm. Use -exec to run commands on found files. Combining multiple criteria with -and or -or refines your search. This makes the find command a versatile tool for locating files based on various conditions.

That was the quick answer. Here are the detailed steps for 12 different methods to use find command in Linux:

1. Locating Files by Name

Searching for files based on their names is a common and powerful use case for the Find command. By using wildcards, you can match patterns and narrow down your search results efficiently. Here is how to do it:

  1. Open your terminal.
opening terminal 22
  1. Use the following command to find files by name:
find starting_directory -name "filename_pattern"

Replace starting_directory with the directory where you want to start the search and filename_pattern with the pattern or name of the file you are looking for.

  1. For example, to find all files with the .txt extension in the /home/guestuser directory, run the command:
find /home/guestuser -name "*.txt"
  1. The command will display a list of files matching the specified name or pattern.
listing all files with specific pattern

2. Finding Files by File Type

The Find command enables you to search for files based on their types, such as text files, directories, or executables. This feature helps you categorize and work with specific file types more efficiently. Follow these steps:

  1. Open your command prompt and run the following command:
find starting_directory -type file_type

Replace starting_directory with the directory where you want to start the search and file_type with the specific file type you are looking for (e.g., f for regular files, d for directories, x for executables).

  1. For example, to find all regular files in the /var/log directory, run the command:
find /var/log -type f
  1. The command will display a list of regular files in the specified directory.
listing regular files in a specific directory

3. Searching for Files by Size

The Find command in Linux enables you to search for files based on their sizes. Searching for files by size allows you to identify large files that may be occupying significant disk space or locate empty files that can be removed. To search files by size follow these steps:

  1. Launch your Terminal and execute the following command:
find starting_directory -size +/-sizeunit

Replace starting_directory with the directory where you want to start the search. Use + to find files larger than the specified size, andto find files smaller than the specified size. size represents the numerical value of the size, and unit represents the size unit (e.g., K for kilobytes, M for megabytes, G for gigabytes).

  1. For example, to find files larger than 1 megabyte in the /home/guestuser/Downloads directory, run the command:
find /home/guestuser/Downloads -size +1M
  1. The command will display a list of files larger than 1 megabyte.
displaying files with a specific size

4. Filtering Files by Time

Filtering files by time allows you to focus on recent or older files, helping you track changes, perform maintenance tasks, or identify outdated information. This method enhances file management and assists in maintaining data integrity and organization. Follow these steps:

  1. Access your command prompt and enter the following command to filter files by time:
find starting_directory -time_predicate time_value

Replace starting_directory with the directory where you want to start the search. time_predicate represents the time attribute you want to filter (e.g., mtime for modification time, ctime for creation time, atime for access time), and time_value represents the time interval or value (e.g., -7 for the last 7 days).

  1. For example, to find files in the /var/log directory that have been modified within the last 7 days, run the command:
find /var/log -mtime -7
  1. The command will display a list of files modified within the specified time interval.
finding files that have been modified within a specific time period

5. Finding Files by Permission

Finding files by permission is useful for system administrators who need to identify files with specific access rights, ensuring security policies are correctly applied.

  1. Start by opening your terminal.
  2. Now run this command to find files by permission:
find /path/to/search -perm permission_value

Replace /path/to/search with the directory path you want to search in, and permission_value with the desired permission (e.g., 755).

finding files by permission

6. Finding Files by Owner

This method helps locate files owned by a specific user, which is useful for managing user data and performing security audits.

  1. Open your terminal and run the following command:
find /path/to/search -user username

Replace /path/to/search with the directory path and username with the name of the user whose files you want to find.

finding files by ownership

7. Finding Empty Files and Directories

This method is useful for cleaning up empty files and directories that may clutter the filesystem.

  1. Access your command window and execute the following command to find empty files and directories:
find /path/to/search -empty

Replace /path/to/search with the directory path you want to search in.

finding empty files and directories

8. Finding Files and Running Commands on Them

This method allows you to find files and perform specific actions on them, such as deleting, moving, or copying.

  1. Launch your Terminal window and enter the following command:
find /path/to/search -name "filename" -exec command {} \;

Replace /path/to/search with the directory path, filename with the name pattern to search for, and command with the action to perform.

finding files and running command on them

9. Finding Files by Content

This method helps locate files that contain specific text within them, which is useful for searching logs or configuration files.

  1. Enter your command window and run the following command:
find /path/to/search -type f -exec grep -l "text_to_find" {} +

Replace /path/to/search with the directory path and text_to_find with the text you are looking for.

finding file by its content

10. Finding Recently Accessed Files

This method is useful for tracking file usage and identifying files accessed within a certain time frame.

  1. Open your Terminal
  2. Run the following command:
find /path/to/search -atime number_of_days

Replace /path/to/search with the directory path and number_of_days with the number of days since last access.

finding recently accessed files

11. Finding Files with Specific Inodes

Finding files by their inode number is useful for identifying hard links and resolving filesystem issues.

  1. In your Terminal, run the following command:
find /path/to/search -inum inode_number

Replace /path/to/search with the directory path and inode_number with the inode number of the file.

finding a file by inode number

This method helps refine search results by excluding certain directories, making searches faster and more relevant.

  1. Enter the following command in the Terminal to exclude specific directories from search:
find /path/to/search -path "/path/to/exclude" -prune -o -name "filename" -print

Replace /path/to/search with th

finding files excluding directories

Advanced Techniques with the Find Command

Using advanced techniques with the Find command in Linux will elevate your file-searching game. From executing commands on found files to combining multiple search criteria, using Find with other commands, and searching with advanced conditions, you’ll gain unparalleled control and efficiency in managing your files. Here are two advanced techniques you can use:

1. Executing Commands on Found Files

One of the powerful capabilities of the Find command is the ability to execute commands on the files it finds. The -exec option allows you to perform operations on each file individually. Here’s an example:

find /home/guestuser/Documents -name "*.log" -exec rm {} \;

This command removes all files with the .log extension in the /home/guestuser/Documents directory.

removing log files using find command

2. Using Find with Other Commands

The Find command can be combined with other Linux commands, such as grep or rm, to enhance its functionality. By piping the output of the Find command to another command, you can perform complex operations. Consider the following example:

find /home/guestuser/Downloads -name "*.txt" | xargs grep "keyword"

This command finds all files with the .txt extension in the /home/guestuser/Downloads directory and searches for the specified keyword within those files using the grep command.

finding files having a specific keyword

Best Practices for Using the Find Command

Using the find command efficiently can save time and help keep your system organized. Here are six best practices to make the most out of this powerful tool:

  • 📂 Use Specific Paths: Always specify the directory path you want to search in. This limits the search scope and speeds up the process.
  • 🔄 Combine Multiple Conditions: Use logical operators like -and and -or to combine multiple search criteria, making your searches more precise and efficient.
  • 🚀 Optimize for Performance: Use -prune to exclude directories you don’t want to search. This reduces search time, especially in large filesystems.
  • 🔒 Check Permissions: Use -perm to find files with specific permissions. This helps maintain security and ensure that sensitive files are not exposed.
  • 🛠️ Run Commands on Found Files: Use -exec to run commands on files that find locates. This can automate tasks like deleting old files or changing permissions.
  • 🔍 Use Quotes for Special Characters: When searching for files with special characters in their names, use quotes around the name patterns. This ensures accurate searches and avoids errors.

To Sum Up

To wrap up, you’ve now learned how to use the find command in Linux with a detailed step-by-step guide. From locating files by name, type, size, and time to understanding advanced techniques and best practices, you’re now equipped to manage your files more efficiently.

If you’re interested in expanding your knowledge further, I recommend:

Frequently Asked Questions

Does the Find command support searching for files based on their last access time?

Yes, the Find command supports searching files based on their last access time using the -atime predicate. This allows you to specify a time frame in days to search for files accessed more or less than a certain number of days ago. To illustrate, if you want to find files accessed within the last 7 days, you can use the following command: find /path/to/directory -atime -7. This command will locate files with their content accessed within the specified time frame, aiding in tasks such as identifying recently accessed files or monitoring activity within a directory.

Can the Find command be used to search for files based on their extensions?

Absolutely! The Find command is highly versatile and can be utilized to search for files based on their file extensions using the -name or -iname predicates. For instance, if you wish to find all text files within a specific directory with the extension “.txt”, you can employ the following command: find /path/to/directory -name “*.txt”. This command will traverse the given directory and its subdirectories, matching files with the specified extension. The Find command’s ability to filter files based on their extensions offers a convenient way to locate specific types of files and perform targeted operations or analyses.

Can the Find command be used to search for files based on their ownership?

Certainly! The Find command is equipped with predicates such as -user and -group, which enable searching for files based on ownership. To find files owned by a particular user, you can execute the command: find /path/to/directory -user username. Replace username with the desired user’s username. This command will locate all files within the specified directory that the specified user owns. Searching for files based on ownership can be valuable in scenarios where you need to identify files belonging to specific users, manage file permissions, or perform administrative tasks in a multi-user environment.

Ojash

Author

Ojash is a skilled Linux expert and tech writer with over a decade of experience. He has extensive knowledge of Linux's file system, command-line interface, and software installations. Ojash is also an expert in shell scripting and automation, with experience in Bash, Python, and Perl. He has published numerous articles on Linux in various online publications, making him a valuable resource for both seasoned Linux users and beginners. Ojash is also an active member of the Linux community and participates in Linux forums.

Akshat

Reviewer

Akshat is a software engineer, product designer and the co-founder of Scrutify. He's an experienced Linux professional and the senior editor of this blog. He is also an open-source contributor to many projects on Github and has written several technical guides on Linux. Apart from that, he’s also actively sharing his ideas and tutorials on Medium and Attirer. As the editor of this blog, Akshat brings his wealth of knowledge and experience to provide readers with valuable insights and advice on a wide range of Linux-related topics.

Share this article
Shareable URL
Prev Post

How to Use the Linux Shutdown Command [9 Best Ways]

Next Post

How to Check Open Ports in Linux [5 Easy Methods]

Leave a Reply

Your email address will not be published. Required fields are marked *

Read next