如何使目录下的所有文件在linux上可读?

Ror*_*ckh 28 linux bash ubuntu command-line chmod

我想让某个目录下的所有文件(和目录)都可以读取,而不必自己chmod每个文件.如果有一个选项也可以递归地执行此操作(看看文件夹和chmod 666下的所有文件)会很棒

小智 35

man 3 chmod 包含您要查找的信息.

chmod -R +r directory
Run Code Online (Sandbox Code Playgroud)

-R选项告诉chmod以递归方式操作.

  • Historicaly`-r`用于*递归*操作,`-R`用于*危险递归*.如果大写的`R`用于`chmod`和`chown`那是因为我们更喜欢使用比使用`find`更精确的操作.看到我的回答! (2认同)

F. *_*uri 9

由于目录可以包含链接和/或绑定挂载,因此使用find可以确保在做什么和不做什么时具有最精细的粒度....

find directory \( -type f -o -type d \) -print0 |
    xargs -0 chmod ugo+r
Run Code Online (Sandbox Code Playgroud)

要排除挂载点下的路径:

find directory -mount \( -type f -o -type d \) -print0 |
    xargs -0 chmod ugo+r
Run Code Online (Sandbox Code Playgroud)

要排除某些特定文件(.htaccess for sample):

find directory \( -type f -o -type d \) ! -name '.htaccess' -print0 |
    xargs -0 chmod ugo+r
Run Code Online (Sandbox Code Playgroud)