203k views
1 vote
1. Write a shell script, renumber.sh, that will rename all the files in a directory. The first argument is a base name, second argument is a file extension. If the script is run as: $ renumber.sh 25thAnniversary jpeg then the resulting files should have names like: 25thAnniversary001.jpeg, 25thAnniversary002.jpeg, 25thAnniversary003.jpeg, etc. Before renaming a file, make sure that a file with the new name does not already exist. If one does, handle the exception appropriately.

User Jeremie
by
5.1k points

2 Answers

6 votes

Answer:

cnt=$(( $cnt + 1 ))

Step-by-step explanation:

User Dskrvk
by
6.3k points
3 votes

Answer:

#!/bin/bash

if [ -z "$1" ] || [ -z "$2" ]

then

echo "invalid arguments"

exit 1

fi

filename="$1"

extension="$2"

cnt=1

for file in *. $extension

do

echo "renaming $file to $filename $cnt.$extension"

mv -i "$file" "$filename$cnt. $extension" #prompt before overwriting a file

cnt=$(( $cnt + 1 ))

done

Step-by-step explanation: