Tips & Tricks

Suppose you have a directory with lot of files and you want to delete all the files except some of them (with particular file extensions). This can be done in following way :

Here is a directory containing lot of files :

$ ls
a.out Cfile.c file.c macro.c my_printf.c orig_file.orig stacksmash.c
bfrovrflw.c cmd.c main.c new_printf.c orig_file.rej test_strace.c
bufrovrflw.c env.c helloworld.c my_fopen.c new.txt prog.c virtual_func.c

Now, you want to delete all the files except .c and .py files.

Here is what you can do :

$ rm !(*.c|*.py)

$ ls
bfrovrflw.c Cfile.c env.c macro.c my_fopen.c new_printf.c stacksmash.c virtual_func.c
bufrovrflw.c cmd.c file.c helloworld.c main.c my_printf.c prog.c test_strace.c

So you can see that files with all other extensions got deleted.

Go Back to Menu