Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
LINUX-shell脚本查找并列出所有具有目录树中写入权限的文件_Linux_Bash_Shell_Ubuntu - Fatal编程技术网

LINUX-shell脚本查找并列出所有具有目录树中写入权限的文件

LINUX-shell脚本查找并列出所有具有目录树中写入权限的文件,linux,bash,shell,ubuntu,Linux,Bash,Shell,Ubuntu,以下是我所掌握的代码: echo $(pwd > adress) var=$(head -1 adress) rm adress found=0 #Flag fileshow() { cd $1 for i in * do if [ -d $i ] then continue elif [ -w $i ]

以下是我所掌握的代码:

echo $(pwd > adress)
var=$(head -1 adress)
rm adress

found=0 #Flag 
fileshow()
{
    cd $1
    for i in *
        do
            if [ -d $i ] 
                then 
                    continue
            elif [ -w $i ]
                then 
                    echo $i 
                    found=1 
            fi
        done
    cd ..
}
fileshow $1

if [ $found -eq 0 ]
    then
        clear
        echo "$(tput setaf 1)There arent any executable files !!!$(tput sgr0)"
fi
它可以工作,但只能在当前目录中找到文件

有人告诉我,我需要使用某种递归方法来循环遍历所有子目录,但我不知道怎么做

因此,如果有人能帮助我,我将非常感激


谢谢

您可以使用
查找

find/path/to/directory/-type f-perm-o=w

其中
-o=w
表示每个文件都设置了“其他写入权限”

或者

find/path/to/directory/-type f-perm/u+w、g+w、o+w


其中
/u+w、g+w、o+w
表示每个文件都设置了用户、组或其他写入权限。

脚本的作用是在当前工作目录下查找不属于目录且可写入当前用户的文件。这可以通过以下命令实现:

find ./ -type f -writable
使用
-type f
的优点是,如果您需要的话,它还排除了符号链接和其他特殊类型的文件。如果希望所有文件都不是目录(如脚本所建议的),则可以使用:

find ./ ! -type d -writable
如果您想对这些文件进行排序(添加了问题,假设字典升序),您可以使用
排序

find ./ -type f -writable | sort
如果您想将这些排序后的文件名用于其他用途,规范模式将是(用嵌入的换行符和其他很少使用的字符处理文件名):

读取时-r-d$'\0';做
echo“文件“$REPLY”是一个普通文件,可写”

完成
-writable
选项在我的
find
版本中似乎不可用(
GNU find version 4.2.27
)。啊,我有4.4.2。我会在一分钟内更新我的答案。是的,这很好,但当我找到这些文件时,我需要将它们传递给C进程,该进程将不得不对它们进行ASC排序。很抱歉,我从一开始就没这么说。那么,如何使用shell脚本实现这一点呢?我需要使用某种变量来存储我的文件,还是?IDK正如我所说,我对脱离shell脚本的概念并不十分熟悉:)我相信您正在寻找管道
。所以你要做一个
查找…|/路径/到/c_进程
。这会将
find
命令的输出重定向到C进程的输入。
while read -r -d $'\0'; do
    echo "File '$REPLY' is an ordinary file and is writable"
done < <(find ./ -type f -writable -print0 | sort -z)