通过Linux终端消除缩略图,将上传目录中的媒体文件展平?

时间:2017-11-09 作者:cenk

我需要获取媒体库的源文件(我上载的初始文件),将目录结构展平,并删除wordpress使用linux/mac终端创建的较小图像。

uploads/2017/12/some-image-150x150.jpg
uploads/2017/12/some-image-360x240.jpg
uploads/2017/12/some-image-720x900.jpg
uploads/2017/12/some-image.jpg
对此:

uploads/some-image.jpg

1 个回复
最合适的回答,由SO网友:cenk 整理而成

Warning: 请先备份,因为这会造成不可挽回的损害。

转到上载目录:

cd /path/to/directory
cp -r uploads uploads.backup
展平上载目录中的文件:

find * -type f -exec bash -c \'file=${1#./}; echo mv "$file" "${file//\\//_}"\' _ \'{}\' \\;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2017/12/some-image-150x150.jpg  --->  2017_12_some-image-150x150.jpg
  2017/12/some-image-360x240.jpg  --->  2017_12_some-image-360x240.jpg
  2017/12/some-image-720x900.jpg  --->  2017_12_some-image-720x900.jpg
  2017/12/some-image.jpg          --->  2017_12_some-image.jpg   
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
! Delete the "echo" after you try, this only outputs what\'s gonna happen !
删除空目录:

find * -depth -type d -exec echo rmdir \'{}\' \\;
删除具有名称模式的较小文件:some\\u file[编号]x[编号]。jpg:

find * -type f -name \'*[0-9]x[0-9]*.jpg\' -delete;

结束