简体   繁体   English

如果原始图像已被删除,如何查找和删除调整大小的 Wordpress 图像?

[英]How to find and delete resized Wordpress images if the original image was already deleted?

This question pertains to the situation where这个问题与以下情况有关

  1. An image was uploaded, say mypicture.jpg上传了一张图片,比如mypicture.jpg
  2. Wordpress created multiple copies of it with different resolutions like mypicture-300x500.jpg and mypicture-600x1000.jpg Wordpress 创建了不同分辨率的多个副本,例如mypicture-300x500.jpgmypicture-600x1000.jpg
  3. You delete the original image only您只删除原始图像

In this scenario, the remaining photos on the filesystem are mypicture-300x500.jpg and mypicture-600x1000.jpg .在这种情况下,文件系统上剩余的照片是mypicture-300x500.jpgmypicture-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.

相关问题 Linux:如果删除了快捷方式/链接,如何删除原始文件 - Linux: How to delete the original file if the shortcut/link is deleted 删除原始文件夹后删除符号链接 - Delete a symbolic link when the original folder is deleted Rsync使用find命令删除原始文件,用符号链接替换原文 - Rsync using find command, delete original, replace original with symlink Linux查找和删除文件,但重定向要删除的文件名 - Linux find and delete files but redirect file names to be deleted 如何通过使用新名称保存调整大小的版本来调整图像大小? - How to resize an image by saving the resized version under a new name? 在perforce中删除其客户端工作区后,如何删除某些目录? - How to delete some directories after their client workspaces are deleted in perforce? shell find -delete - 如何避免删除自身 - shell find -delete — how to avoid delete itself 使用符号链接linux时如何使原始文件不被删除 - How to make original file not delete when using symbolic link linux 单个命令查找文件,压缩并移动到其他位置,删除原始文件 - Single command to find files, zip and move to different location, delete the original files 如何使用grep查找“ ../images/” - How to Use grep to find '../images/'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM