Bash 脚本:递归更改文件权限
Bash Script: Changing file permissions recursively
我需要一个 Bash 脚本来更改目录和所有子目录中所有文件的文件权限。它应该像这样:
for each file in directory (and subdirectories)
if i am the owner of the file
if it is a directory
chmod 770 file
else
chmod 660 file
我想这不是一项艰巨的任务,但我在 Bash 脚本方面不是很有经验。感谢您的帮助! :D
您可以调用两次 find
命令,使用 -user
选项按用户过滤,-type
选项按文件类型过滤:
find . -user "$USER" -type d -exec echo chmod 770 {} +
find . -user "$USER" -not -type d -exec echo chmod 660 {} +
测试后删除 echo
,以实际更改权限。
find
在这里很有用:它递归地查找满足特定条件(在本例中为所有者)的文件 and/or 目录。另一个技巧是对 chmod
使用 X
(而不是 x
)标志,这使得目录可执行但不是常规文件。通过 xargs
:
将它们放在一起
find . -user $(whoami) | xargs chmod ug=Xo=
我没有测试这个,它可能有点不对。我建议先测试它:)
使用find
:
find topdirectory -user "$USER" \( -type f -exec chmod 660 {} + \) -o \( -type f -exec chmod 770 {} + \)
我需要一个 Bash 脚本来更改目录和所有子目录中所有文件的文件权限。它应该像这样:
for each file in directory (and subdirectories)
if i am the owner of the file
if it is a directory
chmod 770 file
else
chmod 660 file
我想这不是一项艰巨的任务,但我在 Bash 脚本方面不是很有经验。感谢您的帮助! :D
您可以调用两次 find
命令,使用 -user
选项按用户过滤,-type
选项按文件类型过滤:
find . -user "$USER" -type d -exec echo chmod 770 {} +
find . -user "$USER" -not -type d -exec echo chmod 660 {} +
测试后删除 echo
,以实际更改权限。
find
在这里很有用:它递归地查找满足特定条件(在本例中为所有者)的文件 and/or 目录。另一个技巧是对 chmod
使用 X
(而不是 x
)标志,这使得目录可执行但不是常规文件。通过 xargs
:
find . -user $(whoami) | xargs chmod ug=Xo=
我没有测试这个,它可能有点不对。我建议先测试它:)
使用find
:
find topdirectory -user "$USER" \( -type f -exec chmod 660 {} + \) -o \( -type f -exec chmod 770 {} + \)