Yamaneko
Yamaneko

Reputation: 3563

Remove all files with different extensions and keep only one using bash regexp

When I'm writing a Latex file, sometimes I need to remove all files which were generated by its compilation and keep only the .tex file. Example, I want to remove all the following files:

paper-stack.log
paper-stack.blg
paper-stack.bbl
paper-stack.aux
paper-stack.pdf

and keep the following file:

paper-stack.tex

Besides that, I want to remove using bash regexp. I rememeber I did it before, but now I'm confused about the regexp patterns after using other shells. It was something like rm paper-stack.[!tex], but I'm not sure about the metacharacters [, ] and !.

Upvotes: 2

Views: 340

Answers (3)

sarnold
sarnold

Reputation: 104050

One of the nice points of creating a Makefile is that it can assist in running LaTeX multiple times if necessary; this is taken from a production Makefile that keeps a technical document up-to-date:

techdoc.pdf: techdoc.tex
        while pdflatex $< ${BUILD_OUTPUT} || exit 1 ; \
                grep -q "Label(s) may have changed" techdoc.log; \
        do :; done

This way, just running make (if this is the first target in the Makefile) will rebuild the techdoc.pdf as many times as necessary to ensure that references and labels are correctly updated. (This is usually just twice, might sometimes be three times, and I've heard that someone clever could make input that causes a loop like this to never terminate -- but I've never seen one yet.)

Once you've got something like this in place, it's a piece of cake to keep going:

.PHONY: clean

clean: techdoc.log techdoc.bbl techdoc.aux techdoc.blg
        rm $?

See http://www.gnu.org/software/make/manual/make.html#Special-Variables to explain the $?, $@, and similar short-hand variables available in GNU Make.

Upvotes: 2

Jasonw
Jasonw

Reputation: 5064

I think you can use the command find to the directory you want and remove them. Something like the following. But consider what sarnold suggested, put the command somewhere like in the makefile.

find my/dir/ -name "*.log" -or -name ".aux" -exec rm '{}' \;

Upvotes: 2

geekosaur
geekosaur

Reputation: 61369

If you have done shopt extglob then you can say something like

rm paper-stack.!(tex)

(As usual when working with rm or other potentially destructive commands, it's a good idea to test with echo first.) I would be more explicit about what to remove though, just on general principles.

Upvotes: 3

Related Questions