如何检查任何文件或目录是否不属于';根';通过unix还是shell脚本?
我有一个目录。让我们开始新的一天如何检查任何文件或目录是否不属于';根';通过unix还是shell脚本?,shell,unix,ls,Shell,Unix,Ls,我有一个目录。让我们开始新的一天/data01。它有许多目录、子目录、文件等。我想检查目录及其所有子目录和文件,直到深度级别,它不属于root。我想要一个命令或shell脚本,可以保证任何深度/最后一级的子目录或文件都不属于root。我使用以下命令进行检查- ls -lrt /data01/ ls -lrt /data01/* 但不知怎的失败了(我不知道怎么做),一些文件被交叉检查的人发现是root。需要帮助 find /data01 -maxdepth $n -not -user ro
/data01
。它有许多目录、子目录、文件等。我想检查目录及其所有子目录和文件,直到深度级别,它不属于root
。我想要一个命令或shell脚本,可以保证任何深度/最后一级的子目录或文件都不属于root。我使用以下命令进行检查-
ls -lrt /data01/
ls -lrt /data01/*
但不知怎的失败了(我不知道怎么做),一些文件被交叉检查的人发现是root。需要帮助
find /data01 -maxdepth $n -not -user root
使用find
命令,上面的选项和参数都是自我解释的,只需小心将n
分配到所需的深度即可
如果要搜索到最后一级,只需删除-maxdepth$n
如果您想探索更多功能,请使用man find
使用find
命令,上面的选项和参数都是自我解释的,只需小心将n
分配到所需的深度即可
如果要搜索到最后一级,只需删除-maxdepth$n
如果您想探索更多功能,请使用man find
选项1:
最简单和更好的选择是使用find
,如下所示
find /data01 -not -user <user> # Prints the out in console
find /data01 -not -user <user> > Output # Redirect the output to file Output
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
选项1:
最简单和更好的选择是使用find
,如下所示
find /data01 -not -user <user> # Prints the out in console
find /data01 -not -user <user> > Output # Redirect the output to file Output
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
$n在这里做什么?@basari66这是您需要指定的最大深度数,假设您指定为1,它不会探索任何子目录。我改进了我的回答,所以您的意思是我应该使用find/data01-user root来查找所有权为root的文件。是吗?@basari66是的-不只是否定。但是你说的是
文件
。实际上,它将是dirs
和files
这$n在这里做什么?@basari66这是您需要指定的最大深度数,假设您指定为1,它将不会探索任何子目录。我改进了我的回答,所以您的意思是我应该使用find/data01-user root来查找所有权为root的文件。是吗?@basari66是的-不只是否定。但是你说的是文件
。实际上,它将是dirs
和文件