更改文件而不是目录的 chmod [英] changing chmod for files but not directories
问题描述
我需要使用 chmod 将所有文件递归更改为 664.我想跳过文件夹.我正在考虑做这样的事情
I need to use chmod to change all files recursivly to 664. I would like to skip the folders. I was thinking of doing something like this
ls -lR | grep ^-r | chmod 664
这不起作用,我假设是因为我无法输入 chmod任何人都知道一种简单的方法来做到这一点?
This doesn't work, I'm assuming because I can't pipe into chmod Anyone know of an easy way to do this?
谢谢
推荐答案
find -exec
答案是一个很好的答案,但它有一个通常无关紧要的缺点,即它为每一个文件.然而,它的功能非常完美,并且只会在文件数量变得非常时表现不佳.使用 xargs
将在为该 组 文件运行子进程之前将文件名批处理成大组.
A find -exec
answer is a good one but it suffers from the usually irrelevant shortcoming that it creates a separate sub-process for every single file. However it's perfectly functional and will only perform badly when the number of files gets really large. Using xargs
will batch up the file names into large groups before running a sub-process for that group of files.
您只需要注意,在使用 xargs
时,您可以正确处理带有嵌入空格、换行符或其他特殊字符的文件名.
You just have to be careful that, in using xargs
, you properly handle filenames with embedded spaces, newlines or other special characters in them.
解决这两个问题的解决方案是(假设您有足够好的 find
和 xargs
实现):
A solution that solves both these problems is (assuming you have a decent enough find
and xargs
implementation):
find . -type f -print0 | xargs -0 chmod 644
-print0
导致 find
使用 NUL 字符(而不是空格)和 -0
终止其输出流上的文件名code> 到 xargs
让它知道它应该期望作为输入格式.
The -print0
causes find
to terminate the file names on its output stream with a NUL character (rather than a space) and the -0
to xargs
lets it know that it should expect that as the input format.
这篇关于更改文件而不是目录的 chmod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!