0

I use Latex to write my documents. Latex creates MANY auxiliary files to compile a document. I often times want to clean my working directory.

When I was working on Windows, I used to keep a .bat file in the working directory that looked like this:

del *.aux
del *.pdf
del *.log
del *.bak
del *.gz
del *.bbl
del *.blg

which I could just click on to get rid of all auxiliary files.

Now, I want to do the same on my Mac. I have created a .sh file like this:

#!/bin/bash

cd `pwd`

echo "Cleaning files..."

rm *.aux
rm *.bak
rm *.bbl
rm *.blg
rm *.gz
rm *.log
rm *.pdf

echo "Done!"

which I know I can run (i.e. invoke from command line), but I cannot click on - which is more convenient because not always I will be using Terminal.

How can I convert this script into a "clickable" one?

I appreciate any input!

  • chmod +x myfile.sh now you should. You can also assign an icon using the get info menu so it will look nice :) – Downgoat Jul 6 '15 at 4:37
  • Thanks for the suggestion. However, when I click on the file, the program invoked still is textedit. – thiagoveloso Jul 6 '15 at 5:05
  • you may need to go to Get Info, and change the default program there – Downgoat Jul 6 '15 at 5:06
  • Thanks, it worked now. However, the rm commands were not applied in the current directory. I guess they were executed in the home dir. Luckily, I did not have any file that matches the to be deleted file extensions. Any sugestions on the script to run on the directory it was invoked from? – thiagoveloso Jul 6 '15 at 5:31
  • add this code: cd "pwd" but replace the " with backticks – Downgoat Jul 6 '15 at 5:40
1

Create a text file like this:

#!/bin/bash

cd "$(dirname "${BASH_SOURCE[0]}")" || {
    echo "Error changing directory." >&2
    exit 1
}

echo "Cleaning files..."

rm *.aux
rm *.bak
rm *.bbl
rm *.blg
rm *.gz
rm *.log
rm *.pdf

echo "Done!"

Give it a ".command" extension, and add execute permission to it. This will make it automatically open and run in Terminal when it's double-clicked.

Note that there are two important differences (and one minor one) between this and @thiagoveloso's script:

  • It uses double-quotes around the path it's cding to, which will prevent problems if any directory names contain spaces (which is entirely normal on OS X).
  • It checks for errors while cding, and if there was a problem it exits (rather than deleting files in an unexpected location). Always check for errors on any script command that affects what the rest of the script will do; cd is a good example of this.
  • (Minor) It cds directly to the script's directory (rather than cding there, capturing that location in a variable, then cding there again based on the variable).
  • Thanks for the answer. The error checking contribution was definitely a nice improvement! – thiagoveloso Jul 7 '15 at 0:49
0

Based on this reference (https://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in), I ended up solving my problem with the following code:

#!/bin/bash

DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )

echo "Cleaning files..."

cd $DIR
rm *.aux
rm *.bak
rm *.bbl
rm *.blg
rm *.gz
rm *.log
rm *.pdf

echo "Done!"

#read -p "Press [Enter] to continue..."

It works very well to clean all the nasty files left behind by Texmaker!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .