如何更改特定文件types的权限

file * | grep 'ASCII text' | chmod -x chmod: missing operand Try `chmod --help' for more information. 

上面的命令给我error.Basically我试图find所有types为ASCII的文件,并将其权限更改为-x。上述语法中有什么错误?

一: grep 'ASCII text' 不仅返回文件名,还返回文件本身的types; 您需要处理输出以返回文件名称

二: chmod不接受来自STDIN的参数,这就是你正在用pipe来做的事情 。 你必须使用xargs或者在for循环中包装上面的代码

这就是说,这里有两个解决scheme:

解决scheme1:使用pipe道

 file * | awk '/ASCII text/ {gsub(/:/,"",$1); print $1}' | xargs chmod -x 

解决scheme2:使用for-loop

 for fn in $(file * | awk '/ASCII text/ {gsub(/:/,"",$1); print $1}'); do chmod -x "$fn"; done 

select你的毒药:-)

这应该工作,无论文件名是否包含空格或冒号:

 find -maxdepth 1 -type f -exec sh -c 'file -b "{}" | grep -sq ASCII' \; -print0 | xargs -0 chmod -x 

您可以删除-maxdepth 1以使其recursion。

如果文件名本身包含string“ASCII”,则可能会有误报。

编辑:

我整合了pepoluan的build议使用-b选项的file所以文件名不输出为由greptesting。 这应该消除误报。

 for f in `file * | grep "ASCII text" | awk "{print \\$1}" | awk -F ":" "{print \\$1}"`; do chmod -x "$f"; done 

而另一个class轮 – 要知道需要去掉: ASCII Text并引用名字

 file * | grep 'ASCII text' | sed 's|\(^.*\):.*|\"\1\"|'| xargs chmod -x