1

How to remove every file in the current directory except *.c files ?

For ex, if i do rm *.c it removes all the .c files, but does not remove other other files.

I just want to do the opposite.

sps
  • 2,720
  • 2
  • 19
  • 38
  • 1
    https://stackoverflow.com/questions/216995/how-can-i-use-inverse-or-negative-wildcards-when-pattern-matching-in-a-unix-linu – Jim Deville Jun 06 '15 at 04:14

1 Answers1

2

You can do it using find:

find . -not -name *.c -delete

The version above will delete anything which isn't in the format of *.c from the current directory and below.

If you want to delete all the non *.c files only in the current directory (and not below) you can use the switch: -depth 1

find . -depth 1 -not -name *.c -delete
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • Thanks for the help. Actually i wanted to use `rm` to do that. But anyways you have basically answered the question. Also from the link given by Jim in comments above i saw that we can use `shopt -s extglob` and then use the `rm !( *.c )` to do the same. Then `shopt -u extglob` when we dont want this functionality. But i guess i need to start using `find` more.. – sps Jun 06 '15 at 04:34
  • @shreyans800755 Please read [this](http://stackoverflow.com/help/editing) – Nir Alfasi Jun 06 '15 at 07:00