简体   繁体   中英

Bash script to rename files and increment number in the file name by N

I have a bunch of numbered pages in a directory for a static website demo I'm creating.

They are:

  • wizard1.html
  • wizard2.html
  • ...
  • wizard30.html

And so on.

Occasionally, I've needed to add a screen or two and insert them somewhere in the middle. I then have to manually rename wizard15 to wizard16 and so on.

I would love a script that would allow me to rename the files beginning at a certain number (ie wizard3, or wizard19, etc) and how many numbers to increment it by (in case I'm adding two, three, or more new screens in the flow).

I do not know bash at all or I would attempt this myself.

This should work

filename=$1 #Filename
filenumber=$2 #file number from where you want to rename. 
count=$3 #number of files
lastfile=$4 #Last filenumber in the folder


for ((i=lastfile; i>=$filenumber; i--))
do
     mv $filename$i $filename`expr $i + $count`
done

You can run it like below

./rename wizard 15 3 30

Put this script in your dir. For example it will have name script.sh
Please, at first make test in some folder!

#!/bin/bash

# output files in dir
echo "------------------------------"
ls -la
echo "------------------------------"

echo -n "Enter starting number for modify: "
read old_starting_num

echo -n "Enter last number for modify: "
read old_last_num

# Check are there files from the range
for (( i=$old_starting_num; i<=old_last_num; i++))
   do
     if [[ ! -f wizard$i.html ]]
     then
     echo "ERROR: number from the range is not exist!"
     exit 1
     fi
done

# Make sure that last number => starting number
count=$(( $old_last_num - $old_starting_num ))
if [[ $count -ge 0 ]]
then

echo -n "Enter NEW starting number: "
read new_starting_num

diff=$(( $new_starting_num - $old_starting_num ))

     for i in $(seq $old_starting_num $old_last_num)
      do
           k=$(( $i + $diff ))
           mv -i -v wizard$i.html wizard$k.html
      done
else
  echo "ERROR: starting number > then last number!"
  exit 1

fi

Grant execution permission and run:

chmod +x script.sh
./script.sh

How does it work:

[root@al]# ./script.sh
------------------------------
итого 24
drwxr-xr-x   2 root root  4096 Сен 11 18:52 .
drwxr-xr-x. 27 root root  4096 Сен 11 18:16 ..
-rwxr-xr-x   1 root root   832 Сен 11 18:52 script.sh
-rw-r--r--   1 root root     0 Сен 11 18:18 wizard1.html
-rw-r--r--   1 root root     0 Сен 11 17:29 wizard5.html
-rw-r--r--   1 root root     0 Сен 11 18:37 wizard6.html
-rw-r--r--   1 root root     0 Сен 11 18:37 wizard7.html
------------------------------
Enter starting number for modify: 5
Enter last number for modify: 6
Enter NEW starting number: 20
«wizard5.html» -> «wizard20.html»
«wizard6.html» -> «wizard21.html»

or

[root@al]# ./script.sh
------------------------------
итого 12
drwxr-xr-x   2 root root 4096 Сен 13 15:44 .
drwxr-xr-x. 28 root root 4096 Сен 12 09:03 ..
-rwxr-xr-x   1 root root  928 Сен 13 15:44 script.sh
-rw-r--r--   1 root root    0 Сен 13 09:38 wizard1.html
-rw-r--r--   1 root root    0 Сен 13 09:31 wizard20.html
-rw-r--r--   1 root root    0 Сен 13 09:38 wizard21.html
-rw-r--r--   1 root root    0 Сен 13 09:38 wizard7.html
------------------------------
Enter starting number for modify: 20
Enter last number for modify: 23
ERROR: number from the ranage is not exist!


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM