regex - Batch rename files by adding +1 to prefix number in a filename -


so, question is: how add +1 prefix number in filename?

the goal go rename multiple files go this:

1_loremipsum_and_stuff_2013.pdf 2_loremipsum_and_stuff_2013.pdf 3_loremipsum_and_stuff_2013.pdf 4_loremipsum_and_stuff_2013.pdf 13_loremipsum_and_stuff_2013.pdf 18_loremipsum_and_stuff_2013.pdf 19_loremipsum_and_stuff_2013.pdf 20_loremipsum_and_stuff_2013.pdf 

to this:

2_loremipsum_and_stuff_2013.pdf 3_loremipsum_and_stuff_2013.pdf 4_loremipsum_and_stuff_2013.pdf 5_loremipsum_and_stuff_2013.pdf 14_loremipsum_and_stuff_2013.pdf 19_loremipsum_and_stuff_2013.pdf 20_loremipsum_and_stuff_2013.pdf 21_loremipsum_and_stuff_2013.pdf 


i'm not experienced terminal @ all. able find examples removing prefix number. works fine, when try replace using regex, can't close.

so after first trying , failing horribly @ correctly regexing in terminal, thought i'd give try in javascript.

i able working in javascript /[0-9]*(?=_)/.

so, best guess terminal this, except doesn't quite work.:

cd  {testfolder}  regex=[0-9]*(?=_)  name in *; mv -v "$name" "${name/$regex/$(( ${name/$regex}+1 ))}"; done 

using bash string manipulation:

s='1_loremipsum_and_stuff_2013.pdf' mv "$s" "$((${s%%_*}+1))_${s#*_}" 

edit: based on discussion below can use

while read f;     mv "$f" "$((${f%%_*}+1))_${f#*_}" done < <(sort -t_ -rnk1,2 <(printf "%s\n" *_*)) 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -