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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rename Files in a directory.

Status
Not open for further replies.

jayjaybigs

IS-IT--Management
Jan 12, 2005
191
0
0
CA
Hi All,

I have about a number files in a directory like so:

hello.txt
hallo.txt
hullo.txt
hollo.txt

Mu aim is rename them as follows:

hello1.txt
hello2.txt
hello3.txt
hello4.txt

Here is my effort, but I am not getting any love with this script....

#! /bin/bsh

$LIST=`ls *.txt`

for file in $LIST
do
filename='echo $file |sed -e `s/*.sql/*txt/`'
mv $file $filename
done

Your help will be appreciated.

JJ
 
Your script doesn't correspond with your requirements at all? Where did *.sql come from?

Annihilannic.
 
Firstly:
Is /bin/bsh a valid shell on your system?
Secondly:
The wrong punctuation and use of '*' is being used in filename='echo $file |sed -e `s/*.sql/*txt/`',
please try filename=`echo $file |sed 's/sql/txt/'`.
Additionally:
Please see the man pages for sed (man sed) where it shows that -e should be followed by a script.

I hope that helps.

Mike

 
#!/bin/ksh

COUNTER = 1
FILENAME="hello"
TXT="txt"

for I in `ls *.txt`
do
mv $I $FILENAME$COUNTER.$TXT
((COUNTER=${COUNTER}+1))
done
 
coffeysm,
and why exactly do you need an 'ls'?

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
I don't...you can just have for I in *.txt. I wrote it up real quick without thinking to much.
 
and why exactly do you need an 'ls'?
Just in case the directory has no .txt file:
for I in `ls *.txt 2>/dev/null`

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top