免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1839 | 回复: 9
打印 上一主题 下一主题

find 命令 求助 函数方案 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-02-10 14:00 |只看该作者 |倒序浏览
$ find /usr/share/locale -maxdepth 0 \( -type d ! -name "zh_CN" \) -exec ls {} \;
am  cs        de_AT  el     et  fr  hu  ja                lt     nl  pt_BR  si  tr  vi
ar  da        de_CH  en_GB  eu  gl  id  ko                lv     no  pt_PT  sk  uk  zh_CN
ca  de        de_DE  es     fi  he  it  locale.alias        nb_NO  pl  ru          sv  ur  zh_TW

俺想将 /usr/share/locale 目录下除zh_CN外的所有目录删除, 执行以上命令确保无误删,发现不成功,错在哪里呢?

$ ls /usr/share/man
es  jp   man0p  man1p  man3   man3x  man5  man7  mann  sv
fr  man  man1   man2   man3p  man4   man6  man8  nl    zh_CN

$ find /usr/share/man -maxdepth 0 \( -type d ! -name "man*" -name "zh_CN" \) -exec ls {} \;
$ find /usr/share/man -maxdepth 1 \( -type d ! -name "man*" ! -name "zh_CN" \) -exec ls {} \;
man1
man1
man1
man1
man1
$ find /usr/share/man -maxdepth 0 \( -type d ! -name "man*" ! -name "zh_CN" \) -exec ls {} \;

俺想将/usr/share/man 下除英文、中文帮助外的文件夹删除,执行以上命令,发现结果不对,错在哪里呢?

哪位高手能回答下了,多谢了!


同时俺想将这个功能——删除指定目录下  除。。。以外的所有目录
rm-dir-except ()
{
   #$0,  需要清理的目标目录;
    #$1, ...   需要排除的目录;
   find $0 -maxdepth 0 .....

}

好像要成为函数还是有些难度的哦。。。

