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.
- 02-13-2013 #1Just Joined!
- Join Date
- Feb 2013
- Posts
- 16
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
- 02-13-2013 #2Trusted Penguin
- Join Date
- May 2011
- Posts
- 3,664
Hello and welcome!
I've moved your post to the Programming/Scripting forum where it will hopefully get more attention.
- 02-13-2013 #3Just Joined!
- Join Date
- Feb 2013
- Posts
- 16
sounds good, thanks!
- 02-13-2013 #4Trusted Penguin
- Join Date
- May 2011
- Posts
- 3,664
Hi,
Is this a homework question? It appears to be so, which is against Forum Rules.
- 02-13-2013 #5Just Joined!
- Join Date
- Feb 2013
- Posts
- 16
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.
- 02-13-2013 #6Trusted Penguin
- Join Date
- May 2011
- Posts
- 3,664
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
- 02-13-2013 #7Just Joined!
- Join Date
- Feb 2013
- Posts
- 16
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.*?
- 02-13-2013 #8Trusted Penguin
- Join Date
- May 2011
- Posts
- 3,664
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.
- 02-13-2013 #9Linux Newbie
- Join Date
- Nov 2012
- Posts
- 134
hi,
rtfmCode:#!/bin/bash shopt -s extglob nullglob for f in ./Img.+([0-9]); do echo mv "$f" "${f%.*}.$((${f##*.}+100))"; done
- 02-13-2013 #10Just Joined!
- Join Date
- Feb 2013
- Posts
- 16
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!


Reply With Quote

