views:

947

answers:

8

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.

+4  A: 

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".

RichieHindle
@Richie: thanks. Is there a way to exclude "makefile"? Which we want to keep.
neversaint
@foolishbrat: Just add another grep: ls | grep -v "\." | grep -v makefile | xargs rm
RichieHindle
+3  A: 

Use the find. What you want is this:

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

Removing everything without an extension can also be done:

find . -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 . -executable -delete
find . -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.

Stephan202
@Stephan: I got "find: -executable: unknown option"
neversaint
What find are you using? -executable doesn't work for me. However, I can do use -perm /ugo+x with GNU find.
Matthew Flaschen
@foolishbrat: perhaps you're using an older option of find. Try kcwu's answer.
Stephan202
@Matthew: I updated the answer. I suppose most alternatives to -executable are more portable.
Stephan202
Interesting. I'm using Ubuntu 8.04 (only one version behind, albeit LTS).
Matthew Flaschen
+7  A: 

you can run

find . -perm +100 -type f -delete
kcwu
Just for clarity, this only checks whether the user's exec bit is set, and it's using deprecated syntax. See man find for the gory details.
Matthew Flaschen
+1  A: 

Instead of passing -exec rm '{}' \; to find one can use -delete arg.

+3  A: 

Use find to remove all files (not folders) that do not contain a dot characeter:

find . \! -name "*.*" -type f -exec rm {} \;
Reuben Peeris
+3  A: 
find . -perm /ugo+x -delete

Corrected version of Stephan202's first command.

EDIT: Also try:

find . -perm /111 -delete

which uses the octal equivalent

Matthew Flaschen
@Matt: under MacOsX, I get this: find: -perm: /ugo+x: illegal mode string
neversaint
Try the octal version.
Matthew Flaschen
+2  A: 

i suggest using first

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

to see the name of files that are matched.

and then, change the ls -l to rm

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

also, you can use a confirm prompt to make it more safe:

find . -not -name "*.*" -exec rm -i {} \;
動靜能量
+5  A: 

I would rather go for a clean target in the Makefile. Most probably it already contains a list of these binaries, so adding a clean target would not require much effort.

Raim
And it's safer. Just think about what happens if you use one of the other grep'ish solutions and someone decides that it would be a good idea to have a "README" file ;-)
lothar