Rename files by incrementing a number within the filename

I have a directory site which has phoned number photo documents, something similar to this:

01.png
02.png
03.png
03.svg
04.png
05.png
06.jpg
07.png
08.png
09.png
09.svg
10.png

Sometimes there might be numerous variations of a documents in various layouts (eg. a png and also svg variation of the 03 and also 09 documents over) yet the numbers are or else successive. Commonly there are 40 - 80 such documents in each directory site. The numbers represent the order these photos show up in a manuscript (a Word record, yet that is trivial). There is nothing else means to establish the order of the photos.

If I add a new photo to the manuscript I require to position a duplicate of the photo in this directory site with the proper numbering. So if the new photo is the 5th in the manuscript I require to rename the documents in the directory site to this in order to include it:

01.png
02.png
03.png
03.svg
04.png
06.png
07.jpg
08.png
09.png
10.png
10.svg
11.png

What is one of the most uncomplicated means from the command line, or from a manuscript or macro to renumber all the documents beginning at a particular number? I have a typical Fedora Linux install making use of bash.

21
2022-06-07 14:31:55
Source Share
Answers: 3

This specific concern is covered in this article. Keep in mind that you would certainly need to change it to sustain the SVG and also PNG layouts, by including a 2nd MV action.

1
2022-06-07 14:58:40
Source

I assume that it needs to do the job:

#!/bin/bash

NEWFILE=$1

for file in `ls|sort -g -r`
do
    filename=$(basename "$file")
    extension=${filename##*.}
    filename=${filename%.*}

    if [ $filename -ge $NEWFILE ]
    then
        mv "$file" "$(($filename + 1))".$extension
    fi
done

Script takes one parameter - variety of you new photo.

PS. Place manuscript in an additional directory site than your photos. In photos directory site there need to be just photos called this way that you defined.

17
2022-06-07 14:57:45
Source

This would certainly be less complicated in zsh, where you can make use of

  • the On glob qualifier to arrange suits in lowering order (and also n to make use of mathematical order, in instance the documents names do not all have leading absolutely nos to the very same size) ;
  • the (l:WIDTH::FILLER:) parameter expansion flag to pad all numbers to the very same size (the size of the bigger number)
break=$1   # the position at which you want to insert a file
setopt extended_glob
width=
for x in [0-9]*(nOn); do
  n=${x%%[^0-9]*}
  if ((n < break)); then break; fi
  ((++n))
  [[ -n $width ]] || width=${#n}
  mv $x ${(l:$width::0:)n}${x##${x%%[^0-9]*}}
done

In bash, below is a manuscript that thinks documents are cushioned to a dealt with size (or else, the manuscript will not rename the appropriate documents) and also pads to a dealt with size (which has to be defined).

break=$1      # the position at which you want to insert a file
width=9999    # the number of digits to pad numbers to
files=([0-9]*)
for ((i=#((${#files}-1)); i>=0; --i)); do
  n=${x%%[^0-9]*}
  x=${files[$i]}
  if ((n < break)); then continue; fi
  n=$((n + 1 + width + 1)); n=${n#1}
  mv -- "${files[$i]}" "$n${x##${x%%[^0-9]*}}"
done
5
2022-06-07 14:54:57
Source