用于递归遍历目录并根据文件类型条件进行 chmodding 的 Shell 脚本

Tho*_*ard 8 command-line bash zsh shell-script

任何人都可以向我指出编写可以递归遍历整个目录结构的 shell 脚本的代码或教程(从当前工作目录开始,或者给出从哪里开始的必需参数)并且可以:

  1. 确定一个项目是否是一个目录,如果是,chmod 755它,或...
  2. 确定一个项目是否是一个文件(不是一个目录),以及chmod 644它。

我正在寻找与 Ubuntu、Debian、基于 RHEL 等的兼容性,所以我没有用任何特定的语言来标记它。我希望在 Bash 中使用它,但是如果您有 ZSH 脚本,那也可以。

gui*_*ido 10

我习惯了这个单行命令(会从当前工作目录开始递归)

find . -type d -exec chmod 0755 '{}' + -or -type f -exec chmod 0644 '{}' +
Run Code Online (Sandbox Code Playgroud)

解释:

find .                    # starting in curdir find   
-type d                   # any directory
-exec chmod 0755 '{}'     # and chmod it to 755
+                         # (variant of -exec look find man page)
-or                       # or
-type f                   # any file
-exec chmod 0644 '{}'     # and chmod it to 644
+                         # (as above)
Run Code Online (Sandbox Code Playgroud)