One problem with UNIX: it's not terribly good at "excluding" things. There's no option to rm that says, "Do what you will with everything else, but please don't delete these files." You can sometimes create a complex that does what you want - but sometimes that's a lot of work, or maybe even impossible.
Here's one place where UNIX'soperators (backquotes) come to the rescue. You can use use ls to list all the files, pipe the output into a command, and then use backquotes to give the resulting list to rm. Here's what this command would look like:
rm -i `ls -d *.txt | grep -v '^john\.txt$'`
[Actually, when you're matching just one filename,might be better. -JP ] This command deletes all files whose names end in .txt, except for john.txt. I've probably been more careful than you need to be about making sure there aren't any extraneous matches; in most cases, grep -v john would probably suffice. Using makes sure that ls doesn't look into any subdirectories and give you those filenames. The asks you before removing each file; if you're sure of yourself, omit the -i.
Of course, if you want to exclude two files, you can do that with egrep:
rm `ls -d *.txt | egrep -v 'john|mary'`
(Not all egrep implementations support the -v option.
Don't forget to quote the vertical bar (
|), to prevent the shell from piping
egrep's output to mary.)
Another solution is thescript.