linux - 如果原始图像已被删除,如何查找并删除调整大小的 WordPress 图像?

标签 linux wordpress bash

这个问题涉及以下情况

  1. 图片已上传,例如 mypicture.jpg
  2. Wordpress 使用不同的分辨率创建了多个副本,例如 mypicture-300x500.jpgmypicture-600x1000.jpg
  3. 您仅删除原始图像

在此场景中,文件系统上的剩余照片为 mypicture-300x500.jpgmypicture-600x1000.jpg

如何编写此脚本来查找这些缺少原始图像的“悬空”图像并删除“悬空”图像。

最佳答案

您可以使用 find 通过 -regex 测试查找所有较低分辨率的图片:

find . -type f -regex '.*-[0-9]+x[0-9]+\.jpg'

这比尝试解析 ls 输出要好得多,后者仅适用于人类,不适用于自动化。因此,一个更安全(更简单)的 bash 脚本可以是:

#!/usr/bin/env bash

while IFS= read -r -d '' f; do
  [[ "$f" =~ (.*)-[0-9]+x[0-9]+\.jpg ]] &&
  ! [ -f "${BASH_REMATCH[1]}".jpg ] &&
  echo rm -f "$f"
done < <(find . -type f -regex '.*-[0-9]+x[0-9]+\.jpg' -print0)

(一旦您确信它按预期工作,请删除 echo)。

Note: we use the -print0 action and the empty read delimiter (-d '') to separate the file names with the NUL character instead of the newline character. This is preferable because it works as expected even if you have unusual file names (e.g., with spaces).

Note: as we test the file name inside the loop we could simply search for files (find . -type f -print0). But I suspect that if you have a large number of files the performance would be negatively impacted. So keeping the -regex test is probably better.

Bash 循环没问题,但当迭代次数增加时,它们往往会变得非常慢。因此,让我们将简单的 bash 脚本合并到带有 -exec 操作的单个 find 命令中:

find . -type f -exec bash -c '[[ "$1" =~ (.*)-[0-9]+x[0-9]+\.jpg ]] &&
  ! [ -f "${BASH_REMATCH[1]}".jpg ]' _ {} \; -print

Note: bash -c takes a script to execute as first argument, then the positional parameters to pass to the script, starting with $0. This is why we pass _ (my favourite for don't care), followed by {} (the current file path).

Note: -print is normally the default find action but here it is needed because -exec is one of the find actions that inhibit the default behaviour.

这将打印文件列表。检查它是否正确,一旦您满意,请添加 -delete 操作:

find . -type f -exec bash -c '[[ "$1" =~ (.*)-[0-9]+x[0-9]+\.jpg ]] &&
  ! [ -f "${BASH_REMATCH[1]}".jpg ]' _ {} \; -delete -print

请参阅 man findman bash 了解更多说明。

演示:

$ touch mypicture.jpg mypicture-300x500.jpg mypicture-600x1000.jpg
$ find . -type f -exec bash -c '[[ "$1" =~ (.*)-[0-9]+x[0-9]+\.jpg ]] &&
  ! [ -f "${BASH_REMATCH[1]}".jpg ]' _ {} \; -print
$ rm -f mypicture.jpg
$ find . -type f -exec bash -c '[[ "$1" =~ (.*)-[0-9]+x[0-9]+\.jpg ]] &&
  ! [ -f "${BASH_REMATCH[1]}".jpg ]' _ {} \; -print
./mypicture-300x500.jpg
./mypicture-600x1000.jpg
$ find . -type f -exec bash -c '[[ "$1" =~ (.*)-[0-9]+x[0-9]+\.jpg ]] &&
  ! [ -f "${BASH_REMATCH[1]}".jpg ]' _ {} \; -delete -print
./mypicture-300x500.jpg
./mypicture-600x1000.jpg
$ ls *.jpg
ls: cannot access '*.jpg': No such file or directory

One last note: if, by accident, one of your full resolution picture matches the regular expression for lower resolution pictures (e.g., if you have a balloon-1x1.jpg full resolution picture) it will be deleted. This is unfortunate but according your specifications there is no easy way to distinguish it from an orphan lower resolution picture. Be careful...

关于linux - 如果原始图像已被删除,如何查找并删除调整大小的 WordPress 图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70344013/

相关文章:

c++ - 如何在通过 Wine(同一台计算机)运行的 linux 程序和 windows 程序之间共享内存?

linux - 在 Linux 中重新启动处理

linux - 在 powershell 脚本中转义 bash 代码序列

css - 在 Wordpress TopBar 插件 @media 屏幕中更改内容(文本)

shell - 命令/别名可以可变吗?

c++ - 类型转换与 memcpy() : which one is better?

javascript - 根据页面的当前 url 打开一个新窗口

java - "external"java 网站的 session

linux - 如何使用一个命令行显示第三个字段中的最高值

bash - 创建带有校验和验证的文件下载脚本