如何将文件权限更改为特定的文件模式到当前目录的子文件夹?

时间:2022-07-18 07:09:44

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 +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

3 个解决方案

#1


47  

use find:

使用找到:

find . -name "*.sh" -exec chmod +x {} \;

#2


7  

Try using the glorious combination of find with xargs.

尝试使用与xargs的光荣组合。

find . -iname \*.sh | xargs chmod +x

The . is the directory to start in, in this case the working directory.

的。是要开始的目录,在本例中是工作目录。

#3


2  

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 {} +

找到。- name”*。-exec chmod +x {} +

Snip from find docs on Arch 2015.09.01 (emphasis added by me):

来自于Arch 2015.09.01的find docs(我的强调):

-exec command {} +

{ } + - exec命令

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 that xargs builds its command lines. Only one instance of {} is allowed within the command. The command is executed in the starting directory.

这个-exec操作的变体在选定的文件上运行指定的命令,但是命令行是通过在末尾附加每个选定的文件名来构建的;命令调用的总数将比匹配文件的数量少得多。命令行构建的方式与xargs构建它的命令行非常相似。在命令中只允许有一个{}实例。该命令在开始目录中执行。

#1


47  

use find:

使用找到:

find . -name "*.sh" -exec chmod +x {} \;

#2


7  

Try using the glorious combination of find with xargs.

尝试使用与xargs的光荣组合。

find . -iname \*.sh | xargs chmod +x

The . is the directory to start in, in this case the working directory.

的。是要开始的目录,在本例中是工作目录。

#3


2  

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 {} +

找到。- name”*。-exec chmod +x {} +

Snip from find docs on Arch 2015.09.01 (emphasis added by me):

来自于Arch 2015.09.01的find docs(我的强调):

-exec command {} +

{ } + - exec命令

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 that xargs builds its command lines. Only one instance of {} is allowed within the command. The command is executed in the starting directory.

这个-exec操作的变体在选定的文件上运行指定的命令,但是命令行是通过在末尾附加每个选定的文件名来构建的;命令调用的总数将比匹配文件的数量少得多。命令行构建的方式与xargs构建它的命令行非常相似。在命令中只允许有一个{}实例。该命令在开始目录中执行。