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:
Explanation
- Make a temporary folder
images2
- Extract2 all the relative paths to files in
images/
fromslides.qmd
and move these files into the temporary folder. - Remove the old
images/
folder that now only has unused images in it. - 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
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}
}