I don't recommend trying to write a rule to exempt specific files. What I would be more likely to do, would be to try and find either a filemask/glob, or some other anchor in the names of the files that you do want to remove, which the files you want to keep, does not have. In most cases, you could probably do that by extension. Unfortunately, because you haven't mentioned what the names of any of the files are, I can't help you more specifically.
Something like two (or more) find loops:-
find *.mp3 d.mp3 -maxdepth 1 -type f | while read mp3s
do
mv $mp3s ~/d.stuff-to-keep
done
find *.txt d.mp3 -maxdepth 1 -type f while read textfiles
do
rm -v $textfiles
done
If not, look for some other common characteristic of the files. If there isn't one, you might need to rename your files to give yourself one. I always put lots of anchors (seperated name fields, extensions etc) in my filenames, because that way I have a lot of different ways for doing what I need with them. My usual name format is:-
<extension>.+<author>+<year>+<work-name>+<volume-number>+<number-of-volumes>
Plus signs are field seperators, dashes are for spaces, but with no dashes as either first or last characters. All lowercase letters, and no control characters or other non-alphanumerics allowed. As a result, I can cut those up with awk, cut, or whatever else I might want to use, and rename them easily. Putting the extension at the start of the name, also means that ls only needs one flag (-l) to get a perfectly ordered file system.
If you know how to use your directory structure as a database with the basic POSIX tools, you sometimes won't need to install anything more complicated, in order to do what you want.
You've got a long char limit on filenames in UNIX, as well as tab completion for navigating around long names. Don't be afraid to make liberal use of both of them.
rm !(filename1|filename2) -rf
works fine outside a script? I'd have expectedrm -rf !(filename1|filename2)
instead (with the option before the operands). – Rickardrm
after the files from time to time. Maybe it's a bit inconsistent but it always worked for me. @mario you don't have to switch back the directory withcd -
because a subshell is used when you execute your script. – Rosenwald