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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 19:05:12  来源:igfitidea点击:

Find all writable files in the current directory

linuxbashfile

提问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 -writableoption 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 -permoption:

-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 (-222would mean all - user, group and other)
  • no prefix - exact specification (222would mean no permssions other than write)
  • /- 任何权限位
  • -- 所有位(-222意味着所有 - 用户、组和其他)
  • 没有前缀 - 确切的规范(222意味着除了写之外没有权限)

回答by ghostdog74

to find writable files regardless of owner, group or others, you can check the wflag in the file permission column of ls.

要查找不分所有者、组或其他人的可写文件,您可以检查wls 的文件权限列中的标志。

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

-fwill test for a file

-f将测试一个文件

-wwill 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 -writableis that it's not portable and it's not easy to emulate correctly with portable findoperators. If your version of finddoesn't have it, you can use touchto check if the file can be written to, using -rto 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 -roption for touchis in POSIX, so it can be considered portable. Of course, this will be much less efficient than find -writable.

-rfor的选项touch在 POSIX 中,因此可以认为它是可移植的。当然,这将比find -writable.

Note that touch -rwillupdate 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