Chinaunix

标题: 如何做到批量删除指定文件夹 [打印本页]

作者: max5    时间: 2012-11-06 11:38
标题: 如何做到批量删除指定文件夹
如何做到批量删除指定文件夹

目录结构如下
D:\tmp
├─AA
│  ├─cache
│  └─tmp
├─BB
│  ├─cache
│  └─tmp
├─CC
│  ├─cache
│  └─tmp
└─DD
    ├─cache
    └─tmp

现在想把除了BB目录以外,各一级子目录下的tmp、cache都删除


如果通过这样的命令
rm -rf /tmp/*/cache
rm -rf /tmp/*/tmp

可以做到把子目录下的tmp和cache都删除,但是我还需要保留某个文件夹下的tmp、cache
这个就不知道如何操作了
作者: yestreenstars    时间: 2012-11-06 12:25
  1. find /tmp/* -type d -name cache -o -name tmp | xargs rm -rf
复制代码

作者: max5    时间: 2012-11-06 12:33
谢谢

那么怎么排除B目录呢
作者: yestreenstars    时间: 2012-11-06 12:40
回复 3# max5
  1. find /tmp/* -type d -name cache -o -name tmp | grep -v "/tmp/BB" | xargs rm -rf
复制代码

作者: waker    时间: 2012-11-06 13:08
find -path './BB*' -prune  -o \( -name tmp -o -name cache \)  -exec rm -rf {} \;

作者: nathanielwen    时间: 2012-11-06 13:15
用个耍赖的方法,把你需要保留的文件夹都移走先,然后删除,再移回来,脚本也很短。而且没有复杂的语法。
假设temp文件夹下是这样的:
  1. [root@nathaniel temp]# tree
  2. .
  3. |-- AA
  4. |   |-- cache
  5. |   `-- temp
  6. |-- BB
  7. |   |-- cache
  8. |   `-- temp
  9. |-- CC
  10. |   |-- cache
  11. |   `-- temp
  12. |-- DD
  13. |   |-- cache
  14. |   `-- temp
  15. |-- EE
  16. |   |-- cache
  17. |   `-- temp
  18. `-- rmdir
复制代码
rmdir是脚本,内容如下:
  1. [root@nathaniel temp]# cat rmdir
  2. mv BB CC ..
  3. rm -fr *
  4. mv ../BB .
  5. mv ../CC .
复制代码
运行一下脚本的话,目录就只剩下BB和 CC了:
  1. [root@nathaniel temp]# tree
  2. .
  3. |-- BB
  4. |   |-- cache
  5. |   `-- temp
  6. `-- CC
  7.     |-- cache
  8.     `-- temp
复制代码

作者: 修杰_JIANG    时间: 2012-11-06 14:50
本帖最后由 修杰_JIANG 于 2012-11-08 13:51 编辑
  1. find /tmp/ |sed -r 's/(.*[^BB]\/\w)$/rm -rf \1/e'
复制代码

作者: dn833    时间: 2012-11-06 18:21
  1. [root@TEST tmp]# tree
  2. .
  3. |-- AA
  4. |   |-- cache
  5. |   `-- tmp
  6. |-- BB
  7. |   |-- cache
  8. |   `-- tmp
  9. |-- CC
  10. |   |-- cache
  11. |   `-- tmp
  12. `-- DD
  13.     |-- cache
  14.     `-- tmp

  15. 12 directories, 0 files
  16. [root@TEST tmp]# find . -type d -regex '^[^BB]*' -mindepth 2|xargs rm -rf
  17. find: warning: you have specified the -mindepth option after a non-option argument -type, but options are not positional (-mindepth affects tests specified before it as well as those specified after it).  Please specify options before other arguments.

  18. [root@TEST tmp]# tree
  19. .
  20. |-- AA
  21. |-- BB
  22. |   |-- cache
  23. |   `-- tmp
  24. |-- CC
  25. `-- DD

  26. 6 directories, 0 files

复制代码

作者: zooyo    时间: 2012-11-06 19:26
提示: 作者被禁止或删除 内容自动屏蔽
作者: max5    时间: 2012-11-06 20:07
谢谢ls各位帮忙
我稍微改造了下 可以排除多个目录了,比如这个排除AA、BB、DD目录
find ./* -type d -name cache -o -name tmp | grep -v 'BB\|DD\|AA'| xargs rm -rf

人的需求总数慢慢膨胀的,我现在又在考虑怎么更方便的维护被排除的目录
当被排除的目录少量时候,这样写没问题,如果被排除的目录多到10个、20个,维护起来就不那么方便了。
我想把这个搞成一个sh,然后把被排除的目录放到一个变量里面。比如一行一条
作者: max5    时间: 2012-11-06 20:21
zooyo 发表于 2012-11-06 19:26
回复 8# dn833


这个好像的确有点问题
作者: dn833    时间: 2012-11-07 00:04
回复 9# zooyo


    有啥子问题??明天再搞下......
作者: dn833    时间: 2012-11-07 00:05
回复 11# max5


    好像......到底是有还是没有......
作者: su8610    时间: 2012-11-07 00:25
有个办法,先定义一个参数,value是你要保留的文件名字,然后继续用find,就是grep的时候用那个参数,不知道可行不




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2