[ 本帖最后由 carbonjiao 于 2009-2-10 14:46 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2009-02-10 14:29 |只看该作者
我的好像没问题:
[root@zhang shell]# ls /usr/share/man
bg  da  el  es  fr  hu  it  ko     man1   man2  man3p  man5  man7  man9  nl  pt     ro  sk  sv
cs  de  en  fi  hr  id  ja  man0p  man1p  man3  man4   man6  man8  mann  pl  pt_BR  ru  sl
[root@zhang shell]# find /usr/share/man -maxdepth 0 \( -type d ! -name "zh_CN" \) -exec ls {} \;
bg  da  el  es  fr  hu  it  ko     man1   man2  man3p  man5  man7  man9  nl  pt     ro  sk  sv
cs  de  en  fi  hr  id  ja  man0p  man1p  man3  man4   man6  man8  mann  pl  pt_BR  ru  sl

论坛徽章:
0
3 [报告]
发表于 2009-02-10 14:32 |只看该作者
是不是你的那个目录下本来就没有zh_CN目录呢?

俺直接复制你的命令:
# find /usr/share/man -maxdepth 0 \( -type d ! -name "zh_CN" \) -exec ls {} \;
es  jp         man0p        man1p  man3   man3x  man5  man7  mann  sv
fr  man  man1        man2   man3p  man4   man6  man8  nl    zh_CN

对帮助文件部分,还需要加筛选 man0p  man1p 等这些 英文帮助。。。

论坛徽章:
0
4 [报告]
发表于 2009-02-10 15:16 |只看该作者
find 的maxdepth参数
maxdepth 参数为0,则find 只处理参数本身
find /usr/share/man -maxdepth 0 \( -type d ! -name "zh_CN" \) -exec ls {} \;
所以 \( -type d ! -name "zh_CN" \) 为真得到结果是/usr/share/man,然后执行ls /usr/share/man,所以结果不是你想要的

论坛徽章:
0
5 [报告]
发表于 2009-02-10 15:43 |只看该作者
dream3401兄有没有什么好的建议,只查找/usr/share/locale 目录,不查找下级目录的办法呢?

论坛徽章:
0
6 [报告]
发表于 2009-02-10 15:58 |只看该作者
如果只找/usr/share/locale目录,试试:find /usr/share/locale -maxdepth 1 测试项

论坛徽章:
0
7 [报告]
发表于 2009-02-10 16:12 |只看该作者
还是把zh_CN及下面的目录LC_MESSAGES给搜出来了。。。

find /usr/share/locale -maxdepth 1 \( -type d ! -name "zh_CN" \) -exec ls {} \;
am  cs           de_CH  es  fr  id  locale.alias  nl           pt_PT  sv  ur
ar  da           de_DE  et  gl  it  lt            no           ru          sw  vi
bn  de           el          eu  he  ja  lv            pl           si          tr  zh_CN
ca  de_AT  en_GB  fi  hu  ko  nb_NO            pt_BR  sk          uk  zh_TW
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES
LC_MESSAGES

论坛徽章:
0
8 [报告]
发表于 2009-02-10 16:20 |只看该作者

回复 #7 carbonjiao 的帖子

以下是俺最开始实现的脚本,但俺认为功能强大的find应该可以完成这个简单动作——删除/usr/share/locale下非zh_CN的目录, zh_CN的下级目录自然是不能删除的。。。

#!/bin/bash
ls /usr/share/locale >>/tmp/del.txt
cat "/tmp/del.txt"| grep -v "^#" | while read dirname; do
          if [ "$dirname" != "zh_CN" ] && [ "$dirname" != "locale.alias" ]; then  
                echo " 删除语言包$dirname"
                rm -rf "/usr/share/locale/$dirname"
          else
                echo " 保留语言包$dirname"
                echo "/usr/share/locale/$dirname"
          fi
done

但这个基本也不具备通用性——删除指定目录下除指定的目录外的目录。。。
比如要实现删除/usr/share/man下除zh_CN和英文帮助外的所有帮助文件,代码就很麻烦的了。。。

论坛徽章:
0
9 [报告]
发表于 2009-02-10 19:28 |只看该作者
原帖由 carbonjiao 于 2009-2-10 16:12 发表
还是把zh_CN及下面的目录LC_MESSAGES给搜出来了。。。

find /usr/share/locale -maxdepth 1 \( -type d ! -name "zh_CN" \) -exec ls {} \;
am  cs           de_CH  es  fr  id  locale.alias  nl           pt_PT  sv   ...

由于maxdepth 为1所以find 命令首先测先参数"/usr/share/locale",由于它是目录,名字也不是zh_CN,所以测试为真,所以执行命令"ls /usr/share/locale",这样的的结果可想而知:会把/usr/share/locale下面的zh_CN目录项显示出来
怎么办呢?可以再一选项就是:
find /usr/share/locale -mindepth 1 -maxdepth 1 ....
你再试试看

如果想略个某个目录,可以用prune动作

[ 本帖最后由 dream3401 于 2009-2-10 19:35 编辑 ]

论坛徽章:
0
10 [报告]
发表于 2009-02-11 14:40 |只看该作者
采用  如下方法成功。  还是不熟悉 find的用法阿


[14:38:54][arch@archlive:~]$ ls /usr/share/locale
ar  da     de_CH  en_GB  eu  gl  it  locale.alias  nb_NO  pl     ru  tr  zh_CN
ca  de     de_DE  es     fi  he  ja  lt            nl     pt_BR  sk  ur  zh_TW
cs  de_AT  el     et     fr  hu  ko  lv            no     pt_PT  sv  vi
[14:38:57][arch@archlive:~]$ sudo find /usr/share/locale/* -maxdepth 0 -type d ! -iregex ".*zh.CN" -exec rm -rf {} \;
[14:39:11][arch@archlive:~]$ ls /usr/share/localelocale.alias  zh_CN
[14:39:15][arch@archlive:~]$

再来研究下用prune maxdepth 等实现方式
再变为函数。。。

多谢了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP