[英]How to find and delete resized Wordpress images if the original image was already deleted?
This question pertains to the situation where这个问题与以下情况有关
mypicture.jpg
上传了一张图片,比如mypicture.jpg
mypicture-300x500.jpg
and mypicture-600x1000.jpg
Wordpress 创建了不同分辨率的多个副本,例如mypicture-300x500.jpg
和mypicture-600x1000.jpg
In this scenario, the remaining photos on the filesystem are mypicture-300x500.jpg
and mypicture-600x1000.jpg
.在这种情况下,文件系统上剩余的照片是mypicture-300x500.jpg
和mypicture-600x1000.jpg
。
How can you script this to find these "dangling" images with the missing original and delete the "dangling" images.您如何编写脚本以找到这些缺少原始图像的“悬空”图像并删除“悬空”图像。
I've written a Bash script that will attempt to find the original filename (ie mypicture.jpg
) based on scraping away the WordPress resolution (ie mypicture-300x500.jpg
), and if it's not found, delete the "dangling image" (ie rm -f mypicture-300x500.jpg
)我编写了一个 Bash 脚本,它将尝试根据刮掉 WordPress 分辨率(即mypicture-300x500.jpg
)来查找原始文件名(即mypicture.jpg
),如果找不到,请删除“悬空图像”(即rm -f mypicture-300x500.jpg
)
#!/bin/bash
for directory in $(find . -type d)
do
for image in $(ls $directory)
do
echo "The current filename is $image"
resolution=$(echo $image | rev | cut -f 1 -d "-" | rev | xargs)
echo "The resolution is $resolution"
extension=$(echo $resolution | rev| cut -f 1 -d "." | rev | xargs)
echo "The extension is $extension"
resolutiononly=$(echo $resolution | sed "s@.$extension@@g")
echo "The resolution only is $resolutiononly"
pattern="[0-9]+x[0-9]+"
if [[ $resolutiononly =~ $pattern ]]; then
echo "The pattern matches"
originalfilename=$(echo $image | sed "s@-$resolution@.$extension@g")
echo "The current filename is $image"
echo "The original filename is $originalfilename"
if [[ -f "$originalfilename" ]]; then
echo "The file exists $originalfilename"
else
rm -f $directory/$image
fi
else
break
fi
done
done
You could use find
:你可以使用find
:
$ find . -type f -regex '.*/mypicture-[0-9]+x[0-9]+\.jpg'
This will print a list of files.这将打印文件列表。 Check that it is correct and then, instead of printing the names of the files, tell find
to delete them:检查它是否正确,然后告诉find
删除它们,而不是打印文件名:
$ find . -type f -regex '.*/mypicture-[0-9]+x[0-9]+\.jpg' -exec rm -f {} \;
See man find
for the explanation.有关解释,请参见man find
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.