Linux: find (List files by filter. walk dir.)

By Xah Lee. Date: . Last updated: .

List Files by File Name Pattern (recursively)

# list files ending in .html
find . -name "*.html"
# list files ending in .html ignore letter case
find . -iname "*.html"

non-recursively

-maxdepth 1

# list files ending in .html ignore letter case, just top level dir
find . -iname "*.html" -maxdepth 1

List Files by File Size Filter

# list files larger than 9 mibi bytes
find . -size +9M
# list files smaller than 9 kibi bytes
find . -size -9k
# list files exactly 1234 bytes
find . -size 1234c

Delete Files by File Name Pattern

# delete all files, name ends with ~
find . -name "*~" -delete

Be very careful when using -delete. Make sure you test first without -delete, and make sure -delete is the last argument. Otherwise you may delete everything.

Delete empty files

# list all empty files
find . -type f -empty
# delete all empty files
find . -type f -empty -print -delete

Delete empty dirs

# list empty dirs
find . -depth -empty -type d
# delete empty dirs
find . -depth -empty -type d -print -delete

Find Recently Modified File

By Modified Time

# list files that's been modified within past 2 days
find . -atime -2
# list files that's modified in last 60 min
find . -mmin -60

By Access Time

# list files that has been opened (accessed) in last 60 min
find . -amin -60

By Status Time

# list files whose file status changed in last 60 min
find . -cmin -60

Using -exec option

you can call a command with the -exec option.

When you use -exec, it spawn process to run the shell command for each file.

# delete all files whose name ends with ~
find . -name "*~" -exec rm {} \;

xargs

Linux, Files and Dirs