Awakening
Awakening

Reputation: 3795

shell find -delete -- how to avoid delete itself

root
  -- 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?
Thanks

Upvotes: 4

Views: 634

Answers (2)

sjsam
sjsam

Reputation: 21965

to delete all level-2-folders that created 30mins before

ssam@udistro:~/so/36928504$ tree root

gives me

root
|-- level1
|   |-- level2dir1
|   |-- level2dir2
|   |   `-- level2dir2file1
|   `-- level2file
`-- level1emptydir

I did :

find root -mindepth 2 -type d 2>/dev/null -exec rm -fR {} \;

The doing :

ssam@udistro:~/so/36928504$ tree root


gives me :

root
|-- level1
|   `-- level2file
`-- level1emptydir

which is what you want I guess..

Upvotes: 0

John1024
John1024

Reputation: 113864

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

find root -mindepth 2 -type d -empty -delete

Upvotes: 4

Related Questions