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)