Delete empty subfolders, keep parent folder

When I use

find /home/user/parentdir -type d -empty -delete

it looks recursively for empty subfolders inside /home/user/parentdir and deletes them. But if /home/user/parentdir is also empty, it deletes the parentdir folder too, which is undesirable for me.

I want to keep this parentdir to rsync some files to backup or cloud. After processing, I need to delete the empty folders, but seems unproductive to recreate parentdir every time.

Any suggestions to keep parentdir? I thought about creating a .nocopy file inside it and exclude it from rsync, but looks like overkill. Is there a more elegant way?


Simply do find /home/user/parentdir -mindepth 1 -type d -empty -delete.

Look:

$ mkdir -p test1/test2
$ find test1 -type d
test1
test1/test2
$ find test1 -mindepth 1  -type d
test1/test2

The find /home/user/parentdir/* in AmourK’s answer is undesirable when there are a lot of files and it is overcomplicated.


By adding /* to the end of parentdir, you are performing the action on all subdirs of parentdir rather than on parentdir itself. And so in the same way /home/user/ is not deleted in the old command, parentdir will not be not be deleted in the command below. * is called a glob operator and it matches any string of characters.

find /home/user/parentdir/* -type d -empty -delete