17

How can I recursively cleanup all empty files and directories in a parent directory?

Let’s say I have this directory structure:

Parent/
  |____Child1/
        |______ file11.txt (empty)
        |______ Dir1/ (empty)

  |____Child2/
        |_______ file21.txt
        |_______ file22.txt (empty)

  |____ file1.txt

I should end up with this:

Parent/
  |____Child2/
        |_______ file21.txt

  |____ file1.txt
2
  • I wasn’t confused, per se, but, even before I saw the comments, I thought it was confusing that the illustration had multiple files with the same name.  You might want to consider using a naming convention like Child1/file11.txt, Child2/file21.txt and Child2/file22.txt. Commented Apr 5, 2020 at 4:59
  • @Scott ok, I have updated the file names. I hope, now it will not confuse anymore. Commented Apr 6, 2020 at 4:27

3 Answers 3

42

This is a really simple one liner:

find Parent -empty -delete

It's fairly self explanatory. Although when I checked I was surprised that it successfully deletes Parent/Child1. Usually you would expect it to process the parent before the child unless you specify -depth.

This works because -delete implies -depth. See the GNU find manual:

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


Note these features are not part of the Posix Standard, but most likely will be there under many Linux Distribution. You may have a specific problem with smaller ones such as Alpine Linux as they are based on Busybox which doesn't support -empty.

Other systems that do include non-standard -empty and -delete include BSD and OSX but apparently not AIX.

3
  • 12
    Please keep in mind that you are not quoting the find manual but the gfind manual. There would be no problem if you did describe a standard behavior of find, but your answer depends on two non-standard features (-empty and -delete).
    – schily
    Commented Apr 4, 2020 at 11:24
  • Incomprehensible what you mean "Parent" Commented Oct 22, 2022 at 12:04
  • @NiklasRosencrantz The term "parent" is from the question. And very clear to most readers. It's also a very common term when working with tree structures such as directory trees. Commented Oct 22, 2022 at 19:18
13

Here's a two command solution

Delete empty files

find Parent/ -type f -size 0 -delete

Try to remove all directories

find Parent/ -type d -depth -print0 | xargs -0 rmdir 2>/dev/null

NB rmdir can't remove non-empty directories, thus it's safe to run but will produce errors, which we are hiding

As above, but being a little more specific about the error messages being ignored. Might need to amend if the message varies across distros.

find Parent/ -type d -depth -print0 | xargs -0 rmdir 2>&1 \
  | grep -iv "Directory not empty$"
3
  • 1
    Generally I wouldn't recommend trying to delete something and then discarding the error message. It can hide errors for other unrelated reasons such as "access denied" errors. Commented Apr 4, 2020 at 9:51
  • That's fair, I'll add an alternative
    – bxm
    Commented Apr 4, 2020 at 9:53
  • 1
    Looks like GNU find has --ignore-fail-on-non-empty, funny enough.
    – Reid
    Commented Apr 6, 2020 at 18:33
2

You can also do this with the fd tool:

fd -t e -x rm -r

https://github.com/sharkdp/fd

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .