AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 34216
In Process
DHamrick
DHamrick
Asked: 2009-07-01 18:06:34 +0800 CST2009-07-01 18:06:34 +0800 CST 2009-07-01 18:06:34 +0800 CST

删除Linux目录中的所有可执行文件[关闭]

  • 772
关闭。这个问题需要细节或清晰。它目前不接受答案。
6年前关闭。
锁定。这个问题及其答案被锁定,因为这个问题离题但具有历史意义。它目前不接受新的答案或交互。

在我递归地 tar 一个目录之前,我希望能够删除所有的可执行文件。在 Windows 中,我会简单地删除所有带有 .exe 扩展名的文件。而且我不能简单地删除所有具有可执行模式的文件,因为这也会删除 shell 脚本。那么有没有办法自动删除目录中的非shell脚本脚本文件?

extension tar
  • 8 8 个回答
  • 6031 Views

8 个回答

  • Voted
  1. Geoff Fritz
    2009-07-01T18:46:48+08:002009-07-01T18:46:48+08:00

    我假设您的意思是二进制文件?如果可执行文件的数量很大,该命令可能会因为“file”和“echo”的命令行太长而失败。一个简单的例子:

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

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

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

    这应该为您提供一个简单的模板来说明如何做到这一点。您只需要确保知道在“file”和 grep 给出的输出中要查找的内容。

    当心。如果你在错误的目录上以 root 身份运行它,你可能会完全破坏你的系统。

    • 5
  2. Matt Simmons
    2009-07-01T18:59:49+08:002009-07-01T18:59:49+08:00
    find . -perm /111 -type f -exec echo rm -v {} \;
    

    这里的神奇之处在于 -perm 标志(用于权限)可以在权限参数之前使用 /,这会导致它在每个位上搜索逻辑 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。而且这些不是异或,所以我们正在寻找至少一个但最多 3 个。

    由于unix文件权限是

    rwxrwxrwx
    421421421
    

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

    --1--1--1
    

    或 111

    需要注意的是,在上面列出的命令中,有一个回声可以防止你在脚上开枪。正确确定文件后,请随意采取安全措施。

    编辑

    好的,这也吹走了所有的 shell 脚本。由于相对原始的正则表达式匹配,我找到的 find 没有很好的、简洁的方法。我可以使用正则表达式找到您想要的所有 .sh 文件:

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

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

    写一个shell脚本,或者使用别人的建议,或者如果你真的想,只是暂时删除shell脚本的可执行权限,删除所有可执行文件,然后加上+x。祝你好运。

    • 2
  3. John Kugelman
    2009-07-01T18:48:44+08:002009-07-01T18:48:44+08:00
    file * | grep executable | grep -v 'shell script' | cut -d: -f 1 | xargs rm
    

    也许:

    make clean
    

    ;-)

    • 1
  4. Zoredache
    2009-07-01T22:09:56+08:002009-07-01T22:09:56+08:00

    在我递归地 tar 一个目录之前,我希望能够删除所有的可执行文件。

    恕我直言,简短的回答是没有 100% 万无一失的方法来做到这一点。仅根据权限可能会遗漏或包含您不想要的东西。取决于文件名将不起作用。即使取决于file命令的输出可能也不是一个好主意,我已经看到它多次错误识别事物。

    • 1
  5. krawyoti
    2009-07-02T03:17:28+08:002009-07-02T03:17:28+08:00

    您需要find遍历树中的文件,file命令行实用程序以确定它们的文件类型,然后perl过滤可执行文件:

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

    您确实要删除文件,将最终替换ls -l --为rm --.

    注意:需要所有-print0和-0开关来确保正确处理文件名中的空格。这也是为什么perl使用 代替 的原因awk,它不能真正吞下输入中的空字符。这些--开关旨在防止文件名以破折号开头。

    • 1
  6. Jauder Ho
    2009-07-01T18:26:12+08:002009-07-01T18:26:12+08:00

    您可能必须编写一个 shell 脚本来执行此操作。在我的脑海中,首先运行文件,获取可执行文件并排除所有脚本。然后你可以对剩下的部分进行操作,这应该是你正在寻找的。

    • 0
  7. solarc
    2009-08-12T18:14:29+08:002009-08-12T18:14:29+08:00

    使用马特的回答中的位,我想出了这个:

    find . -perm /111 -type f -print0 | xargs -0 file | grep -v 'shell script' | cut -d: -f1 | xargs echo rm
    
    • 0
  8. クリス
    2015-09-27T07:03:46+08:002015-09-27T07:03:46+08:00

    我创建了一个 bash 脚本来做到这一点。如果您研究了其中的所有行,我相信您可以弄清楚如何从命令行执行所有操作并对其进行修改以删除任何类型的文件,即使用“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 u-x $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
    
    • 0

相关问题

  • 解压 tar 中的特定目录

  • 资源管理器右键单击上下文菜单项丢失

  • 如何在不安全的网络中的服务器之间执行安全 rsync

  • 在提取之前从 tgz 获取提取的大小

  • 为什么“tar -cSf file.tar source”内存不足?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    从 IP 地址解析主机名

    • 8 个回答
  • Marko Smith

    如何按大小对 du -h 输出进行排序

    • 30 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    Windows 中执行反向 DNS 查找的命令行实用程序是什么?

    • 14 个回答
  • Marko Smith

    如何检查 Windows 机器上的端口是否被阻塞?

    • 4 个回答
  • Marko Smith

    我应该打开哪个端口以允许远程桌面?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    MikeN 在 Nginx 中,如何在维护子域的同时将所有 http 请求重写为 https? 2009-09-22 06:04:43 +0800 CST
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    0x89 bash中的双方括号和单方括号有什么区别? 2009-08-10 13:11:51 +0800 CST
  • Martin Hope
    kch 如何更改我的私钥密码? 2009-08-06 21:37:57 +0800 CST
  • Martin Hope
    Kyle Brandt IPv4 子网如何工作? 2009-08-05 06:05:31 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve