Using LaTeX creates a bunch of auxiliary files like foo.aux, foo.log, etc. Sometimes, I want to create a clean run of my document. I usually manually execute this in the terminal as
$ ls foo*
foo.tex
foo.aux
foo.log
foo.bbl
foo-blx.bib
$ rm foo{.aux,.log,.bbl,-blx.bib}
This works fine. However, it is error-prone and I don't want to accidentally erase my .tex file. So I added this function to my ~/.bashrc:
# Delete all non-TeX files
# invoke as: cleantex foo
# where: foo.tex, foo.aux, foo.log, etc. are files in directory
cleantex () {
if [ -n "1ドル"]; then
name = $(basename -s ".tex" "1ドル")
rm -f $name{.!(*tex),-blx.bib}
fi
}
My question is about the key line
rm -f $name{.!(*tex),-blx.bib}
that actually executes the script.
Is this line well-written? What might I improve here?
2 Answers 2
My real concerns with the script are:
cleantex path/to/file.texwill not delete files in that path, but in the current directory.- you should check the actual .tex file exists before you delete all the things around it.
- the brace-expansion is unnecessarily complicated...... especially when combined with the extended glob
!(*tex). I would manually resolve the brace-expansion so that there is only one complicated operation on that line. - I would use actual glob-expansion and only delete existing files.... and not use the -f option on rm (which does more than just suppress the error message if files do not exist....)
Mostly minor things:
[ -n "1ドル" ]is equivalent to[ "1ドル" ]. I'd go for the shorter one- Since you are in
bash, you can use a variable substitution${1%.tex}instead ofbasename, which is slightly better - There cannot be spaces around the
=sign in assignments:name = valis incorrect, should bename=val - As @rolfl suggested in his answer, it would be better to check if a
texfile exists before deleting anything
Putting it all together:
cleantex () {
if [ "1ドル" ]; then
name=${1%.tex}
test -f $name.tex || return 1
rm -f $name{.!(*tex),-blx.bib}
fi
}
Some extra remarks:
- I would add the
-vflag for therm, so that it will print what it actually removed:rm -vf $name{.!(*tex),-blx.bib} - I guess you will never create
.texfiles with spaces in the name. If you ever do, you'll need to quote$name, for example:rm -f "$name"{.!(*tex),-blx.bib} - I would drop the
-fflag fromrm. Sure, there will be some error messages that way when there's nothing to delete, but I don't see that as a bad thing.
basenameare you using? I am not familiar with (and can't find) the-soption for it. \$\endgroup\$man basenamehas the option, as does the official documentation. Is your distribution out of date? \$\endgroup\$.texsuffix if there is one. So,cleantex foois the same ascleantex foo.tex. \$\endgroup\$basename foo.tex .texwill producefoo. Food for thought \$\endgroup\$