Operations 6 min read

Master Linux File Searches: Powerful Find Command Techniques

This guide explores the versatile Linux find command, demonstrating how to locate files and directories by modification time, size, name patterns, and depth, while showing practical examples, useful flags, and advanced tricks such as combining criteria, excluding mounts, and executing actions on matches.

Code Mala Tang
Code Mala Tang
Code Mala Tang
Master Linux File Searches: Powerful Find Command Techniques

Find command is one of the most valuable Linux commands, allowing you to locate files and directories based on criteria such as age, size, and name. All examples can be combined, e.g., filtering by name, size, and date in a single search.

Find Recently Modified Files

$ find /home -mmin -60 -type f
/home/kpatronas/.motd_shown
-mmin -60

: files modified in the last 60 minutes. -type f: search only files. /home: path to search.

To search by hour you can use the trick -$((26*60)), which evaluates to 120 minutes.

$ find / -mmin -$((26*60)) -type f
/home/kpatronas/.motd_shown
/etc/hosts
/etc/timezone
/etc/ld.so.cache
/etc/hostname
/etc/ld.so.conf.d/ld.wsl.conf
/var/cache/ldconfig/aux-cache

To find files by days use the -mtime option; note that rounding affects how modification times are interpreted.

$ find / -mtime -3 -type f
/home/kpatronas/.landscape/sysinfo.log
/home/kpatronas/.sudo_as_admin_successful
/home/kpatronas/.bash_history
/home/kpatronas/.motd_shown
/home/kpatronas/.profile
/home/kpatronas/.bash_logout

To find files modified between two timestamps, use the -newermt option. -newermt: finds files newer than the given timestamp; prefixing with ! negates the result.

$ find / -type f -newermt "2022-11-22 00:00:00" ! -newermt "2022-11-22 00:41:00"
/home/kpatronas/.motd_shown
/etc/hosts
/etc/timezone
/etc/ld.so.cache
/etc/hostname
/etc/ld.so.conf.d/ld.wsl.conf
/var/cache/ldconfig/aux-cache

Finding Directories

Replace -type d with -type f to apply the same examples to directories. Omitting -type searches both files and directories.

$ find / -mmin -60 -type d
/
/home/kpatronas
/etc
/mnt/wsl
/sys
/dev
/run
/proc
/var/cache/ldconfig

Find Files by Size

Use the -size option to find files of a specific size. Suffixes: b (512‑byte blocks, default), c (bytes), w (two‑byte words), k (kilobytes), M (megabytes), G (gigabytes).

$ find / -size 1M

Prefix the value with + or - to find files larger or smaller than the given size.

Greater than 1 MB

find / -size +1M

Less than 1 MB

find / -size -1M

To find files between two sizes, combine the criteria, e.g., find / -size +10M -size -20M for files between 10 MB and 20 MB.

Find Files by Name Pattern

Use the -name option to match file names, e.g., files named hosts:

find / -type f -name 'hosts'

Wildcards are supported; 'hosts*' matches all files starting with host.

find / -type f -name 'hosts*'

Some Useful Tips

Limit search depth with -mindepth and -maxdepth. The following command searches only between the 4th and 6th levels of subdirectories:

$ find / -mindepth 4 -maxdepth 6

Use -exec to run a command on each matched file, e.g., delete all *.test files in the home directory:

$ find ~ -name '*.test' -exec rm {} +

To search only the root partition while excluding a mounted /test partition, use -mount:

$ find / -mount -name '*.test'
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxShell scriptingFile Searchfind command
Code Mala Tang
Written by

Code Mala Tang

Read source code together, write articles together, and enjoy spicy hot pot together.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.