
After finishing an endless paper, a presentation full of pictures, or even your own thesis, have you ever had to deal with the image’s directory size? The number of images may grow faster because you probably are more concerned about the main subject, the LaTeX document itself, than the images that come and go. Also, other files can parasite your final project, helping it to grow and scare everyone who asks you to share the source. I would like to present a little piece of code written in Bash that may help you. This article is also for me to test the Medium platform. So let’s go!
First of all, I will assume you have the path $projectFolder and the main file name $mainfilename of your main LaTeX file, without the extension ‘.tex’. Moreover, I assume your LaTeX interpreter generates a log file $mainfile'.log' since this is used to do the job. If we are all good, the following script will remove all unnecessary files hidden inside directories from your project area. All of them are moved to the directory $nonUsed .
nonUsed="./nonUsedFiles"
mkdir -p "$nonUsed"
# Directory Level 1
for imgFolder in $(ls -d "$projectFolder"/*/); do
echo "$imgFolder"
for imageFile in $(ls "$imgFolder"); do
# echo "$imageFile"
if grep "$imageFile" "$projectFolder/$mainfilename.log" -c > 1; then
echo "+ File $imageFile is in use."
else
echo "- File $imageFile is not in use."
mkdir -p $nonUsed"/"$imgFolder
mv "$imgFolder/$imageFile" "$nonUsed/$imgFolder$imageFile"
fi
done
done
This is a helpful but still limited script since it removes only the one-level files. You can either do another script for other levels in your folders, e.g.
# Directory Level 2
for imgFolder in $(ls -d "$projectFolder"/*/*/); do
echo "$imgFolder"
for imageFile in $(ls "$imgFolder"); do
# echo "$imageFile"
if grep "$imageFile" "$projectFolder/$mainfilename.log" -c > 1; then
echo "+ File $imageFile is in use."
else
echo "- File $imageFile is not in use."
mkdir -p $nonUsed"/"$imgFolder
mv "$imgFolder/$imageFile" "$nonUsed/$imgFolder$imageFile"
fi
done
done
or just improve the script to make it better. This is only a message we can use the LaTeX log files to help us improving storage space. Have fun! (and report possible typos or malfunctionings…)