Awakening Awakening - 1 year ago 73
Bash Question

shell find -delete -- how to avoid delete itself

-- level-1-folder-1-dynamic
-- level-1-folder-2-dynamic-maybe-empty
-- level-1-folder-3-dynamic
-- level-2-folder-dynamic-need-to-be-deleted
-- level-2-folder-dynamic-need-to-be-deleted
-- file-1
-- file-2

I want to use
find command
to delete all level-2-folders that created 30mins before, but I can't find all level-2 folders and delete them.

What I can do now is to find all the files and delete them, but the level-2-folders still remain

find root -type -f -cmin +30 -delete

And if I using
find root -type -d -empty -delete
, it will delete all the empty folders, including level-1 and root itself.

How can I delete all the level-2-folders?


Answer Source

To delete only those empty directories that are level 2 or deeper, use -mindepth 2:

find root -mindepth 2 -type d -empty -delete
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download