bash 为什么 chmod 上的递归模式除了递归之外什么都做?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3472383/
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
Why does recursive mode on chmod do everything but recursion?
提问by Razor Storm
chmod -R 775 *.cgionly changes permissions on the files in the current directory, the files in the subdirectory don't get modified at all. This is the exact same functionality as simply doing chmod 775 *.cgi. I have seen people use solutions such as with findand whatnot. Ok great, but why does -R mode even exist if it doesn't even accomplish anything?
chmod -R 775 *.cgi只更改当前目录中文件的权限,子目录中的文件根本不会被修改。这与简单地执行完全相同的功能chmod 775 *.cgi。我见过人们使用诸如 withfind和诸如此类的解决方案。好的,但是如果 -R 模式甚至没有完成任何事情,为什么它甚至存在?
回答by msw
Probably because you have no directories named *.cgi. Quoth the manual:
可能是因为您没有名为*.cgi. 引用手册:
-RRecursively change file mode bits. For each file operand that names a directory, chmod shall change the file mode bits of the directory and all files in the file hierarchy below it.
-R递归更改文件模式位。对于命名目录的每个文件操作数,chmod 将更改该目录的文件模式位以及它下面的文件层次结构中的所有文件。
For example:
例如:
$ ls -R
.:
a a.c b.c c.c
./a:
a.c b.c sub
./a/sub:
a.c b.c
$ chmod -R 444 *.c
$ ls -l
drwxr-xr-x 3 msw msw 4096 2010-08-12 18:07 a
-r--r--r-- 1 msw msw 0 2010-08-12 18:07 a.c
-r--r--r-- 1 msw msw 0 2010-08-12 18:07 b.c
-r--r--r-- 1 msw msw 0 2010-08-12 18:07 c.c
$ : directory a not affected
$ chmod -R u-w a
$ ls -l a
-r-xr-xr-x 1 msw msw 0 2010-08-12 18:07 a.c
-r-xr-xr-x 1 msw msw 0 2010-08-12 18:07 b.c
dr-xr-xr-x 3 msw msw 4096 2010-08-12 18:07 sub
$ ls -l a/sub
-r-xr-xr-x 1 msw msw 0 2010-08-12 18:07 a.c
-r-xr-xr-x 1 msw msw 0 2010-08-12 18:07 b.c
$ : got em
回答by Matthew Slattery
-Rtells chmodto recurse into any directories that are given as arguments.
-R告诉chmod递归到作为参数给出的任何目录。
If you say chmod -R 775 *.cgi, the shell will expand *.cgiinto a list of files which match that pattern, and pass that as the list of arguments - so chmodisn't being asked to look into any other directories.
如果您说chmod -R 775 *.cgi,shell 将扩展*.cgi为与该模式匹配的文件列表,并将其作为参数列表传递 - 因此chmod不会被要求查看任何其他目录。
(It willrecurse into any directorywhich matches *.cgi...)
(这将递归到任何目录相匹配*.cgi...)
回答by dmckee --- ex-moderator kitten
To accomplish what you want try
完成你想要的尝试
find . -name '*.cgi' -print0 | xargs -0 chmod 755
Here findgenerates a list of all the file with a .cgiending from the current directory downward, and passes that list to xargswhich applies chmodto each one.
这里find生成一个包含.cgi从当前目录向下结尾的所有文件的列表,并将该列表传递给xargs适用chmod于每个文件的列表。
回答by sth
*.cgiis expanded by your shell to a list of all the file names in the current directory ending in .cgi. Then the shell calls chmodwith this list of filenames.
*.cgi由您的 shell 扩展为当前目录中所有以.cgi. 然后 shellchmod使用这个文件名列表调用。
chmodlooks at all those file names it got from the shell, changes there modes and would recurse if some of them were directories. But probably none of them are, so there is nothing to recurse.
chmod查看它从 shell 获得的所有文件名,更改那里的模式,如果其中一些是目录,则会递归。但可能它们都不是,所以没有什么可以递归的。
To find all cgi files in the current directory and its subdirectories, and run chmodon them, you could do:
要查找当前目录及其子目录中的所有 cgi 文件并运行chmod它们,您可以执行以下操作:
find . -name '*.cgi' -print0 | xargs -0 chmod 775
回答by tamasd
*is a shell builtin, while -R is a command line option. So chmodwill never get *as an argument.
*是一个内置的 shell,而 -R 是一个命令行选项。所以chmod永远不会得到*作为一个论点。
Put the case that foo0.cgi and foo1.cgi are the contents of the directory. If you type chmod -R o+r *.cgithen chmodwill get the '-R', 'o+r', 'foo0.cgi' and 'foo1.cgi' as arguments.
假设foo0.cgi 和foo1.cgi 是目录的内容。如果您键入chmod -R o+r *.cgi然后chmod将得到“-R”,“O + R”,“foo0.cgi”和“foo1.cgi”作为参数。
What you want to do can be accomplished easily:
您想做的事情很容易完成:
find . -iname '*.cgi' | xargs chmod 755

