I have a folder of images (around 500) that have a filename structure like:
foo-bar-1_UPCNUMBER.jpg
foo-bar-1_UPCNUMBER_1.jpg
foo-bar-1_UPCNUMBER_2.jpg
foo-bar-2_UPCNUMBER.jpg
foo-bar-2_UPCNUMBER_1.jpg
etc ..
The goal is to use Terminal to iterate over all images in the folder, remove all parts of a string AT or BEFORE the first occurrence of "_" so the desired outcome would be:
UPCNUMBER.jpg
UPCNUMBER_1.jpg
UPCNUMBER_2.jpg
OTHER-UPCNUMBER.jpg
etc ..
I have tried using this script suggested by a friend but it's not working at all.
for f in file1.some.stuff.mp3 file2.other.stuff.stuff.mp3 file3.some.thing.mp3 file4.one.two.three.four.mp3; do
echo "${f%%.*}.${f##*.}"
done
Is this doable using Terminal or is there some software I can use/buy that does this in bulk? I also tried using Photoshop batch operations but it doesn't work. Thank you.
I have a folder of images (around 500) that have a filename structure like:
foo-bar-1_UPCNUMBER.jpg
foo-bar-1_UPCNUMBER_1.jpg
foo-bar-1_UPCNUMBER_2.jpg
foo-bar-2_UPCNUMBER.jpg
foo-bar-2_UPCNUMBER_1.jpg
etc ..
The goal is to use Terminal to iterate over all images in the folder, remove all parts of a string AT or BEFORE the first occurrence of "_" so the desired outcome would be:
UPCNUMBER.jpg
UPCNUMBER_1.jpg
UPCNUMBER_2.jpg
OTHER-UPCNUMBER.jpg
etc ..
I have tried using this script suggested by a friend but it's not working at all.
for f in file1.some.stuff.mp3 file2.other.stuff.stuff.mp3 file3.some.thing.mp3 file4.one.two.three.four.mp3; do
echo "${f%%.*}.${f##*.}"
done
Is this doable using Terminal or is there some software I can use/buy that does this in bulk? I also tried using Photoshop batch operations but it doesn't work. Thank you.
Share Improve this question asked Mar 31 at 17:01 TonyTony 133 bronze badges 7 | Show 2 more comments1 Answer
Reset to default 2This might be what you're trying to do (untested):
shopt -s nullglob
for f in *_*; do
mv -i -- "$f" "${f#*_}"
done
xxx_UPCNUMBER.jpg
. – Marco Bonelli Commented Mar 31 at 17:18f
, less all leading characters up to and including the first_
if a_
appears at all, would be spelled${f#*_}
. That could be used infor
loop a little like the one presented in the question. However, thefor
loop in the question itself seems intended for a different task. – John Bollinger Commented Mar 31 at 17:30OTHER-UPCNUMBER.jpg
in the output when no file name like that was in the input. Please fix the example so the output you show is exactly the output you expect from the input you show and if the transformation for any of the file names isn't obvious then please explain it. Dont repeat the string "UPCNUMBER" everywhere, make up some numbers. In every question give us a minimal reproducible example including concise, testable sample input and expected output that we can copy/paste to test a potential solution against – Ed Morton Commented Mar 31 at 18:23