Skip to main content Logo

Quis custodiet ipsos custodes?
Home | About | All pages | Cluster Status | RSS Feed

Bash bits: find has a -delete flag

Published: 14-07-2019 | Author: Remy van Elst | Text only version of this article

❗ This post is over five years old. It may no longer be up to date. Opinions may have changed.

Table of Contents

Bash Bits are small examples, tips and tutorials for the bash shell. This bash bit shows you that find has a -delete option. I recently found this out, before I would always use -exec rm {} \;. This is shorter and easier to remember.

Recently I removed all Google Ads from this site due to their invasive tracking, as well as Google Analytics. Please, if you found this content useful, consider a small donation using any of the options below:

I'm developing an open source monitoring app called Leaf Node Monitoring, for windows, linux & android. Go check it out!

Consider sponsoring me on Github. It means the world to me if you show your appreciation and you'll help pay the server costs.

You can also sponsor me by getting a Digital Ocean VPS. With this referral link you'll get $200 credit for 60 days. Spend $25 after your credit expires and I'll get $25!

All Bash Bits can be found using this link

find -delete flag

A long long time ago one of my co-workers showed me an option to find which removes all the files the find command found. Let's say I would want to remove all .txt files in a folder, I first would build the find command:

find . -type f -iname "*.txt"

If it found files, a list of them will be printed to the console. After checking the list, I would append the command to remove all found results:

find . -type f -iname "*.txt" -exec rm {} \;

That would, for every file, run the command rm filename;. This is a lot of typing and depending on the shell you might need to escape characters (like here).

Just now I was looking to remove all empty directories and found on the find manpage that it nowdays has a -delete flag. Which makes the above command easier:

find . -type f -iname "*.txt" -delete

Or in my case, to remove empty directories:

find . -type d -empty -delete

(if you omit the -type d find will also remove empty files).

The -delete flag will perform better since it doesn't have to spawn an external rm process for every file. However, the POSIX 1003.1 man page for find specifies -exec but not -delete, thus the former being a requirement for POSIX compatibility. Some embedded linux systems or UNIX systems therefore might also not have the delete flag.

The manpage has more information including a warning:

Delete files; true if removal succeeded. If the removal failed, an error message is issued. If -delete fails, find's exit status will be nonzero (when it eventually exits). Use of -delete automatically turns on the '-depth' option. Warnings: Don't forget that the find command line is evaluated as an expression, so putting -delete first will make find try to delete everything below the starting points you specified. When testing a find command line that you later intend to use with -delete, you should explicitly specify -depth in order to avoid later surprises.

Tags: bash , bash-bits , delete , exec , find , rm , shell , snippets