joh*_*345 1 shell bash shell-script
如何计算给定目录中当前用户具有读权限和写权限的文件数?
我从:
echo "whats the directory you want to check ?"
read dir
Run Code Online (Sandbox Code Playgroud)
不确定我应该使用find命令吗?
您需要要求root获取文件列表(对于位于您可以访问但无法读取的目录下的文件),然后检查权限:
sudo find "$dir" -print0|perl -Mfiletest=access -l -0ne'++$n if-r&&-w}{print+$n'
Run Code Online (Sandbox Code Playgroud)
如果您不关心非可读目录下的文件(但您仍然可以读写),请使用 GNU find:
find "$dir" -writable -readable -printf . | wc -c
Run Code Online (Sandbox Code Playgroud)
请注意,两者都检查访问权限(包括目录的每种类型的文件),它不仅基于权限。它应该为您提供在读+写模式下成功打开的文件数(无需创建)。例如,对于权限为 rwxrwxrwx 的符号链接,它只报告那些指向您具有读写权限的文件的符号链接。