Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 18
Hi all, I have multiple files that look like these: ImgA.1 ImgA.2 ImgA.3 .......... ImgA. 99 Could you suggest me options to replace A by B and increase the numbering ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2013
    Posts
    23

    Easiest way to modify multiple filenames all at once?


    Hi all,

    I have multiple files that look like these:

    ImgA.1
    ImgA.2
    ImgA.3
    ..........
    ImgA. 99

    Could you suggest me options to replace A by B and increase the numbering by 100 so that they end up looking like these ones?

    ImgB.101
    ImgB.102
    ImgB.103
    .........
    ImgB.199

    Thanks a lot!
    Cheers

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,307
    Hello and welcome!

    I've moved your post to the Programming/Scripting forum where it will hopefully get more attention.

  3. #3
    Just Joined!
    Join Date
    Feb 2013
    Posts
    23
    sounds good, thanks!

  4. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,307
    Quote Originally Posted by sponge View Post
    Hi all,

    I have multiple files that look like these:

    ImgA.1
    ImgA.2
    ImgA.3
    ..........
    ImgA. 99

    Could you suggest me options to replace A by B and increase the numbering by 100 so that they end up looking like these ones?

    ImgB.101
    ImgB.102
    ImgB.103
    .........
    ImgB.199

    Thanks a lot!
    Cheers
    Hi,

    Is this a homework question? It appears to be so, which is against Forum Rules.

  5. #5
    Just Joined!
    Join Date
    Feb 2013
    Posts
    23
    Nop,
    it has nothing to do with homework, just wanted to ask for suggestions in the forum so I know how to deal with it the next time.

  6. #6
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,307
    well, then i guess it's okay ;)

    there are many ways to do this. for example, some awk gurus could probably do it all in one-liner...but here's how I'd do it:

    Code:
    #!/bin/bash
    files=$(ls ImgA.[0-9]*)
    for file in $files; do
      num=$(echo $file|cut -f2 -d.)
      newnum=$(( $num + 100 ))
      newfile=$(echo $file|sed -e "s|^ImgA\.$num$|ImgB.$newnum|")
      echo mv $file $newfile
    done

  7. #7
    Just Joined!
    Join Date
    Feb 2013
    Posts
    23
    Thanks a lot Atreyu.Sorry for more questions, im quite a newbie. Is it [0-9] in the second line required?
    Wouldnt it be enough just ImgA.*?

  8. #8
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,307
    Quote Originally Posted by sponge View Post
    Thanks a lot Atreyu.Sorry for more questions, im quite a newbie. Is it [0-9] in the second line required?
    Wouldnt it be enough just ImgA.*?
    As long as you only had files with "ImgA." followed by a number in the directory. The point of the [0-9] regex is to match the number extension explicitly. So yeah, your wildcard notation is probably fine. I just got into the habit of matching on filenames as specifically as possible - it is good practice.

  9. #9
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    211
    hi,

    Code:
    #!/bin/bash
    
    shopt -s extglob nullglob
    
    for f in ./Img.+([0-9]); do echo mv "$f" "${f%.*}.$((${f##*.}+100))"; done
    rtfm

  10. #10
    Just Joined!
    Join Date
    Feb 2013
    Posts
    23
    Quote Originally Posted by atreyu View Post
    well, then i guess it's okay

    there are many ways to do this. for example, some awk gurus could probably do it all in one-liner...but here's how I'd do it:

    Code:
    #!/bin/bash
    files=$(ls ImgA.[0-9]*)
    for file in $files; do
      num=$(echo $file|cut -f2 -d.)
      newnum=$(( $num + 100 ))
      newfile=$(echo $file|sed -e "s|^ImgA\.$num$|ImgB.$newnum|")
      echo mv $file $newfile
    done

    So I ran into another problem. The files that I need to modify are numbered so that they start:

    001
    002
    003
    and so on...

    I adapted your script and executed it but im getting this issue:

    ' value too great for base '

    which seems to be due to incompatibility with the C language octal notation?

    Would it make sense to make a bash version of this script or would I run into the same problem?
    Thanks!

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •