最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

bash - How to remove part of image filename, based on first occurence of a particular character "_"? Using ter

programmeradmin2浏览0评论

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
  • 2 remove all parts of a string AT or BEFORE the first occurrence of "_" - this would create duplicates. How do you solve the conflict when multiple (like 2, 3 or more) files have the same suffix? Like xxx_UPCNUMBER.jpg. – Marco Bonelli Commented Mar 31 at 17:18
  • The value of variable f, less all leading characters up to and including the first _ if a _ appears at all, would be spelled ${f#*_}. That could be used in for loop a little like the one presented in the question. However, the for loop in the question itself seems intended for a different task. – John Bollinger Commented Mar 31 at 17:30
  • 1 Regarding "I have tried using this script..." - none of the file names in that script match the format of the file names you say you need to work on and it doesn't attempt to rename any of the files so those issues may be why it doesn't work for you. – Ed Morton Commented Mar 31 at 18:19
  • @MarcoBonelli: There shouldn't be any duplicates, as each UPC number is unique and the ones that repeat themseves – Tony Commented Mar 31 at 18:22
  • 2 You have 5 sample input lines but only 4 expected output lines from that input. You also have a file OTHER-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
 |  Show 2 more comments

1 Answer 1

Reset to default 2

This might be what you're trying to do (untested):

shopt -s nullglob
for f in *_*; do
    mv -i -- "$f" "${f#*_}"
done

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论