【问题标题】:How to check if any file or directory is not owned by 'root' through unix or shell scripts?如何通过 unix 或 shell 脚本检查是否有任何文件或目录不属于“root”?
【发布时间】:2018-03-28 14:42:23
【问题描述】:

我有一个目录。让我们的一天/data01。它有许多目录、子目录、文件等。我想检查该目录及其所有子目录和文件是否达到深度级别,它不属于root。我想要一个命令或 shell 脚本,它可以保证任何子目录或文件直到深度/最后级别都不归 root 所有。我用以下命令检查 -

ls -lrt /data01/ 

ls -lrt /data01/* 

但这以某种方式失败了(我不知道如何),并且某些文件在交叉检查时以root身份出现。需要帮助!

【问题讨论】:

    标签: shell unix ls


    【解决方案1】:
    find /data01 -maxdepth $n -not -user root
    

    使用find 命令,上面的选项和参数是不言自明的,只需注意将n 分配给所需的深度。

    如果要搜索到最后一级,只需删除-maxdepth $n

    如果您想探索更多功能,请使用man find

    【讨论】:

    • $n 在这里做什么?
    • @basari66 这是您需要指定的最大深度数,假设您指定为1,它不会探索任何子目录
    • 我已经改进了我的答案
    • 所以你的意思是我应该使用 find /data01 -user root 来找出所有权为 root 的文件。对吗?
    • @basari66 是的。 -not 只是否定。但你说file。实际上它将是dirsfiles
    【解决方案2】:

    选项 1:

    最简单和更好的选择是使用find,如下所示,

    find /data01 -not -user <user>           # Prints the out in console
    find /data01 -not -user <user> > Output  # Redirect the output to file Output
    

    注意: 在您的情况下,将 &lt;user&gt; 替换为 root。如果您想通过子目录限制递归检查,请使用选项-maxdepth &lt;levels&gt;。根据手册的详细信息:在命令行参数下方的目录的大多数级别(非负整数)级别下降

    选项 2:

    如果你想使用ls,那么结合awk等其他命令来实现,示例如下,

    ls -l * -R | awk '{if($3=="root") print $0}' 
    
    # -R --> Recursively list through all sub directories
    # if($3=="root") --> checks the 3rd position(owner user) in the ls output is "root",
    # in your case use if($3!="root") , but you may need to grep out blank lines and directory names from the final output
    # print $0 --> Prints the entire line when if block is true
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-13
      • 1970-01-01
      • 2011-06-15
      • 2010-09-10
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 2011-04-29
      相关资源
      最近更新 更多