删除目录中所有具有可执行权限的文件

sar*_*mar 4 shell files

我需要executable从目录中删除所有文件。我的目录包含一些配置文件和可执行文件,我需要删除所有executable我不想要的文件。

为此,我写了一个这样的命令:

ls -lp | grep -v / | awk 'match($0,"-**x*x*x",a);{print  a[1]}'| \
awk '{print $9}' | xargs rm -f
Run Code Online (Sandbox Code Playgroud)

有没有其他方法可以做到这一点?

我试过find。它将列出所有其他子目录文件。我使用grep -v /, 以避免当前文件夹中的子目录。

hee*_*ayl 21

您应该find用于此任务:

find . -type f -executable
Run Code Online (Sandbox Code Playgroud)

将递归地向您显示当前用户可执行的文件。

将搜索限制为仅当前目录:

find . -maxdepth 1 -type f -executable
Run Code Online (Sandbox Code Playgroud)

去除:

find . -maxdepth 1 -type f -executable -exec rm {} +
Run Code Online (Sandbox Code Playgroud)

或者使用 GNU find

find . -maxdepth 1 -type f -executable -delete
Run Code Online (Sandbox Code Playgroud)

作为旁注,如果您想查找任何用户可执行的文件,而不仅仅是当前用户(设置为常规执行权限位):

find . -type f -perm /111
Run Code Online (Sandbox Code Playgroud)