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

file rename loop

Status
Not open for further replies.

rss0213

Programmer
Jul 12, 2007
58
US
Hi. I have files in a directory with names like:

1.tif
2.tif
3.tif
.
.
.
n.tif

I need to rename these files to YYYYMMDD_SEQ_CTR.tif, where 'YYYYMMDD' = date (duh), 'SEQ' = a pre-assigned sequence number, and 'CTR' = a counter that I just increment for each file in the dir. So, I have the following code:

ctr=0
for i in *.tif
do
let "ctr+=1"
newtifname=$DATEDIR"_"$SEQ"_"$ctr".tif"
mv "$i" $newtifname
done

But when I execute this code, I get this error:

mv: cannot stat `*.tif': No such file or directory

Anyone know what I'm doing wrong?

Thanks!
Scott
 


If there are no *.tif files in the current directory where the script is executing, then you will get that error.

Try this:
Code:
cd {the directory with the *.tif files}
ls -1 *.tif >/dev/null 2>&1
if [ $? -ne 0 ]; then
  echo "!Error: No *.tif files in `pwd`"
  exit 1
fi
ctr=0
for i in *.tif
do
  ((ctr+=1))
  newtifname=$DATEDIR"_"$SEQ"_"$ctr".tif"
  mv "$i" $newtifname
done
[3eyes]


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Ok, I see now. I thought there were files there because we have this code a little farther down that zips the tif files:

find $TMPPATH -name *.tif | zip -j $output_zip_file -@ >> $LOG

where TMPPATH is the same dir I'm processing in my for loop, but now I realize that the files are deeper down in the dir structure....

So I suppose I can incorporate my file rename into the find command? Is this possible?

Thanks!
Scott
 

If you did not have the "$cntr" you could.
[noevil]


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top