bash 查找当前目录下所有可写文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2491985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find all writable files in the current directory
提问by vehomzzz
I want to quickly identify all writable files in the directory. What is the quick way to do it?
我想快速识别目录中的所有可写文件。什么是快速的方法呢?
回答by matja
find -type f -maxdepth 1 -writable
回答by Paused until further notice.
The -writable
option will find files that are writable by the current user. If you'd like to find files that are writable by anyone (or even other combinations), you can use the -perm
option:
该-writable
选项将查找当前用户可写的文件。如果您想查找任何人(或什至其他组合)均可写的文件,您可以使用以下-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)- no prefix - exact specification (
222
would mean no permssions other than write)
/
- 任何权限位-
- 所有位(-222
意味着所有 - 用户、组和其他)- 没有前缀 - 确切的规范(
222
意味着除了写之外没有权限)
回答by ghostdog74
to find writable files regardless of owner, group or others, you can check the w
flag in the file permission column of ls.
要查找不分所有者、组或其他人的可写文件,您可以检查w
ls 的文件权限列中的标志。
ls -l | awk ' ~ /^.*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. that's all.
$1 是第一个字段,(即 ls -l 的权限块),正则表达式只是说在字段一中找到字母“w”。就这样。
if you want to find owner write permission
如果你想找到所有者写权限
ls -l | awk ' ~ /^..w/'
if you want to find group write permission
如果你想找到组写权限
ls -l | awk ' ~ /^.....w/'
if you want to find others write permission
如果你想找别人写权限
ls -l | awk ' ~ /w.$/'
回答by Paul R
-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
回答by David Waters
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 questionhas already been asked on serverfault
这个问题已经在serverfault 上问过了
EDIT: @ghostdog74 asked if you removed write permissions for this file if this would still find the file. The answer, no this only finds files that are writable.
编辑:@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
回答by Idelic
The problem with find -writable
is that it's not portable and it's not easy to emulate correctly with portable find
operators. 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 -writable
在于它不可移植,并且不容易与可移植find
操作符正确模拟。如果你的版本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. Of course, this will be much less efficient than find -writable
.
-r
for的选项touch
在 POSIX 中,因此可以认为它是可移植的。当然,这将比find -writable
.
Note that touch -r
willupdate each file's ctime(time of last change to its meta-data), but one rarely cares about ctime anyway.
请注意,这touch -r
将更新每个文件的ctime(最后一次更改其元数据的时间),但无论如何人们很少关心 ctime。
回答by JaseC
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.
将 www-data 替换为 nobody 或 apache 或任何您的网络用户。
回答by syam
I know this a very old thread, however...
我知道这是一个非常古老的线程,但是......
The below command helped me: 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. I am using Linux 2.6.18-371.4.1.el5.
您可以根据要搜索的目录下的级别数使用 -maxdepth。我使用的是 Linux 2.6.18-371.4.1.el5。
回答by ghostdog74
stat -c "%A->%n" *| sed -n '/^.*w.*/p'
回答by muruga
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