Fundamentals 10 min read

Master Linux File Search: Powerful find Command Tricks and Examples

This guide walks through the versatile Linux find command, showing how to locate files and directories by name, type, size, timestamps, permissions, and more, while demonstrating practical examples such as pattern matching, case‑insensitive searches, depth limiting, and executing actions on results.

FunTester
FunTester
FunTester
Master Linux File Search: Powerful find Command Tricks and Examples

Viewing the Manual

Before using find, you can read its full documentation with man find to understand all available options.

Viewing Directory Structure

Use the tree command to display the current directory hierarchy, which helps you see where files are located before searching.

$ tree
.
├── demoSub
│   └── testhappy.txt
├── testgood.txt
└── testnice.txt

Finding All Files

To list every file and directory under the current path:

$ find .

Finding by Filename

Specify a filename directly to check its existence in the current directory:

$ find testnice.txt

Finding by Type

Use -type d to list only directories or -type f for regular files:

$ find . -type d
$ find . -type f

Finding by Name Pattern

Combine -name with a pattern. The pattern is case‑sensitive:

$ find . -type f -name "testhappy.txt"
$ find . -type f -name "test*"
$ find . -type f -name "*.txt"

For case‑insensitive matching, replace -name with -iname:

$ find . -type f -iname "test*"

Finding by Modification Time

Use -mmin for minute‑based modification time. -mmin -10 finds files changed within the last 10 minutes; -mmin +10 finds files changed more than 10 minutes ago. A range can be expressed with two -mmin options: $ find . -type f -mmin +5 -mmin -10 For day‑based searches, use -mtime (e.g., -mtime -2 for files modified in the last two days).

Finding by Access and Change Time

Other time attributes include -atime / -amin (last access) and -ctime / -cmin (last status change).

Finding by Size

The -size option filters files by size. Prefix + means greater than, - means less than. Units: c (bytes), k (KB), M (MB), G (GB).

$ find . -size +2M   # files larger than 2 MB
$ find . -size -2M   # files smaller than 2 MB

Finding Empty Files

Use -empty to locate files or directories with no content:

$ find . -empty

Finding by Permissions

Search for files with specific permission bits using -perm:

$ find . -perm 664

Executing Actions on Found Items

The -exec option runs a command on each match. The placeholder {} represents the current file, and the command must end with \; or + (the latter groups multiple files).

# Change permissions to 777 for all files with 664
find . -perm 664 -exec chmod 777 {} +

# Change owner and group
find . -exec chown qin:tester {} +

# Delete all .txt files (use a dry‑run first)
find . -type f -name "*.txt" -exec rm {} +

Limiting Search Depth

Restrict how deep find descends with -maxdepth. For example, -maxdepth 1 searches only the current directory:

$ find . -maxdepth 1 -name "*.txt" -exec rm {} +

Summary

The find command is a powerful tool for locating files and directories based on a wide range of criteria—name, type, size, timestamps, permissions, and more. Combined with -exec and depth‑limiting options, it enables complex batch operations directly from the shell, turning simple command‑line usage into an efficient workflow.

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.

LinuxShellcommand-lineUnixFile Searchfind
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

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.