简体   繁体   中英

renaming files with a shell script

I have a lot of files following are the files

DSCN2352.JPG  DSCN2356.JPG  DSCN2360.JPG  DSCN2364.JPG
DSCN2353.JPG  DSCN2357.JPG  DSCN2361.JPG  DSCN2365.JPG

I want to rename these files so I wrote a script

#!/bin/bash
declare -a kl=( "$@" )
kl=$(ls *.JPG)
echo ${kl[@]}
p=0
for i in kl ;
 do mv $kl d$p.JPG
  $p=$p+1  ;
 done

I am getting error

mv: target `d0.JPG' is not a directory
./rename.sh: line 8: 0=0+1: command not found

UPDATE
after updating the script

#/bin/bash
p=0
for i in *.JPG;
do
mv "$i" "d$p.JPG"
(( p+1 ))  ;
done

files

DSCN2352.JPG  DSCN2356.JPG  DSCN2360.JPG  DSCN2364.JPG
DSCN2353.JPG  DSCN2357.JPG  DSCN2361.JPG  DSCN2365.JPG

disappear and I am left with only one file d0.jpg rest of the files get deleted after executing updated script.

Change

$p=$p+1  ;

to

(( p=p+1 ))

or simply

(( p++ ))

Good day.

p=0
for i in *.JPG
do
  mv "$i" d$p.JPG
  p=$(($p+1))
done

You could do something like this:

#!/bin/bash
p=0
for file in ./*.JPG ; do
    echo "$file"
    mv $file d$p.JPG
    p=`expr $p + 1`
done

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