ATB Team

How to Find a File in Linux: 10 Ways to Use the Find Command

If you’ve been using Linux for a while, you’ve probably found yourself in a situation where you need to search for a file or directory, but you’re not sure where it is. Luckily, Linux provides a powerful command called find that lets you search your filesystem for files and directories based on various criteria, such as name, size, type, or modification time.

In this guide, we’ll explore the basics of the find command and show you 10 ways to use it to find files in Linux. Whether you’re a beginner or an experienced user, this guide will help you become more proficient in finding files quickly and easily on your system.

What is the find Command?

The find command in Linux is used to search for files and directories within a directory hierarchy. It allows you to specify a wide range of search criteria, including:

  • File name
  • Size
  • Date of modification
  • Permissions
  • File type

Basic Syntax of the find Command

Before diving into the different ways you can use find, let’s take a look at the basic syntax:

find [path] [options] [expression]
  • [path]: The directory where the search should begin. If you want to search the entire system, use /. If you want to search from the current directory, use ..
  • [options]: These are optional flags that you can use to modify the behavior of the find command.
  • [expression]: This is the condition you’re using to filter the files (e.g., searching by name, size, type, etc.).

Example: Find Files in the Current Directory

find . 

This command will list all files and directories in the current directory and its subdirectories.

10 Ways to Use the find Command in Linux

1. Find Files by Name

One of the most common ways to use the find command is to search for files by name. You can search for files with a specific name or pattern.

Example: Find files named file.txt in the current directory and subdirectories.

find . -name "file.txt"

You can also use wildcards (like *) to search for files with similar names.

Example: Find all .txt files.

find . -name "*.txt"

2. Find Files by Extension

You can also search for files based on their extension. This is particularly useful if you know the type of file you’re looking for but don’t know the exact name.

Example: Find all .jpg image files.

find . -name "*.jpg"

3. Find Files by Size

The find command can help you locate files based on their size. You can specify file size criteria such as greater than, less than, or exactly.

Example: Find all files larger than 10 MB.

find . -size +10M

Other size options:

  • -size -10M (files smaller than 10 MB)
  • -size 10M (files exactly 10 MB)

4. Find Files Modified in the Last n Days

If you want to find files that were modified within a certain number of days, you can use the -mtime option.

Example: Find files modified in the last 7 days.

find . -mtime -7

If you want to find files that haven’t been modified in the last 7 days, use a positive number:

find . -mtime +7

5. Find Files by Permissions

You can search for files with specific permissions using the -perm option. This is useful when you need to find files that are readable, writable, or executable by specific users or groups.

Example: Find files that are readable and writable by the owner.

find . -perm 600

6. Find Empty Files and Directories

You can use the -empty option to find files or directories that are empty.

Example: Find empty files in the current directory and subdirectories.

find . -type f -empty

You can also search for empty directories:

find . -type d -empty

7. Find Files by Type

If you’re only interested in searching for specific types of files, such as regular files, directories, or symbolic links, you can use the -type option.

Example: Find all directories in the current directory and subdirectories.

find . -type d

Other types you can search for:

  • -type f: Regular files
  • -type l: Symbolic links
  • -type p: Named pipes (FIFOs)

8. Find Files and Execute Commands on Them

You can use the -exec option to run a command on each file that the find command returns. This is useful when you want to automate tasks on files that match your search criteria.

Example: Find all .txt files and list their details using the ls -l command.

find . -name "*.txt" -exec ls -l {} \;

In this example, {} is a placeholder for the found file, and \; is used to terminate the command.

9. Find Files Owned by a Specific User

You can use the -user option to search for files that are owned by a specific user.

Example: Find all files owned by the user john.

find . -user john

10. Find Files Based on Modification Time

The -mmin option allows you to find files based on their last modification time, in minutes. You can search for files modified within a certain number of minutes.

Example: Find files modified in the last 30 minutes.

find . -mmin -30

Similarly, you can use + for files modified more than 30 minutes ago:

find . -mmin +30

Conclusion

The find command in Linux is a powerful tool for locating files based on various criteria like name, size, date of modification, permissions, and more. By mastering this command, you’ll be able to quickly locate files on your system, making it much easier to manage your data.

Here are a few things to remember:

  • You can search by name, type, size, modification time, and more.
  • The -exec option allows you to run commands on files found by find.
  • Combine different options to make your search more specific and efficient.

Now that you know how to use the find command in Linux, you can search for files like a pro

Leave a Comment

Table Of Content