Linux: Traverse Directory: find, xargs

By Xah Lee. Date: . Last updated: .
linux shell find command 85396
find for traverse directory.

list files whose name matches a text pattern

# list files ending in .html
find . -name "*.html"

list only files larger than n bytes

# 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

[see Kilo vs Kibi, Mega vs Mibi]

delete all files whose name matches a text pattern

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

be very careful with this. Usually, you should first do find . -name "*~".

A much faster way is to use the -delete option of find directly.

# delete all files whose name ends with ~.
find . -name "*~" -delete
# -delete is a option of find

Using the -delete option is much faster because it doesn't spawn processes. When you use -exec …, it actually spawn process to run the shell command for each file.

The advantage of using -exec … is that you can call any unix command, not just options supported by find.

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 -delete

delete empty dirs

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

find recently modified file

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

# list, file content modified in last 60 min
find . -mmin -60

# list, file accessed in last 60 min
find . -amin -60

“find” with “xargs”

use “find” on file names that may contain spaces or dash

# print file names that may contain spaces
find . -print0 | xargs -0 -l -i echo "{}";

Here's the options used for xargs:

Here's a useful example:

# convert all bmp files to png in a dir. Requires “convert” from ImageMagick
find . -name "*bmp" -print0 | xargs -0 -l -i basename "{}" ".bmp" | xargs -0 -l -i convert "{}.bmp" "{}.png"

Use GNU Parallel for xargs

Note: a modern replacement for xargs is GNU parallel. The syntax is almost indentical to xargs, except it runs in parallel. It also doesn't have problems with file names containing quotes or apostrophes.

Thanks to Ole Tange for telling me about GNU Parallel. (Ole is the author)

Linux Shell Basics

  1. Get System Info
  2. Shell Basics
  3. grep, cat, awk, uniq
  4. sort
  5. find, xargs
  6. diff Files/Dir
  7. dir size: du
  8. dir tree
  9. tar gzip bzip2 xz 7zip rar zip
  10. wget, curl, GET, HEAD
  11. rsync
  12. Install Packages

Sys Admin

  1. Job Control
  2. ps
  3. top
  4. htop
  5. RAM stat
  6. Users and Groups
  7. File Permission
  8. Opened Files: lsof
  9. shutdown, sleep


  1. Bash Keys, Terminal Keys, Man Page Keys
  2. Bash Prompt Setup
  3. Bash Color Prompt
  4. .bashrc, .profile, .bash_profile
  5. Virtual Console
  6. Terminal Control Sequence Keys
  7. Reset Terminal
  8. tmux
  9. man page
  10. Bash Manual in Chapters
  11. BASH Shell Misc Tips
  12. Log Terminal Session

Linux Desktop

  1. Most Useful GUI Apps
  2. Set Default App
  3. Shell Commands for GUI Apps
  4. Image Viewers
  5. Music Players
  6. Move File to Trash by Command
  7. X11 Selection and Clipboard
  8. How to Switch to LXDE, Xfce
  9. LXDE Keyboard Shortcuts
  10. LXDE/Openbox, Change Keyboard Shortcuts
  11. LXDE Set Key Repeat Rate
  12. LXDE/OpenBox, Disable Mouse Scroll Wheel Hide Window
  13. Xfce Keyboard Shortcuts
  14. Xfce Good Themes
  15. xmonad Keybinding
  16. How to Restart X11
  17. Why Tiling Window Manager Sucks
  18. Standard Fonts
  19. How to Install Font

If you have a question, put $5 at patreon and message me.