Linux 递归地chmod
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13377606/
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
Chmod recursively
提问by
I have an archive, which is archived by someone else, and I want to automatically, after I download it, to change a branch of the file system within the extracted files to gain read access. (I can't change how archive is created).
我有一个存档,由其他人存档,我想在下载后自动更改提取文件中的文件系统分支以获得读取访问权限。(我无法更改存档的创建方式)。
I've looked into this thread: chmod: How to recursively add execute permissions only to files which already have execute permissionas into some others, but no joy.
我已经研究过这个线程:chmod:如何递归地将执行权限仅添加到已经具有执行权限的文件中,但没有乐趣。
The directories originally come with multiple but all wrong flags, they may appear as:
目录最初带有多个但都是错误的标志,它们可能显示为:
drwx------
d---r-x---
drwxrwxr-x
dr--r-xr--
Those are just the few I've discovered so far, but could be more.
这些只是我迄今为止发现的少数几个,但可能更多。
find
errors when tries to look into a directory with no x
permission, and so doesn't pass it to chmod
. What I've been doing so far, is manually change permissions on the parent directory, then go into the child directories and do the same for them and so on. But this is a lot of hand labour. Isn't there some way to do this automatically?
find
尝试在没有x
权限的情况下查看目录时出错,因此不会将其传递给chmod
. 到目前为止,我一直在做的是手动更改父目录的权限,然后进入子目录并对它们执行相同的操作等等。但这是大量的手工劳动。没有办法自动执行此操作吗?
I.e. how I am doing it now:
即我现在是如何做的:
do:
做:
$ chmod -R +x
$ chmod -R +r
until I get no errors, then
直到我没有错误,然后
$ find -type f -exec chmod -x {} +
But there must be a better way.
但必须有更好的方法。
采纳答案by Fred Foo
You need read access, in addition to execute access, to list a directory. If you only have execute access, then you can find out the names of entries in the directory, but no other information (not even types, so you don't know which of the entries are subdirectories). This works for me:
除了执行访问权限之外,您还需要读取访问权限才能列出目录。如果您只有执行访问权限,那么您可以找到目录中条目的名称,但没有其他信息(甚至没有类型,因此您不知道哪些条目是子目录)。这对我有用:
find . -type d -exec chmod +rx {} \;
回答by choroba
Try to change all the persmissions at the same time:
尝试同时更改所有权限:
chmod -R +xr
回答by Dmitry
You can use chmod with the X
mode letter (the capital X) to set the executable flag only for directories.
您可以使用带有X
模式字母(大写 X)的chmod来仅为目录设置可执行标志。
In the example below the executable flag is cleared and then set for all directories recursively:
在下面的示例中,可执行标志被清除,然后递归地为所有目录设置:
~$ mkdir foo
~$ mkdir foo/bar
~$ mkdir foo/baz
~$ touch foo/x
~$ touch foo/y
~$ chmod -R go-X foo
~$ ls -l foo
total 8
drwxrw-r-- 2 wq wq 4096 Nov 14 15:31 bar
drwxrw-r-- 2 wq wq 4096 Nov 14 15:31 baz
-rw-rw-r-- 1 wq wq 0 Nov 14 15:31 x
-rw-rw-r-- 1 wq wq 0 Nov 14 15:31 y
~$ chmod -R go+X foo
~$ ls -l foo
total 8
drwxrwxr-x 2 wq wq 4096 Nov 14 15:31 bar
drwxrwxr-x 2 wq wq 4096 Nov 14 15:31 baz
-rw-rw-r-- 1 wq wq 0 Nov 14 15:31 x
-rw-rw-r-- 1 wq wq 0 Nov 14 15:31 y
A bit of explaination:
一点解释:
chmod -x foo
- clear the eXecutableflag forfoo
chmod +x foo
- set the eXecutableflag forfoo
chmod go+x foo
- same as above, but set the flag only for Groupand Otherusers, don't touch the User(owner) permissionchmod go+X foo
- same as above, but apply only to directories, don't touch fileschmod -R go+X foo
- same as above, but do this Recursivelyfor all subdirectories offoo
chmod -x foo
- 清除可执行标志foo
chmod +x foo
- 设置可执行标志foo
chmod go+x foo
- 同上,但只为组和其他用户设置标志,不要触及用户(所有者)权限chmod go+X foo
- 同上,但只适用于目录,不要触及文件chmod -R go+X foo
- 与上面相同,但对所有子目录递归执行此操作foo
回答by Tim Jensen
To make everything writable by the owner, read/execute by the group, and world executable:
要使所有者可以写所有内容,由组读取/执行,以及全局可执行:
chmod -R 0755
To make everything wide open:
使一切都大开:
chmod -R 0777
回答by AlikElzin-kilaka
回答by Anthony Vinay
Give 0777 to all files and directories starting from the current path :
将 0777 赋予从当前路径开始的所有文件和目录:
chmod -R 0777 ./
chmod -R 0777 ./