I have a directory containing lot of files in different formats. I would like to know how I can delete all files with specific format (Lets say *.dat) except a few files in a same format (e.g. A.dat and B.dat). Please advise!
Advertisement
Answer
I’d write a little script (as a command-line one-liner it is slightly too big):
#!/bin/sh for f in *.dat; do case $f in (A.dat|B.dat) ;; # do nothing (*) rm -- "$f";; # remove the file esac done
As an alternative, you could use an interactive rm -i *.dat
which asks you for each file if it should be removed. Answer y
for the files you no longer need, and n
for A.dat
and B.dat
.
Modern shells like zsh and bash also offer powerful globbing features for your problem. I suggest you read their manual pages, which will help you become a proficient shell guru.