简体   繁体   English

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

[英]Find all writable files in the current directory

I want to quickly identify all writable files in the directory. 我想快速识别目录中的所有可写文件。 What is the quick way to do it? 快速的方法是什么?

find -type f -maxdepth 1 -writable

The -writable option will find files that are writable by the current user. -writable选项将查找当前用户可写的文件。 If you'd like to find files that are writable by anyone (or even other combinations), you can use the -perm option: 如果您想查找任何人(甚至其他组合)可写的文件,您可以使用-perm选项:

find -maxdepth 1 -type f -perm /222

This will find files that are writable by their owner (whoever that may be): 这将找到其所有者(无论是谁)可写的文件:

find -maxdepth 1 -type f -perm /200

Various characters can be used to control the meaning of the mode argument: 可以使用各种字符来控制mode参数的含义:

  • / - any permission bit / - 任何权限位
  • - - all bits ( -222 would mean all - user, group and other) - - 所有位( -222表示全部 - 用户,组和其他)
  • no prefix - exact specification ( 222 would mean no permssions other than write) 没有前缀 - 确切的规范( 222意味着除写之外没有任何权利)

to find writable files regardless of owner, group or others, you can check the w flag in the file permission column of ls. 要查找可写文件而不管所有者,组或其他人,您可以在ls的文件权限列中检查w标志。

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

$1 is the first field, (ie the permission block of ls -l) , the regular expression just say find the letter "w" in field one. $ 1是第一个字段,(即ls -l的权限块),正则表达式只是在字段1中找到字母“w”。 that's all. 就这样。

if you want to find owner write permission 如果你想找到所有者写入权限

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

if you want to find group write permission 如果你想找到组写权限

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

if you want to find others write permission 如果你想找别人的写权限

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

-f will test for a file -f将测试文件

-w will test whether it's writeable -w将测试它是否可写

Example: 例:

$ for f in *; do [ -f $f ] && [ -w $f ] && echo $f; done

If you are in shell use 如果你在shell中使用

find .  -maxdepth 1 -type f -writable

see man find 见男人找

You will find you get better answers for this type of question on superuser.com or serverfault.com 您会在superuser.com或serverfault.com上找到更好的答案

If you are writing code not just using shell you may be interested in the access(2) system call. 如果您正在编写代码而不仅仅是使用shell,那么您可能对access(2)系统调用感兴趣。

This question has already been asked on serverfault serverfault上已经提出过这个问题

EDIT: @ghostdog74 asked if you removed write permissions for this file if this would still find the file. 编辑:@ ghostdog74询问您是否删除了此文件的写入权限,如果仍然找到该文件。 The answer, no this only finds files that are writable. 答案,不,这只能找到可写的文件。

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

The problem with find -writable is that it's not portable and it's not easy to emulate correctly with portable find operators. find -writable的问题在于它不可移植,并且使用便携式find运算符进行正确模拟并不容易。 If your version of find doesn't have it, you can use touch to check if the file can be written to, using -r to make sure you (almost) don't modify the file: 如果您的find版本没有它,您可以使用touch来检查是否可以写入文件,使用-r确保您(几乎)不修改文件:

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

The -r option for touch is in POSIX, so it can be considered portable. touch-r选项在POSIX中,因此可以认为是可移植的。 Of course, this will be much less efficient than find -writable . 当然,这比find -writable效率低find -writable

Note that touch -r will update each file's ctime (time of last change to its meta-data), but one rarely cares about ctime anyway. 请注意, touch -r 更新每个文件的ctime (上次更改其元数据的时间),但无论如何都很少关心ctime。

If you want to find all files that are writable by apache etal then you can do this: 如果要查找apache etal可写的所有文件,则可以执行以下操作:

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

Replace www-data with nobody or apache or whatever your web user is. 用nobody或apache或任何你的网络用户替换www-data。

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'

I know this a very old thread, however... 我知道这是一个非常古老的线索,但是......

The below command helped me: find . -type f -perm /+w 以下命令帮助我: find . -type f -perm /+w find . -type f -perm /+w

You can use -maxdepth based on how many levels below directory you want to search. 您可以根据要搜索的目录下面的级别使用-maxdepth。 I am using Linux 2.6.18-371.4.1.el5. 我使用的是Linux 2.6.18-371.4.1.el5。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM