Possible Duplicate:Deleting all files in a folder except files X, Y, and Z I am writing a script to compile many .tex files. After that, many auxiliaries files were created. So, I would like to remove them at the end of the script. I tough to use rm $file.xxx but I would have to write all the extensions by hand, since that I want to keep my source file and the pdf output, so I can not use the * to delete all.How to delete all the extensions except those with extension .tex/.pdf?Thanks.
Deleting many files except one type
shell script;rm;latex
Well, you could just ignore the rm errors, but that's not elegant :).Add a function like this to your ~/.bashrc: function cleantex () {   find . -name $1* | grep -vP '.tex$|.pdf$' | while read -r i; do rm $i; done      }You then call it with the (unique) beginning of your filename like so: $ cleantex mytexfileIt will delete all files in the given directory that start with $file and do not end in .tex or .pdf. The $ makes sure the match is only at the end of the file, the extension. Otherwise it will ignore files like footexfoo.aux because the file name contains tex. You can add as many extensions to ignore as you want: function cleantex () {       find . -name $1* | grep -vP '.tex$|.pdf$|.foo$|.bar$' | while read -r i; do rm $i; done          }If you want to remove these files from within a bash script, you don't really need the function. Just add this line to your script (changing mytexfile to whatever your tex file is called):find . -name mytexfile* | grep -vP '.tex$|.pdf$' | while read -r i; do rm $i; doneFinally, if you want to be able to do this for all tex files, whatever their name, use:find . -name *.tex | sed 's/\.tex$//' | while read -r name; do     find . -name $name* | grep -vP '.tex$|.pdf$' | while read -r i;       do        rm $i;       done;done