3v-Hosting Blog

How to Use the Linux find Command

Administration

8 min read


In a Linux environment, searching for files is one of the most common tasks system administrators, developers, and power users face. Linux offers a powerful and flexible utility for this purpose: the find command. Understand how to use find in Linux to enhance productivity. Locate files, filter search results, and even execute actions on the found files—all directly from the command line. This article gives you the complete lowdown on the find command in Linux, including its syntax, options, practical examples, and advanced use cases.

 

 

 

Introduction to the find Command

 

The find command in Linux is a versatile tool designed to search for files and directories based on a variety of conditions, such as name, size, type, permissions, modification date, and more. The find command is more powerful than other search tools because it traverses directory trees recursively. This ensures a thorough inspection of the filesystem starting from a specified directory.


The basic syntax is:

    find [starting-point] [options] [expression]

    starting-point: Directory path where the search begins.
    options: Flags that alter the behavior of the search.
    expression: Criteria to match files or directories.

Even though other utilities like locate or grep can also be used for finding files, find in Linux is unique because it always searches in real-time and supports more complex filtering and action executions.

 

 

 

 

Basic File Search with find

 

At its simplest, the linux find command can be used to locate a file by name. For example:

    find /home/user -name "report.txt"

This command starts at /home/user and searches recursively for a file named exactly report.txt. By default, -name is case-sensitive. If you need a case-insensitive search, you can use -iname:

    find /home/user -iname "Report.txt"

This small distinction can be critical when working on filesystems where case sensitivity matters.


To search for directories instead of files, you can use the -type d option:

    find /var/log -type d -name "apache2"

In contrast, to restrict results to regular files, use -type f:

    find /var/log -type f -name "*.log"

This example will return all files ending with .log inside /var/log and its subdirectories.

 

 

 

 

Using Size, Time, and Permissions Filters

 

The real power of find command linux lies in its ability to filter based on various file attributes beyond just names.


1. Size-Based Search

You can locate files based on their size using the -size option:

    find /home -size +100M

This finds all files larger than 100 Megabytes. Similarly:

    -size -10k - finds files smaller than 10 kilobytes;
    -size 1G - finds files exactly 1 gigabyte in size.

 

2. Time-Based Search

You can find files based on when they were last accessed (-atime), modified (-mtime), or changed (-ctime).

    find /var/log -mtime -7

This command finds files modified within the last 7 days.

 

3. Permissions-Based Search

Searching for files with specific permissions can be done with -perm:

    find /home -perm 644

This finds files with permissions exactly set to rw-r--r--. To find files that are readable by everyone, regardless of owner permissions:

    find /home -perm -o=r

This flexibility makes linux command find extremely useful for auditing files across systems.

 

 

 

 

Executing Commands on Found Files

 

One of the most advanced features of the bash find command is its ability to execute actions on each file that matches the search criteria. This is performed using the -exec option.

 

For example, to remove all .tmp files:

    find /tmp -type f -name "*.tmp" -exec rm {} \;

Here, {} is a placeholder for the current file, and \; marks the end of the command.


Another practical example is changing permissions:

    find /var/www -type f -name "*.php" -exec chmod 644 {} \;


You can also chain multiple commands:

    find /home/user/docs -name "*.txt" -exec cp {} /home/user/backup/ \; -exec rm {} \;

This command copies .txt files to a backup directory and deletes the originals.

Alternatively, you can use -exec ... + instead of \; to execute the command once with all the files as arguments (more efficient for a large number of files).

 


 

Other useful articles on administration in our Blog:


    - 10 most frequently used examples for IPTABLES

    - Cron - Schedule tasks on Linux servers correctly

    - 502 Bad Gateway Error: What It Is and How to Fix It

    - How to Check Your Ubuntu Version

 


 

 

Combining Conditions with Logical Operators

 

When performing complex searches, combining conditions using logical operators can be very powerful.


AND operator (-a)

    find /etc -type f -name "*.conf" -a -size +10k

Finds configuration files larger than 10 kilobytes.


OR operator (-o)

    find /etc -name "*.conf" -o -name "*.cfg"

Finds files with .conf or .cfg extensions.


Grouping with parentheses is often necessary:

    find /etc \( -name "*.conf" -o -name "*.cfg" \) -a -size +10k

This ensures that the logical operators apply correctly to grouped expressions, similar to using parentheses in regular programming.

 

 

 

 

 

Special Options and Performance Considerations

The find linux tool provides additional special options to improve performance and fine-tune searches.


Pruning Directories

You can avoid descending into specific directories using the -prune option:

    find / -path /proc -prune -o -name "core" -print

This prevents find from wasting time inside /proc.


Depth Control

Control the depth of search with -maxdepth and -mindepth:

    find /home/user -maxdepth 2 -name "*.jpg"

This limits the search to two directory levels.


Following Symlinks

By default, find does not follow symbolic links. To change this behavior, use -L:

    find -L /home/user -name "*.txt"

Be careful with this option because it can cause infinite loops if symbolic links are circular.

 

 

 

 

 

Integration with Other Linux Commands

 

The ability of find in linux to integrate seamlessly with other shell utilities makes it even more valuable.


For instance, finding large files and sorting them by size:

    find /var -type f -exec du -h {} + | sort -rh | head -n 10

This pipeline finds files under /var, calculates their disk usage, sorts them in reverse order by size, and shows the 10 largest files.


Another example is using xargs for efficiency:

    find /tmp -name "*.log" | xargs rm

This approach is faster when dealing with a large number of files because it minimizes the number of spawned processes.


Note that if filenames contain spaces, it’s better to use:

    find /tmp -name "*.log" -print0 | xargs -0 rm

Here -print0 and -0 allow safe processing of files with special characters in their names.

 

 

 

 

Practical Use Cases

 

1. Cleaning Up Old Backups

    find /backup -type f -name "*.tar.gz" -mtime +30 -exec rm {} \;

Removes backup archives older than 30 days.


2. Finding World-Writable Files

    find / -type f -perm -o=w

Useful for security audits.


3. Locating Empty Directories

    find /var/www -type d -empty

Helps in identifying unnecessary folders.


4. Searching Files Modified During a Specific Period

    find /home/user -type f -newermt 2025-04-01 ! -newermt 2025-04-15

Finds files modified between April 1 and April 14, 2025.

 

 

 

 

Conclusion

The find command is a foundational tool that anyone working with Linux systems must master. Its ability to filter files based on almost any criteria, execute actions directly on matches, and combine with other Linux utilities makes it indispensable for system administration, software development, and everyday file management.

Understand how to use find in Linux. Remember options and syntax, but also develop an intuition for building efficient search expressions that save time and automate workflows. The Linux search command is the most reliable tool in the Linux toolkit, whether you're doing a simple file name search or performing a complex system audit.

Master the bash find command to achieve true command-line proficiency and unlock the power to automate and script with ease in Linux environments.