1

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.

1
  • style your code correctly, it won't let me edit it for some reason. Commented Jul 25, 2015 at 14:48

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.