Linux – Find and rename files removing suffix

findlinuxrename

I'm using the following command to move all files (non-recursively) ended in _128.jpg into the 128×160 subdir. This works great.

find . -iname '*_128.jpg' | xargs -I '{}' mv {} 128x160

But I also need to remove the _128 suffix from each file. Also, I must keep my current xargs method, making an exec for each would make the process extremely longer.

Thanks in advance for your cooperation!

Best Answer

Something like this should do the trick :

find . -iname '*_128.jpg' | xargs -I % sh -c 'newname=$(echo % | sed "s/_128//"); mv % 128x160/$newname'

Here i have used a multiple commands approach using sh -c 'command1; command2' and sed to clear _128 in the filename.