umask补习班
umask 是创建文件或目录时设置权限掩码的常见命令,通常为 0022(等价于 022)。按常规规则,目录基准权限是 777、文件基准权限是 666,减去 umask 022 后,新建目录为 755、文件为 644。文章用 7-zip 在 Windows 上创建 foo.zip 和 bar.tar 并上传到 Linux,通过 unzip 和 tar 解压来演示权限的差异。
unzip 解压后 foo_dir 为 755、foo_file 为 644,符合上述减法规则。而 tar 解压的结果则取决于执行身份:root 缺省 extract all protection information,会保留归档中记录的权限(bar_dir 和 bar_file 均为 777);非 root 缺省 apply user’s umask when extracting files instead of recorded permissions,但 tar 较为特殊,它以目录和文件本身的权限作为基准权限,而非 777(目录)和 666(文件),所以最终得到 755(777 - 022)。正因为文件来自 Windows,初始权限即为 777,才有了这个对比效果。最后还给出了批量修改权限的技巧:用 find /path -type d | xargs chmod 755 和 find /path -type f | xargs chmod 644 分别处理目录与文件。