1

I'm using the following command to move all files (non-recursively) ended in _128.jpg into the 128x160 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!

andreszs
  • 709
  • 1
  • 6
  • 16
  • Is this really faster than using exec? I believe it would be faster if you used `find ..... | xargs mv -t 128x160` because in that case it would move as many files it could with one `mv` command. The way you call it it runs `mv` as many times as there are files to be moved. Compare `find . -iname '*_128.jpg' | xargs -I '@' echo @ bla` to `find . -iname '*_128.jpg' | xargs echo bla` – Marki Feb 09 '14 at 16:56

1 Answers1

5

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.

krisFR
  • 13,280
  • 4
  • 36
  • 42