Search code examples
shellexpansionrm

Delete anything other than *.c


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.


Solution

  • 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