I need to get the source files (the initial files I have uploaded) of my media library flattening the directory structure and getting rid of the smaller images created by wordpress using linux/mac terminal.


to this:


1 Answer 1


Warning: Please make a backup first, as this can do irreversable damage.

Go to your uploads directory:

cd /path/to/directory
cp -r uploads uploads.backup

Flatten the files in uploads directory:

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 !

Remove empty directories:

find * -depth -type d -exec echo rmdir '{}' \;

Delete smaller files with name pattern: some_file[NUMBER]x[NUMBER].jpg:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.