Operations 11 min read

Mastering Linux Find: Essential Tips and Advanced Usage

This article provides a comprehensive guide to the Linux find command, covering basic printing, filename patterns, type, timestamp, size, permission filters, empty file detection, deletion, date-range searches, parallel execution, absolute path retrieval, basename extraction, and how to exclude the search directory itself.

Linux Cloud Computing Practice
Linux Cloud Computing Practice
Linux Cloud Computing Practice
Mastering Linux Find: Essential Tips and Advanced Usage

Here are basic examples of the find command that I use frequently; after the examples you can read a theoretical explanation translated from the find man page with my own understanding, followed by deeper usage examples and analysis.

(1) Basic printing

The default action of -print prints each found file followed by a newline ( \n). Use -print0 to separate entries with a null byte ( \0) to avoid issues with spaces in filenames. Note that -print0 replaces \n with \0, and you may need external tools like xargs or tr to handle the null delimiters.

$ mkdir /tmp/a
$ touch /tmp/a/{1..5}.log
$ find /tmp/a   # equivalent to find /tmp/a -print
/tmp/a
/tmp/a/4.log
/tmp/a/2.log
/tmp/a/5.log
/tmp/a/1.log
/tmp/a/3.log

$ find /tmp/a -print0
/tmp/a/tmp/a/4.log/tmp/a/2.log/tmp/a/5.log/tmp/a/1.log/tmp/a/3.log

(2) Filename search

Common options are -name (matches the basename) and -path (matches the directory name plus basename). Enclose patterns in quotes and combine with wildcards.

$ find /tmp -name "*.log"
/tmp/screen.log
/tmp/x.log
/tmp/timing.log
/tmp/a/4.log
...

Use -path when you need to match a directory component, e.g.:

$ find /tmp -path '*a*/*.log'
/tmp/abc/axyz.log

Remember that character classes such as [a-z] match single characters; ranges like [1-20] are interpreted as [1-2] plus the character 0.

(3) Search by file type: -type

Typical types are regular files ( f), directories ( d), and symbolic links ( l).

$ find /tmp -type f -name "a*.sh"
$ find /tmp -type d -name "a*"

(4) Search by timestamps

Use -atime, -mtime, or -ctime. Example: find shell scripts modified within the last three days.

$ find /tmp -type f -mtime -3 -name "*.sh"

(5) Search by size

Find files larger than 100 KB:

$ find /tmp -type f -size +100k -name '*.sh'

(6) Search by permissions

Find shell scripts where the owner has read, write, and execute permissions:

$ find /tmp -type f -perm -0700 -name '*.sh'

(7) Find empty files or directories

$ find /tmp -type d -empty

(8) Delete found files

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

(9) Search within a date range

$ find /test -type f -newermt 2017-06-03 -a ! -newermt 2017-06-06

Alternatively, create two reference files with touch -m -d and use -newer.

(10) Parallel search acceleration

Use xargs -P 0 to run multiple find processes in parallel, for example to locate all files ending with “Find.pm” from the root.

ls --hide proc / | xargs -i -P 0 find /{} -type f -name "*Find.pm"

(11) Get absolute paths

When the search path is relative, prepend $(pwd), use the $PWD variable, or invoke readlink -f via -exec to obtain full paths.

(12) Extract basename with -printf

The -printf format %f prints the basename, while %P prints the path relative to the search directory.

$ find /tmp/test -printf "%f
"

(13) Exclude the search directory itself

Combine -path with a negation to omit the starting directory from results.

$ find /tmp/test ! -path /tmp/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.

File Searchcommand-linefind
Linux Cloud Computing Practice
Written by

Linux Cloud Computing Practice

Welcome to Linux Cloud Computing Practice. We offer high-quality articles on Linux, cloud computing, DevOps, networking and related topics. Dive in and start your Linux cloud computing journey!

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.