LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 949|回复: 4

关于xargs传递到rm的参数 (问题解决了!感谢各位大神的指点,受俺一拜~~)

[复制链接]
发表于 2008-6-2 20:01:07 | 显示全部楼层 |阅读模式
想删除文件夹下及其子文件夹中的除以.txt为结尾之外的所有文件。

用xargs向rm 传递的时候由于文件名中间有空格,结果被认成两个文件删除失败。

能不能在不改文件名的情况下完成任务。请高人指点。谢!~

========================================
2楼版主的命令好使!说不好使是因为忘了加后面的分号。版主的命令真是意味深长,回味无穷,值得细细揣摩。

经4楼5楼指点man了xargs发现xargs读取输入时遇到空格就停止,解决方法是使用xargs的-0参数,指定读取输入时使用NULL做为分隔符号;在find中使用-print0选项,指定输出时使用NULL做为分隔符号。
find  ./  !  -name  '*txt'  -type  f  -print0  |  xargs  -0  rm -i
这样就删除了当前目录和子目录中的其它所有文件,只保留以txt结尾的。

linux命令行就是好使!
发表于 2008-6-2 20:59:20 | 显示全部楼层
  1. find . ! -name '*.txt' ! -type d -exec rm -i {} \;
复制代码
回复 支持 反对

使用道具 举报

发表于 2008-6-2 23:34:04 | 显示全部楼层
Post by Jockey;1858209
  1. find . ! -name '*.txt' ! -type d -exec rm -i {} \;
复制代码


不好使啊!~
回复 支持 反对

使用道具 举报

发表于 2008-6-3 15:30:04 | 显示全部楼层
man xargs

find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f
Find files named core in or below the directory /tmp and delete them, processing filenames in such a way that file  or directory names containing spaces or newlines are correctly handled.
回复 支持 反对

使用道具 举报

发表于 2008-6-3 15:33:13 | 显示全部楼层
文档的内容应该是最全的

Because Unix filenames can contain blanks and newlines, this default behaviour is often  problematic;  filenames  containing  blanks  and/or newlines are incorrectly processed by xargs.  In these situations it is better to use the ‘-0’option, which prevents such problems.   When using this option you will need to ensure that the program which produces the input for xargs also uses a null character as a separator.  If that program is GNU find for example, the ‘-print0’option does this for you.
回复 支持 反对

使用道具 举报

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

本版积分规则

快速回复 返回顶部 返回列表