Flatten media files in uploads directory via linux terminal eliminating thumbnails?

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.

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

to this:

uploads/some-image.jpg

Topic mac uploads linux Wordpress

Category Web


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;

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.