简体   繁体   English

递归删除文件夹中的所有二进制文件

[英]Recursively delete all binary files in folder

I want to recursively delete all binary files in a folder under linux using the command-line or a bash script.我想使用命令行或 bash 脚本递归删除 linux 下文件夹中的所有二进制文件 I found我发现

grep -r -m 1 "^"  path/to/folder | grep "^Binary file"

to list all binary files in path/to/folder at How to list all binary file extensions within a directory tree?如何列出目录树中的所有二进制文件扩展名中列出path/to/folder中的所有二进制文件? . . I would now like to delete all these files.我现在想删除所有这些文件。 I could do我可以

grep -r -m 1 "^"  path/to/folder | grep "^Binary file" | xargs rm

but that is rather fishy as it also tries to delete the files 'Binary', 'file', and 'matches' as in但这很可疑,因为它还尝试删除文件“二进制”、“文件”和“匹配”,如

rm: cannot remove ‘Binary’: No such file or directory
rm: cannot remove ‘file’: No such file or directory
rm: cannot remove ‘matches’: No such file or directory

The question is thus how do I delete those files correctly ?因此,问题是如何正确删除这些文件

This command will return all binary executable files recursively within a directory, run this first to ensure proper output.此命令将递归返回目录中的所有二进制可执行文件,首先运行此命令以确保正确输出。

find . -type f -executable -exec sh -c "file -i '{}' | grep -q 'x-executable; charset=binary'" \; -print

If that works you can pass the output to xargs to delete these files.如果可行,您可以将输出传递给 xargs 以删除这些文件。

find . -type f -executable -exec sh -c "file -i '{}' | grep -q 'x-executable; charset=binary'" \; -print | xargs rm -f

Hope this helped, have an awesome day!希望这有帮助,祝你有美好的一天! :) :)

I coded a tool, called blobs , that lists runable binaries.我编写了一个名为blob的工具,它列出了可运行的二进制文件。

Its readme mentions how to pipe to any other command.它的自述文件提到了如何通过管道传输到任何其他命令。

This should do the job, if you are deleting a lot of binrary files in a folder.如果您要删除文件夹中的大量二进制文件,这应该可以完成工作。

 find . -type f -executable | xargs rm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM