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.
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
".
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With