这个问题涉及以下情况
- 图片已上传,例如
mypicture.jpg
- Wordpress 使用不同的分辨率创建了多个副本,例如
mypicture-300x500.jpg
和mypicture-600x1000.jpg
- 您仅删除原始图像
在此场景中,文件系统上的剩余照片为 mypicture-300x500.jpg
和 mypicture-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 emptyread
delimiter (-d ''
) to separate the file names with theNUL
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:
find
action but here it is needed because-exec
is one of thefind
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 find
和 man 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/