![如何在 Linux 中删除基于源代码的文件类型?](https://linux22.com/image/176197/%E5%A6%82%E4%BD%95%E5%9C%A8%20Linux%20%E4%B8%AD%E5%88%A0%E9%99%A4%E5%9F%BA%E4%BA%8E%E6%BA%90%E4%BB%A3%E7%A0%81%E7%9A%84%E6%96%87%E4%BB%B6%E7%B1%BB%E5%9E%8B%EF%BC%9F.png)
我有一个被抓取的原始文件列表,它由文本和源代码组成。下面是列出的文件类型,我想删除所有类型为 C 源、python 脚本、HTML 和空文件的文件,只保留 ASCII 和 unicode 文件。
file *
1dW6WJMN.txt: Python script, ASCII text executable
9dJbZ3Vv.txt: ASCII text, with CRLF line terminators
9dQsmVU4.txt: Python script, UTF-8 Unicode text executable, with CRLF line terminators
A5hENB7D.txt: C source, ASCII text, with CRLF line terminators
cidREdJG.txt: UTF-8 Unicode text, with very long lines, with CRLF line terminators
exhjw1gK.txt: UTF-8 Unicode text, with CRLF line terminators
iu7LPrqz.txt: ASCII text, with very long lines, with CRLF line terminators
LsDHarjD.txt: ASCII text
nLABt1a6.txt: C source, ASCII text, with CRLF line terminators
nqMDtVuz.txt: ASCII text, with CRLF line terminators
nqPuYb23.txt: UTF-8 Unicode text, with CRLF line terminators
nQtzxhfQ.txt: ASCII text, with CRLF line terminators
NQuLWwpt.txt: ASCII text, with CRLF line terminators
nQXeJeED.txt: ASCII text, with CRLF line terminators
nqXGv6ws.txt: UTF-8 Unicode text, with CRLF line terminators
nQxr4Hwi.txt: ASCII text, with CRLF line terminators
nQxr4Hwii.txt: empty
VQjrxevh.txt: HTML document, UTF-8 Unicode text, with very long lines, with CRLF line terminators
yfDEfn4L.txt: C source, ASCII text, with CRLF line terminators
yydAEDRn.txt: HTML document, ASCII text, with very long lines, with CRLF line terminators
我尝试使用带有 ASCII 的简单 grep,但所有源代码文件也包含术语 ASCII。有没有其他方法可以过滤掉这些源代码文件,因为有时还有我想删除的 PHP、javascript 文件。我对 Linux 很陌生,任何帮助将不胜感激。提前致谢
答案1
尝试更长的图案。您可以使用包含空格、制表符或多个单词的模式。我还建议使用管道的渐进方法:
$ file * | egrep -v 'ASCII text|Unicode text' | sed 's/: ..*$//'
如果这不能让您获得所需的文件名列表,请点击向上箭头并编辑模式以匹配文件的或多或少或不同部分。file
最后一步可能是将输出发送到充满命令的文件中:
$ file * | egrep -v 'ASCII text|Unicode text' | sed -e 's/: ..*$//' -e 's/^/rm / > commands
检查文件commands
内容的正确性,也许可以消除最后一个麻烦的情况。使用管道获得 95% 的目标,然后进行手动编辑。没有什么可耻的。然后运行管道写出的命令:
$ sh ./commands