AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 35076
Accepted
mlambie
mlambie
Asked: 2009-07-03 06:58:53 +0800 CST2009-07-03 06:58:53 +0800 CST 2009-07-03 06:58:53 +0800 CST

需要修复用户主目录中的文件权限

  • 772

有没有人有工具或脚本可以递归地更正目录上的文件权限?

在 Ubuntu Linux 机器上,一堆文件被错误地复制到具有完整 777 权限(用户、组、其他 - 读取、写入、执行)的 USB 磁盘。我想把它们放回更正的用户目录中。

目录应该是 775,所有其他文件可以是 664。所有文件都是图像、文档或 MP3,所以它们都不需要是可执行的。如果设置了目录位,则需要执行,否则只需要用户和组,读取和写入。

我认为在编写 shell 脚本之前检查是否存在这样的实用程序是值得的 :)

linux ubuntu filesystems permissions shell
  • 9 9 个回答
  • 61534 Views

9 个回答

  • Voted
  1. Best Answer
    freiheit
    2009-07-03T07:04:31+08:002009-07-03T07:04:31+08:00

    这应该可以解决问题:

    find /home/user -type d -print0 | xargs -0 chmod 0775
    find /home/user -type f -print0 | xargs -0 chmod 0664
    
    • 74
  2. ThorstenS
    2009-07-03T07:14:07+08:002009-07-03T07:14:07+08:00

    find 可以单独使用 -exec 来解决问题:

    find /home/user -type f -exec chmod 0664 {} \;
    find /home/user -type d -exec chmod 0775 {} \;
    

    防止 find 为每个条目生成一个 chmod:

    find /home/user -type f -exec chmod 0664 {} +
    find /home/user -type d -exec chmod 0775 {} +
    

    (这有效地调用 chmod 一次,将所有文件的列表作为参数,而不是每个文件一个 chmod)

    • 21
  3. David Pashley
    2009-07-03T07:42:15+08:002009-07-03T07:42:15+08:00

    此答案不会解决您的问题,但有人可能会发现它对于文件权限低于应有权限的类似问题很有用。

    # chmod -R . u=rwX,g=rX,o=rX
    

    神奇的是 X 权限,而不是 x。chmod 手册页是这样描述的:

    仅当文件是目录或已对某些用户具有执行权限时才执行/搜索

    这不适合您的情况,因为您的文件具有执行权限,因此将匹配第二个测试。

    • 8
  4. noabody
    2018-07-22T14:37:01+08:002018-07-22T14:37:01+08:00

    前几天我做了一个非常简单的 bash 脚本,因为我需要修复权限。为什么没有正式的实用程序来重置基本、非根、文件和文件夹权限?

    该脚本使用查找 755 个所有文件夹和 644 个库。然后它使用 readelf 测试每个文件以查看它是否具有二进制 elf 标头。如果不是,它会在前两个字符中扫描 shebang #!。在检查文件是否已经具有匹配权限之后,它会 755 和 644 其他所有实例。

    特殊情况会处理一个例外,例如*.bak要忽略的文件。

    #!/bin/bash
    read -r -p "Correct file and folder permissions? [y/N] " chse
    if [[ "$chse" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
      echo "Processing ..."
      find -H $(pwd) -type d -exec chmod 0755 {} \;
      # set dirs to 755
      find -H $(pwd) -type f \( -iname '*.so.*' -o -iname '*.so' \) -exec chmod 0644 {} \;
      # libs
      IFS=$'\n'
      for value in $(find -H $(pwd) -type f ! \( -iname '*.so.*' -o -iname '*.so' -o -iname '*.bak' \) -printf '%p\n'); do
        tstbin=$(readelf -l "$value" 2>/dev/null | grep -Pio 'executable|shared')
        if [ -z "$tstbin" ]; then
          tstbat=$(cat "$value" | head -c2 | grep -io '#!')
          if [ -n "$tstbat" ]; then
            perm=$(stat -c '%a' "$value")
            if [ "$perm" != "755" ]; then
              chmod 755 $value
              echo "Set script  755 $value"
              # set batch to 755
            fi
          else
            perm=$(stat -c '%a' "$value")
            if [ "$perm" != "644" ]; then
              chmod 644 $value
              echo "Set regular 644 $value"
              # set regular files to 644
            fi
          fi
          # above aren't elf binary
        else
          perm=$(stat -c '%a' "$value")
          if [ "$perm" != "755" ]; then
            chmod 755 $value
            echo "Set binary  755 $value"
            # set elf binaries to 755
          fi
        fi
      done
      unset IFS
      # process linux permissions for files and folders
    else
      echo "Aborted."
    fi
    
    • 2
  5. Tombart
    2016-11-13T06:25:20+08:002016-11-13T06:25:20+08:00

    如果您使用 ssh,最好不要修改~/.ssh权限。

    DIR=/home/user
    find $DIR -type d -not -path "$DIR/.ssh" -print0 | xargs -0 chmod 0775
    find $DIR -type f -not -path "$DIR/.ssh/*" -print0 | xargs -0 chmod 0664
    
    • 1
  6. Marcs
    2017-01-25T10:07:30+08:002017-01-25T10:07:30+08:00

    我根据 freiheit 的解决方案制作了一个脚本,它添加了基本的参数检查。

    #!/bin/sh
    
    if [ $# -lt 1 ]; then
        echo "USAGE: $0 <path>"
        exit 1
    fi
    
    find $1 -type d -print0 | xargs -0 chmod 0755
    find $1 -type f -print0 | xargs -0 chmod 0644
    
    • 1
  7. chutz
    2019-03-22T15:08:48+08:002019-03-22T15:08:48+08:00

    另一种不完全回答原始请求但更可能是 OP 意图的替代方法是象征性地指定权限。例如,我假设 OP 想要“删除公众的写访问权限,并从所有文件中删除可执行权限”。

    如果您使用符号权限表示,这是可能的。

    • ow 是“从公众中删除写访问”(“o” - 其他,“-” - 删除,“w” - 写)
    • ax 是“仅在文件上为每个人删除执行”(“a” - 全部,“-” - 删除,“x” - 执行)

    以这种方式写出来而不是显式设置“0664”,您可以避免意外地允许对以前锁定的文件的额外权限。例如,如果一个文件是 0770,它不会错误地变成 0664。

    要完成第一个要求,您可以使用chmod:

    chmod --recursive o-w $dir
    

    做第二个要求:

    find $dir -type f -exec chmod a-x {} +
    

    或者两者都做:

    find $dir -type f -exec chmod a-x,o-w {} +
    find $dir -type d -exec chmod o-w {} +
    
    • 0
  8. TheKitoInc
    2021-01-06T09:02:56+08:002021-01-06T09:02:56+08:00

    我使用这个脚本 https://github.com/TheKito/Scripts/blob/master/FixHomesDirectoryPermissions.sh

    #!/bin/bash
    HME=/home
    for i in $(ls "$HME");
    do
            if [ -d "$HME/$i" ]; then
                    echo "$i";
                    chown -R $i "$HME/$i"
                    chgrp -R $i "$HME/$i"
                    find "$HME/$i" -type d -exec chmod 0770 "{}" \;
                    find "$HME/$i" -type f -exec chmod 0660 "{}" \;
            fi
    done;
    
    • 0
  9. K. Donovan
    2019-03-22T11:17:36+08:002019-03-22T11:17:36+08:00

    我找到了一个简化的 C-shell 解决方案。这可能不完全是万无一失的,但应该适用于大多数目录。它假定 Unix 'file' 命令有效,并找到 exec 文件以将它们重置为 exec 权限:

    find /home/user -type d | xargs chmod 0775
    find /home/user -type f | xargs -0 chmod 0664
    find /home/user -type f -exec file {} \; | grep executable | cut -d":" -f1 | xargs chmod 0775
    
    • -1

相关问题

  • 更改 PHP 的默认配置设置?

  • 保护新的 Ubuntu 服务器 [关闭]

  • (软)Ubuntu 7.10 上的 RAID 6,我应该迁移到 8.10 吗?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    从 IP 地址解析主机名

    • 8 个回答
  • Marko Smith

    如何按大小对 du -h 输出进行排序

    • 30 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    Windows 中执行反向 DNS 查找的命令行实用程序是什么?

    • 14 个回答
  • Marko Smith

    如何检查 Windows 机器上的端口是否被阻塞?

    • 4 个回答
  • Marko Smith

    我应该打开哪个端口以允许远程桌面?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    MikeN 在 Nginx 中,如何在维护子域的同时将所有 http 请求重写为 https? 2009-09-22 06:04:43 +0800 CST
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    0x89 bash中的双方括号和单方括号有什么区别? 2009-08-10 13:11:51 +0800 CST
  • Martin Hope
    kch 如何更改我的私钥密码? 2009-08-06 21:37:57 +0800 CST
  • Martin Hope
    Kyle Brandt IPv4 子网如何工作? 2009-08-05 06:05:31 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve