Home > database >  Moving images that are not linked to in a set of markdown files to a trash folder
Moving images that are not linked to in a set of markdown files to a trash folder

Time:06-05

In a folder, I have some markdown files that have links to some png or jpg images. The images are saved in the attachments sub-folder.

Now if I delete an image's link (say ![](attachments/fig1.png)) from a markdown file, the corresponding image file, of course, doesn't get removed. I want to remove that image to a .trash sub-folder. I compiled a short shell script for that (with help from https://www.stevemar.net/remove-unused-images/), but it does nothing!

#!zsh

imagepaths=$(find . -name '*.jpg' -o -name '*.png')

for imagepath in $imagepaths; do
    filename=$(basename -- $imagepath)
    if ! grep -q --exclude-dir=".git" $filename .; then
        mv $imagepath ./.trash
    fi
done

CodePudding user response:

1. find all images files

find . -type f -name "*.jpg" -or -name "*.png"

2. create awk script script.awk

BEGIN { # before processing input file file "markdown.txt"
  RS = "^$"; # read input file as a single string
  # converst filesList into array filesArr
  split(filesList,filesArr); 
}
{ # process input file "markdown.txt" as single string
  for(i in filesArr) { # for each file-name in filesArr
    if ($0 ~ filesArr[i]) { # if current file-name matched in input file
      delete filesArr[i]; # delete current file-name from filesArr
    }
  }
}
END { # after processing input file file "markdown.txt"
  for(i in filesArr) { # for each unmatched file-name in filesArr
    printf("mv \"%s\" ./.trash\n", filesArr[i]); # print "mv" command
  }
}

3. print all unmatched files mv commands

awk -f script.awk -v filesList="$(find . -type f -name "*.jpg" -or -name "*.png")" markdown.txt

4. execute all mv command at once

bash <<< $(awk -f script.awk -v filesList="$(find . -type f -name "*.jpg" -or -name "*.png")" markdown.txt)

CodePudding user response:

This is the python code that worked for me.

import shutil
from pathlib import Path


cwd = Path.cwd()
attachment_folder = cwd / '../attachments'
note_folder = cwd / '../'
trash_folder = Path('../Trash')

all_note_paths = list(note_folder.glob('*.md'))
all_attachment_paths = list(attachment_folder.glob('*.*'))

all_texts = ''

for note_path in all_note_paths:
    with open(note_path, 'r') as f:
        all_texts  =  f.read()

for attachment_path in all_attachment_paths:
    if attachment_path.stem not in all_texts:
        print(f'{attachment_path.name} moved to Trash')
        shutil.move(attachment_path, trash_folder/f'{attachment_path.name}')
  • Related