如何删除/删除可执行文件(又名没有扩展名的文件)

我有一个目录src /包含许多.cc文件及其二进制文件。 例如:

src/ |_ foo.cc |_ bar.cc |_ qux.cc |_ thehead.hh |_ foo (executable/binary) |_ bar (executable/binary) |_ qux (executable/binary) |_ makefile 

实际上有很多.cc和可执行文件。

我需要以全局方式删除这些二进制文件,而不必列出所有文件。 有没有一个快速和紧凑的方法来做到这一点?

我试过了:

 $ rm * 

但它会删除包含.cc和.hh文件的所有文件。

我知道这个命令:

 $ rm foo bar qux .... 

但是我们仍然需要一一列出所有的文件。

你可以跑

 find . -perm +100 -type f -delete 

干得好:

 ls | grep -v "\." | xargs rm 

grep -v表示“只允许不包含点的文件名”, xargs rm表示“然后将文件名列表传递给rm ”。

使用find 。 你想要的是这样的:

 find . -type f -executable -exec rm '{}' \; 

删除没有扩展的所有内容也可以完成:

 find . -type f -not -iname "*.*" -exec rm '{}' \; 

前一个选项不会删除Makefile ,因此是首选。 我认为kcwu的答案显示了使用-delete选项改进上述方法的好方法:

 find . -type f -executable -delete find . -type f -not -iname "*.*" -delete 

编辑 :我在Ubuntu 8.10下使用GNU findutils find ,版本4.4.0。 我没有意识到-executable开关是非常罕见的。

我宁愿去一个干净的目标在Makefile中。 很可能它已经包含了这些二进制文件的列表,所以添加一个干净的目标不需要太多的努力。

 find . -perm /ugo+x -delete 

更正Stephan202的第一个命令的版本。

编辑:也请尝试:

 find . -perm /111 -delete 

它使用八进制等效

而不是传递-exec rm '{}' \; 找到一个可以使用-delete参数。

使用find来删除不包含点字符的所有文件(不是文件夹):

 find . \! -name "*.*" -type f -exec rm {} \; 

我建议先使用

 find . -not -name "*.*" -exec ls -l {} \; 

查看匹配的文件的名称。

然后,将ls -l更改为rm

 find . -not -name "*.*" -exec rm {} \; 

另外,您可以使用确认提示使其更安全:

 find . -not -name "*.*" -exec rm -i {} \;