Remove unused files from (quarto) markdown projects

How to use the command line to clean up images and other files once they are no longer needed in a quarto document
quarto
markdown
how-to
Author

Cynthia Huang

Published

December 15, 2023

Modified

June 20, 2024

Often when I remove images or files from a quarto markdown document I don’t delete the actual files. This is particularly true if I’ve just “dragged and dropped” or “copy-pasted” an image into a document via visual mode. This can lead to unused images cluttering my images/ folder. Here’s how I cleaned up some “dangling” images that I was no longer using in my quarto slides.

Before/After

Before pruning my project folder looked something like this:

.
├── images
│   ├── 3MT-slide-final-faculty.png
│   ├── diagram-data-transform-script-v2.png
│   ├── diagram-data-transform-script.png
│   ├── diagram-data-transform-xmap-v2.png
│   ├── diagram-data-transform-xmap.png
│   ├── diagram_cross-tax.png
│   ├── diagram_expost-tasks.png
│   ├── icon-IEEE-VIS.png
│   ├── icon-aus-src-data.png
│   ├── icon-aus-target-data.png
│   ├── icon-database.png
│   ├── icon-official-stats.png
│   ├── icon-official-stats.svg
│   ├── isiccomb_rev3_TRUEonly.png
│   ├── plot-anzsco-isco-bigraph-only.png
│   ├── plot-anzsco-isco-bigraph.png
│   ├── plot-isiccomb-by-income-groups.png
│   └── plot-isiccomb-by-income-groups.svg
├── references.bib
├── slides.html
└── slides.qmd

After clean-up:

.
├── images
│   ├── diagram-data-transform-script-v2.png
│   ├── diagram-data-transform-xmap-v2.png
│   ├── diagram_expost-tasks.png
│   ├── plot-anzsco-isco-bigraph-only.png
│   └── plot-isiccomb-by-income-groups.svg
├── references.bib
├── slides.html
└── slides.qmd

Code

To clean up the unused images, I ran these Bash/Zsh commands from inside my quarto project1:

mkdir images2
for file in $(ggrep -oP '(?<=\]\()images/.*?(?=\))' slides.qmd);
    do mv -v $file images2/;
done
rm -rf images
mv images2 images

Explanation

  1. Make a temporary folder images2
  2. Extract2 all the relative paths to files in images/ from slides.qmd and move these files into the temporary folder.
  3. Remove the old images/ folder that now only has unused images in it.
  4. Rename the temporary folder to images/

Requirements & Caveats

The command in lines 2-5 requires GNU grep which supports Perl Compatible Regular Expressions, but by default macOS has BSD grep pre-installed. You can install GNU grep using Homebrew via: brew install grep.

Footnotes

  1. You could also put them into a script and run the script, or even alias it in your shell profile, but I couldn’t be bothered↩︎

  2. I asked ChatGPT to generate the regex for me, so no guarantees on performance or correctness. It worked for me in the projects I was cleaning up but that’s all I can say.↩︎

Citation

BibTeX citation:
@online{huang2023,
  author = {Huang, Cynthia},
  title = {Remove Unused Files from (Quarto) Markdown Projects},
  date = {2023-12-15},
  url = {https://www.cynthiahqy.com/posts/remove-unused-files},
  langid = {en}
}
For attribution, please cite this work as:
Huang, Cynthia. 2023. “Remove Unused Files from (Quarto) Markdown Projects.” December 15, 2023. https://www.cynthiahqy.com/posts/remove-unused-files.