Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Renaming Multiple Files 4

Status
Not open for further replies.

iaresean

Programmer
Mar 24, 2003
570
ZA
Hello All;

I have about 370+ files in a folder that I would like to rename so that they can be used by another script that I have written. I need to rename them all in a specific pattern/sequence. (e.g. file0001.txt file0002.txt file0003.txt)

I know that I could go through each file myself and rename it, but I think that I will find that a bit tedious and annoying. How could I write a script to do this for me when all the current files are almost completely randomly named? I need to run through each filename individually, perform an increment and then a rename.

Does anyone have an idea of how I could do this?

Sorry if my intentions aren't clear. :-(
If they aren't, just give me a shout and I will try to explain it a bit more clearly.

THANKS A HUGE BUNCH FOR ANY AND ALL HELP! :)

Sean. [peace]
 
Something like this ?
cd yourFolder
ls >/tmp/list.$$
awk '{
cmd=sprintf("mv \"%s\" file%04d.txt",$0,NR)
system(cmd)
}' /tmp/list.$$

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
This isn't tested so try it with copies of your files first

#!/bin/ksh

counter=1

for files in *
do
mv $files file${counter}.txt
counter=`expr $counter + 1`
done
 
How about
Code:
find . \( -type d ! -name . -prune \) -o \( -type f -print \) | while read file
do
    let num=$(expr $num+1)
    mv $file $num.txt
done
The fancy find simply stops the find recursing down into subdirectories.
Cheers, Neil
 
Try this...
Code:
typeset -Z cnt=0001
for file in *
do
  [ -f $file ] || continue
  mv $file file$cnt.txt
  let cnt+=1
done
 
Wow, thanks for all the suggestions guys. I really
appreciate the responses. I think I will go for PHV's
answer since he answered first.

A BIG THANKS TO YOU ALL THOUGH!

Sean. [peace]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top