Krishna Moorthy
Krishna Moorthy

Reputation: 73

Rename multiple folders using shell script

How do I rename multiple user defined folder/files?

Say for Ex. I have multiple folders like krish, moorthy, ravi, robert, etc..

I want to rename all these directories as script_1 , script_2, script_3 etc. I tried below script but it doesn't produce an output:

for i in *
do
mv $* $script_'$i'
done

While executing, it says it cannot move, cannot stat *

Please help me to go through this.

Upvotes: 1

Views: 77

Answers (2)

adelphus
adelphus

Reputation: 10326

j=1;
for i in $(ls); do
   mv $i script_$j && j=$[$j +1]; 
done

for i in ... returns the value as i, not the index. Here, I've just added a new variable j as the incrementing index.

Upvotes: 0

dramzy
dramzy

Reputation: 1429

This is what you want:

#! /bin/bash
s=1
for i in *
do
  mv  $i  "script_$s"
  s=$((s+1))
done

i in the loop represents the current file/directory; it's not an index, so you need a separate indexing variable, I called it s.

Upvotes: 1

Related Questions