在Linux中删除目录中的所有可执行文件

在我recursion地tar目录之前,我想能够删除所有的可执行文件。 在Windows中,我会简单地删除所有的.exe扩展名的文件。 而且我不能简单地删除所有带有可执行模式的文件,因为那样会删除shell脚本。 那么是否有办法自动删除目录中的非shell脚本文件?

我假定你的意思是二进制文件? 如果可执行文件的数量非常大,则由于“file”和“echo”的命令行太长,此命令可能会失败。 一个简单的例子:

find /bin -type f | xargs file | grep "ELF.*executable" | awk -F: '{print $1}' | xargs echo

如果在上面的例子中用“rm”代替“echo”,这些文件将被删除。 “grep”命令应该防止库被删除(你可以在/ lib上testing)。

(注意,我现在只能访问FreeBSD,所以linux的“file”命令的输出可能会有所不同,从而改变你需要做的事情。)

这应该给你一个简单的模板,如何做到这一点。 你只需要确定知道在“file”给出的输出中寻找什么,grep是相应的。

小心。 如果你以root身份运行在错误的目录中,你完全可能会使你的系统受到束缚。

 find . -perm /111 -type f -exec echo rm -v {} \; 

这里的奇妙之处在于,-perm标志(用于权限)可以在permission参数前面加一个/,这使得它在每个位上search一个逻辑OR。 从手册页:

  -perm /mode Any of the permission bits mode are set for the file. Symbolic modes are accepted in this form. You must specify 'u', 'g' or 'o' if you use a symbolic mode. See the EXAMPLES section for some illustrative examples. If no permission bits in mode are set, this test currently matches no files. However, it will soon be changed to match any file (the idea is to be more con- sistent with the behaviour of perm -000). 

如果不明确,/指定111在用户中指定x或在其他组中指定x或x。 而那些不是异或,所以我们至less要找一个,但最多3个。

由于unix文件的权限是

 rwxrwxrwx 421421421 

我们关心x位,我们得到了一个掩码

 --1--1--1 

或111

应该指出的是,在上面列出的命令,有一个回声,以防止你在自己的脚射击。 一旦你正确地钉住了文件,随意采取安全措施。

编辑

好的,这也吹走了所有的shell脚本。 发现我发现,由于相对原始的正则expression式匹配,没有一个好的,简单的方法来做到这一点。 我可以使用正则expression式find所有需要的.sh文件:

  -regex ".*\(.*sh\$\)" 

但是我根本无法反转。 所以我放弃了。 我仍然留在这里,以防某人有用。

编写一个shell脚本,或者使用其他人的build议,或者如果你真的想,只是临时删除可执行文件的权限到shell脚本,删除所有的可执行文件,然后加回+ x。 祝你好运。

 file * | grep executable | grep -v 'shell script' | cut -d: -f 1 | xargs rm 

也许:

 make clean 

😉

在我recursion地tar目录之前,我想能够删除所有的可执行文件。

恕我直言,简单的答案是,没有100%的万无一失的方式来做到这一点。 根据权限单独可能会错过或包括你不想要的东西。 取决于文件名将不起作用。 即使取决于文件命令的输出可能不是一个好主意,我已经看到它错过了几次识别的东西。

您需要find遍历树中的filefile命令行实用程序来确定它们的文件types,然后是perl来过滤可执行文件:

 find . -type f -print0 | xargs -0 file -N0 -- \ | perl -nlwe '/(.*\0).*ELF.*executable/ and printf $1' | xargs -0 ls -l -- 

你真的想删除文件ls -l --rm --replace最后的ls -l --

注意:所有-print0-0开关都需要确保文件名中的空格处理正确。 这也是为什么使用perl而不是awk的原因, awk在input中不能真正吞下空字符。 --开关是为了防止文件名开始破折号。

你可能不得不编写一个shell脚本来做到这一点。 closures我的头顶,先运行文件,获取可执行文件并排除所有脚本。 然后,你可以运行其余的应该是你正在寻找的东西。

用马特的答案,我想出了这个:

 find . -perm /111 -type f -print0 | xargs -0 file | grep -v 'shell script' | cut -d: -f1 | xargs echo rm 

我创build了一个bash脚本来做到这一点。 如果你研究它的所有行,我相信你可以弄清楚如何做一切从命令行,并修改它删除任何types的文件,即使用“find-iname”或其他。

 #!/bin/bash #this script will grab all executable files in the #bash shell scripts ending in .sh will be saved #current directory and delete them #it will also destroy itself so keep a copy somewhere echo "WARNING! This script will self-destruct and take all" echo "executable files in the current directory when executed" echo "" #first create a new file to set up executable files touch xdelgo.txt #save all the bash shell scripts ending in .sh #this will only work and in the pwd and not child directories touch protected.sh echo "#!/bin/bash" >> protected.sh find . -type f | grep .sh | grep -v xdel.sh >> protect.txt SAVE=$'\n';x=sudo;for l in $(<protect.txt);do echo -e "$x chmod ux $l"; ((x+1));done >> protected.sh sudo chmod u+x protected.sh ./protected.sh #throw all executable files from the current directory inside find -type f -executable >> xdelgo.txt echo "The following files have been marked for deletion" echo " " cat xdelgo.txt echo " " #create a new script that will execute deletion touch xdelgo.sh echo "#!/bin/bash" >> xdelgo.sh echo "rm xdelgo.sh" >> xdelgo.sh echo "rm xdelgo.txt" >> xdelgo.sh echo "rm protect.txt" >> xdelgo.sh #append rm to each line and throw into new executable file DEL=$'\n';x=rm;for l in $(<xdelgo.txt);do echo -e "$x\t$l";((x+1));done >> xdelgo.sh #go give permission to the file sudo chmod u+x xdelgo.sh echo "bash scripts ending in .sh have been protected" echo "run ./restore.sh to re-enable .sh files" echo "" touch restore.sh echo "#!/bin/bash" >> restore.sh find . | grep .sh | grep -v xdel.sh >> restore.txt SAVE=$'\n';x=sudo;for l in $(<restore.txt);do echo -e "$x chmod u+x $l"; ((x+1));done >> restore.sh sudo chmod u+x restore.sh echo "rm restore.txt" >> restore.sh echo "rm restore.sh" >> restore.sh echo "rm protected.sh" >> restore.sh #THE POINT OF NO RETURN echo "Are you sure you really wanna delete these files?" echo "Press ENTER to continue, ctrl+c to abort" read input ./xdelgo.sh