Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove/delete executable files (aka files without extension) only

Tags:

c++

file

linux

unix

I have a directory src/ that contain many .cc files and its binary. For example:

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

In reality there are many .cc and executable files.

I need to remove those binaries in a global way without having to list all the files. Is there a fast and compact way to do it?

I tried:

$ rm *

but it removes all the files include .cc and .hh files.

I am aware of this command:

$ rm foo bar qux ....

but then we still have to list all the files one by one.

like image 853
neversaint Avatar asked May 13 '09 06:05

neversaint


2 Answers

Here you go:

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

The grep -v says "only allow filenames that don't contain a dot", and the xargs rm says "then pass the list of filenames to rm".

like image 175
RichieHindle Avatar answered Nov 16 '22 04:11

RichieHindle


Use the find. What you want is this:

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

Removing everything without an extension can also be done:

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

The former option does not delete the Makefile, and is thus to be preferred. I think kcwu's answer shows a nice way to improve the above using the -delete option :

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

Edit: I use GNU findutils find, version 4.4.0, under Ubuntu 8.10. I was not aware the -executable switch is so uncommon.

like image 33
Stephan202 Avatar answered Nov 16 '22 04:11

Stephan202