Linux 如何将特定文件模式的文件权限更改为当前目录的子文件夹?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4249878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I change file permission to a certain file pattern to sub folders of my current directory?
提问by ivanceras
Using chmod
, I do chmod +x *.sh
in the current directory but what if I want to change all files including files within subfolders that has an sh file extension?.
使用chmod
,我chmod +x *.sh
在当前目录中执行但如果我想更改所有文件,包括具有 sh 文件扩展名的子文件夹中的文件怎么办?
chmod +x -R *
will work but I need something more like chmod +x -R *.sh
chmod +x -R *
会工作,但我需要更像 chmod +x -R *.sh
采纳答案by ennuikiller
use find:
使用查找:
find . -name "*.sh" -exec chmod +x {} \;
回答by Orbling
Try using the glorious combination of find with xargs.
尝试使用 find 和 xargs 的完美结合。
find . -iname \*.sh -print0 | xargs -r0 chmod +x
The .
is the directory to start in, in this case the working directory.
的.
是,在这种情况下,工作目录开始的目录。
回答by chappjc
With modern versions of find
, you get the benefits of an xargs
approach that avoids multiple calls to the command (chmod
). The command is only slightly different.
使用现代版本的find
,您可以获得xargs
避免多次调用命令 ( chmod
)的方法的好处。命令只是略有不同。
find . -name "*.sh" -exec chmod +x {} +
find . -name "*.sh" -exec chmod +x {} +
Snip from find
docson Arch 2015.09.01 (emphasisadded by me):
摘自Arch 2015.09.01 上的find
文档(我添加了重点):
-exec command {} +
This variant of the
-exec
action runs the specified command on the selected files, but the command line is built by appending each selected file name at the end; the total number of invocations of the command will be much less than the number of matched files. The command line is built in much the same way thatxargs
builds its command lines. Only one instance of{}
is allowed within the command. The command is executed in the starting directory.
-exec command {} +
这个
-exec
动作的变体在选定的文件上运行指定的命令,但命令行是通过在末尾附加每个选定的文件名来构建的;命令的总调用次数将远小于匹配文件的数量。命令行的构建方式与xargs
构建其命令行的方式大致相同。命令中只{}
允许一个实例。该命令在起始目录中执行。