查找当前目录中的所有可写文件

我想快速识别目录中的所有可写文件。 什么是快速的方法来做到这一点?

find -type f -maxdepth 1 -writable 

-writable选项将查找当前用户可写入的文件。 如果您想查找任何人可以写入的文件(甚至是其他组合),可以使用-perm选项:

 find -maxdepth 1 -type f -perm /222 

这将找到他们的所有者(可能是谁)可写的文件:

 find -maxdepth 1 -type f -perm /200 

可以使用各种字符来控制模式参数的含义:

  • / – 任何权限位
  • - – 所有位( -222将意味着所有用户,组和其他)
  • 没有前缀 – 确切的规范( 222将意味着除了写以外没有其他的许可)

如果你在使用shell

 find . -maxdepth 1 -type f -writable 

看到男人找到

你会发现你在superuser.com或serverfault.com上得到这类问题的更好的答案

如果你正在编写的代码不仅仅是使用shell,你可能会对access(2)系统调用感兴趣。

这个问题已经在serverfault上提出

编辑:@ ghostdog74问你是否删除这个文件的写权限,如果这仍然会找到该文件。 答案是,不,这只能找到可写的文件。

 dwaters@eirene ~/temp $ cd temp dwaters@eirene ~/temp/temp $ ls dwaters@eirene ~/temp/temp $ touch newfile dwaters@eirene ~/temp/temp $ ls -alph total 0 drwxr-xr-x+ 2 dwaters Domain Users 0 Mar 22 13:27 ./ drwxrwxrwx+ 3 dwaters Domain Users 0 Mar 22 13:26 ../ -rw-r--r-- 1 dwaters Domain Users 0 Mar 22 13:27 newfile dwaters@eirene ~/temp/temp $ find . -maxdepth 1 -type f -writable ./newfile dwaters@eirene ~/temp/temp $ chmod 000 newfile dwaters@eirene ~/temp/temp $ ls -alph total 0 drwxr-xr-x+ 2 dwaters Domain Users 0 Mar 22 13:27 ./ drwxrwxrwx+ 3 dwaters Domain Users 0 Mar 22 13:26 ../ ---------- 1 dwaters Domain Users 0 Mar 22 13:27 newfile dwaters@eirene ~/temp/temp $ find . -maxdepth 1 -type f -writable dwaters@eirene ~/temp/temp 

无论所有者,组或其他人如何查找可写文件,都可以在ls的文件权限列中查看w标志。

 ls -l | awk '$1 ~ /^.*w.*/' 

$ 1是第一个字段(即ls -l的权限块),正则表达式只是在字段1中找到字母“w”。 就这样。

如果你想找到所有者写入权限

 ls -l | awk '$1 ~ /^..w/' 

如果你想找到组写权限

 ls -l | awk '$1 ~ /^.....w/' 

如果你想找别人写权限

 ls -l | awk '$1 ~ /w.$/' 

-f将测试一个文件

-w将测试它是否可写

例:

 $ for f in *; do [ -f $f ] && [ -w $f ] && echo $f; done 
 for var in `ls` do if [ -f $var -a -w $var ] then echo "$var having write permission"; else echo "$var not having write permission"; fi done 
 stat -c "%A->%n" *| sed -n '/^.*w.*/p' 

find -writable的问题是它不可移植,使用便携式find操作符来正确模拟并不容易。 如果您的find版本没有,您可以使用touch来检查文件是否可以写入,使用-r确保(几乎)不修改文件:

 find . -type f | while read f; do touch -r "$f" "$f" && echo "File $f is writable"; done 

touch-r选项在POSIX中,所以可以认为是可移植的。 当然,这会比find -writable效率低find -writable

请注意, touch -r 更新每个文件的ctime (上次更改为其元数据的时间),但是很少关心ctime。

我知道这是一个非常古老的线程,但是…

下面的命令帮助我: find . -type f -perm /+w find . -type f -perm /+w

您可以根据您要搜索的目录下多少个级别使用-maxdepth。 我正在使用Linux 2.6.18-371.4.1.el5。

如果你想找到所有可以被apache写的文件,那么你可以这样做:

 sudo su www-data find . -writable 2>/dev/null 

将www-data替换为nobody或apache或任何您的web用户。