简体   繁体   中英

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

要仅删除级别为2或更深的空目录,请使用-mindepth 2

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM