移动图像在一组Markdown文件中未链接到垃圾文件夹

发布于 2025-02-04 08:29:56 字数 603 浏览 4 评论 0 原文

在一个文件夹中,我有一些链接到某些PNG或JPG图像的链接。图像保存在附件子折中。

现在,如果我从 markdown文件中删除图像的链接(SAS ![](attactments/fig1.png)),当然,相应的图像文件不是被删除。我想将该图像删除到.trash子文件夹中。我为此编辑了一个简短的shell脚本(在),但它无济于事!

#!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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瑶笙 2025-02-11 08:29:56

这是对我有用的Python代码。

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}')

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}')
梅窗月明清似水 2025-02-11 08:29:56

1。查找所有图像文件

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

2。创建 awk 脚本 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。打印所有无与伦比的文件 mv 命令

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

4。执行所有 MV 一次命令

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

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 = "^
quot;; # 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文