bash 循环更改权限

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11486249/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:45:40  来源:igfitidea点击:

bash loop to change permissions

bash

提问by Jeff

I'm trying to change all the permissions for the files in my ~/Documents folder and I thought the following little loop would work, but it doesn't seem to be working. Can anyone help me? Here's the loop:

我正在尝试更改 ~/Documents 文件夹中文件的所有权限,我认为以下小循环会起作用,但似乎不起作用。谁能帮我?这是循环:

files=~/Documents/*
for $file in $files {
   chmod 755 $file }

I'm trying to write this directly into the bash command line gives me the following error:

我试图将其直接写入 bash 命令行,但出现以下错误:

-bash: syntax error near unexpected token `chmod'

thanks for your help/advice

感谢您的帮助/建议

采纳答案by another.anon.coward

The loop should be :

循环应该是:

for file in $files
do
   test -f "$file" && chmod 755 "$file"
done  

Please note that you are relying on globbingwhen you say files=~/Documents/*which will include the directories under ~/Documentsas well, thus using the loop will change permissions of the directories as well. You can add a simple test to make sure that you update only the permission of files. Also please note that globbing can be turned off with set -fin which case fileswill have value of just <HOME_DIR>/*which maynot be a filename; which also will not serve your purpose.

请注意,当您说which 将包含其下的目录时,您依赖于globbing,因此使用循环也会更改目录的权限。您可以添加一个简单的测试来确保您只更新文件的权限。另请注意,可以关闭通配符,在这种情况下,它的值为 just which 可能不是文件名;这也不会达到您的目的。 files=~/Documents/*~/Documentsset -ffiles<HOME_DIR>/*

More importantly, as pointed by ghotiin the commentsit is not a good ideato rely on globbing. When you make use of globbing the filenames with hypens can have undefined behaviour & filenames with semi-colons can cause security vulnerabilities.Apart for security vulnerabilities associated with globbing there are somecommonpitfallsassociated with it as well. Please take a look at the answer provided by ghotiwhich highlights some of risks involved with your current operations.

更重要的是,通过指向ghoti评论它是不是一个好主意依靠通配符。当您使用带连字符的文件名时,可能会出现未定义的行为,而带分号的文件名可能会导致安全漏洞。除了与 globbing 相关的安全漏洞之外,还有一些常见的陷阱与之相关。请查看ghti 提供答案,其中重点介绍了与您当前运营相关的一些风险。

You could use findas well (This will recursive set for all the files):
find ~/Documents/ -type f -exec chmod 755 {} \;
For setting permissions only for the files under ~/Documents/you can make use of -maxdepthoption as such:
find ~/Documents/ -maxdepth 1 -type f -exec chmod 755 {} \;

您也可以使用find(这将对所有文件进行递归设置):
find ~/Documents/ -type f -exec chmod 755 {} \;
对于仅为下的文件设置权限,~/Documents/您可以使用如下-maxdepth选项:
find ~/Documents/ -maxdepth 1 -type f -exec chmod 755 {} \;

Hope this helps!

希望这可以帮助!

回答by ghoti

There are risks in making your documents executable. I recommend against it unless you know exactly what you're doing.

使您的文档可执行存在风险。我建议不要这样做,除非您确切地知道自己在做什么。

I'm going to take a wild guess that you don't reallywant to make all your Documents folder executable, and what you're really trying to do is standardize on user-writable everyone-else-readable permission for everything in that folder.

我会大胆地猜测您并不是真的想让所有的 Documents 文件夹都可以执行,而您真正想做的是标准化该文件夹中所有内容的用户可写的其他人可读的权限.

Note that (as someone mentioned in another answer) the chmodcommand has its own "recursive" option, -R. Note alsothat you can use "symbolic" permissions with chmod, you're not stuck with octal-only. So:

请注意(正如有人在另一个答案中提到的)该chmod命令有自己的“递归”选项,-R. 另请注意,您可以在 chmod 中使用“符号”权限,您不会被仅限于八进制。所以:

chmod -R go+r-wx ~/Documents/

will add Read and remove Write and eXecute functions from everything in and under ~/Documents/.

将从中和下的所有内容中添加读取和删除写入和执行函数~/Documents/

Note that this will make subdirectoriesreadable, but will not provide access to the files within them (because that's what the executable bit on a directory does). So you may want to use TWO commands:

请注意,这将使子目录可读,但不会提供对其中文件的访问(因为这是目录上的可执行位所做的)。所以你可能想使用两个命令:

find ~/Documents/ -type d -exec chmod 755 {} \;
find ~/Documents/ -type f -exec chmod 644 {} \;

The first line only runs chmodon directories, making them both readable and accessible. The second line affects only your files, making them readable by the world.

第一行只chmod在目录上运行,使它们既可读又可访问。第二行只影响你的文件,让全世界都可以读取它们。

回答by l'L'l

Try

尝试

find . -exec chmod 755 {} +\;

or

或者

find . -type f -exec chmod 755 {} +\;

回答by Thor

Why not:

为什么不:

chmod -R 755 ~/Documents