Changing file names

I have a series of files as follows

file-1.pdf
file-2.pdf
file-3.pdf
file-4.pdf
file-5.pdf
file-6.pdf
file-7.pdf

I want to have the file names with odd numbers
starting from an initial number, for example 2000.

The result would be the following:

file-2001.pdf
file-2003.pdf
file-2005.pdf
file-2007.pdf
file-2009.pdf
file-2011.pdf
file-2013.pdf
file-2015.pdf

What have you tried?

I thought I could do it in one line, however my idea
is to use a bash script.

We are not a free scriptwriting service. We will help with problems but we cannot do absolutely everything. Please post your attempt.

#!/bin/bash

files=`find . -name 'kay_even*.png.pdf'` 

digits=2001
for fl in $files ; do
  printf "$fl.$digits\n"
  digits=$(($digits + 2))
done

---------- Post updated at 03:10 PM ---------- Previous update was at 03:01 PM ----------

The thing I have to do is to remove the original number
from the file name.

#!/bin/bash

files=`find . -name 'kay_even*.png.pdf'` 

digits=2001
for fl in $files ; do
  fname=`echo $fl | awk 'BEGIN {FS=".png.pdf"} {print $1}'`
  printf "$fname.$digits.png.pdf\n"
  digits=$(($digits + 2))
done

---------- Post updated at 03:39 PM ---------- Previous update was at 03:10 PM ----------

I have got somewhere now

#!/bin/bash

files=`find . -name 'kay.even*.png.pdf'` 

digits=2001
for fl in $files ; do
  fname=`echo $fl | awk 'BEGIN {FS=".png.pdf"} {print $1}'`
  fnew=`echo $fname | awk 'BEGIN {FS="."} {print $1"."$2"."$3}'`
  printf "fname: $fname\n"  
  printf "fnew: $fnew\n"  
  printf "$fnew.$digits.png.pdf\n"
  digits=$(($digits + 2))
done

You could also try something like this:

#!/bin/bash

files=$(find . -name "kay.even*.png.pdf")

num=2001
for fl in $files
do
   prefix=${fl%%[0-9]*}
   suffix=${fl##*[0-9]}
   ((num+=2))
   echo mv $fl $prefix$num$suffix
done
1 Like

I'm very glad to see that you're making progress, but I don't see how any of the code you have shown us can process the files you described in the 1st message in this thread???

How will find . -name 'kay.even*.png.pdf give you a list of files in the current directory with names:

file-1.pdf
file-2.pdf
file-3.pdf
file-4.pdf
file-5.pdf
file-6.pdf
file-7.pdf

What is the real format specification for the filenames you're trying to rename?

Are the files you want to rename all in the current directory; or are they scattered thoughout the file hierarchy rooted in the current directory?

Is there any chance that an existing filename might be one of the names you will try to create with your renumbering scheme? (If there is a chance of that, the scripts you have shown us so far could all destroy one or more of your input files in the renaming process.)

How about

ls -1 *.pdf | awk -v No=2001 '{printf "mv %s ", $0; sub (/-[0-9]*\./,"-"No"."); printf "%s\n", $0; No+=2}'

If happy with what you see, add a | sh to the end.

Had changed the file names to make splitting the fields more convenient. The files are all in the current directory.
Had chosen 2000 to ensure no existing files start with that
number, so no chance that an existing filename might be one
of the names I will try to create with the renumbering scheme.

I repeat: What is the real format specification for the filenames you're trying to rename?