Operations 6 min read

Master Linux ‘find’ Command: Syntax, Options, and Practical Examples

This guide introduces the powerful Linux find command, explaining its syntax, common options such as -name, -type, -perm, and actions like -print, -exec, and -ok, and provides numerous practical examples for searching files by name, path, permissions, type, size, time, and executing commands on matches.

Open Source Linux
Open Source Linux
Open Source Linux
Master Linux ‘find’ Command: Syntax, Options, and Practical Examples

Overview

The find command searches for files in a specified directory hierarchy and can perform actions on the results. It supports many criteria such as name, type, permissions, timestamps, size, etc.

Syntax

find [path] -options [-print -exec -ok]

In Chinese the syntax translates to:

find [directory] [search rules] [action]

Key Options

-name : match file name (case‑sensitive).

-iname : match file name case‑insensitively.

-perm : match file permissions.

-user : match file owner.

-group : match file group.

-type : match file type (f, d, l, etc.).

-print : output the matched paths.

-exec : execute a shell command on each match (e.g., find . -name "file.txt" -exec cp {} {}.bak \;).

-ok : like -exec but asks for confirmation before each command.

Wildcard Characters

Common glob patterns used with find: * – matches any string of characters. ? – matches any single character. [...] – matches any one of the characters inside the brackets.

Practical Examples

1. Find by name

# Exact name
find . -name "example.txt" -print
# Case‑insensitive
find . -iname "example.txt" -print

2. Find by directory

# Exclude "code" directory
find . -path "./code" -prune -o -name "*.txt" -print
# Exclude "code" and "codetest"
find . \( -path "./code" -o -path "./codetest" \) -prune -o -name "*.txt" -print
# Search only the current directory (no recursion)
find . -maxdepth 1 -name "*.txt" -print

3. Find by permissions

find . -perm 755 -type f -print

4. Find by file type

find . -type l -print   # symbolic links

5. Find by owner or group

# Owner is admin
find . -user admin -print
# Files without an owner
find . -nouser -print
# Group is mysql
find . -group mysql -print
# Files without a group
find . -nogroup -print

6. Find by time

# Modified within the last day
find . -mtime -1 -print
# Modified more than a day ago
find . -mtime +1 -print
# Accessed within the last day
find . -atime -1 -print
# Changed status within the last day
find . -ctime -1 -print
# Changed status more than a day ago
find . -ctime +1 -type f -print
# Changed status more than 10 minutes ago
find . -cmin +10 -type f -print

7. Find by file age relative to another file

# Newer than aa.txt
find . -newer "aa.txt" -type f -print
# Older than aa.txt
find . ! -newer "aa.txt" -type f -print
# Between aa.txt and bb.txt
find . -newer "aa.txt" ! -newer "bb.txt" -type f -print

8. Find by size

# Larger than 1 MiB
find / -size +1M -type f -print
# Exactly 6 bytes
find . -size 6c -print
# Smaller than 32 KiB
find . -size -32k -print

9. Execute commands on matches

# Delete del.txt after confirmation
find . -name "del.txt" -ok rm {} \;
# Backup aa.txt
find . -name "aa.txt" -exec cp {} {}.bak \;

These examples demonstrate how to combine find options to locate files based on various criteria and perform actions such as copying, deleting, or listing them.

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.

LinuxShellUnixFile Searchcommand-linefind
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

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.