rename files in a folder with date&time stamp

Hi,

I want to rename all the files (more than 100 files) in a fodler to another folder with date&time stamp.

foe eg,

file1.dat
file2.dat
file3.dat
..

to be renamed as

file1100629_16_30_15.txt (yy-mon-dd_hh_mi_ss)
file1100629_16_30_16.txt
..
so on

Thanks & Regards,

You do know about cp -p - preserves file times and permissions?

filetime()
{
perl -e '
      $mtime = (stat("$ARGV[0]"))[9];

      # time structure into variables

      ($sec,$min,$hr,$day,$mon,$yr,$wday,@dntcare) = localtime($mtime);
      $yr = ($yr>=70) ? $yr+1900 : $yr+2000;
      $yr=$yr%100;
      $mon+=1;

      printf ("%d%02d%02d-%02d-%02d-%02d", $yr,$mon,$day,$hr,$min, sec); ' "$1"
}
cd /old/path
for fname in *
do
   newname=${fname%%.*}$(filetime $fname).txt
   mv $fname /another/path/${newname}
done
  

Hi Jim,

Thanks for your reply. i tried that it is working fine.

Could you please help me, whether is it possible to add date&time stamp option in the below script.

for i in 'ls ../in '
do
mv ../in/$i ../prc/$i ---i want to add here --
done

Thanks

---------- Post updated 06-30-10 at 11:30 AM ---------- Previous update was 06-29-10 at 05:02 PM ----------

Hi Jim,

filetime()
{
perl -e '
$mtime = (stat("$ARGV[0]"))[9];
# time structure into variables
($sec,$min,$hr,$day,$mon,$yr,$wday,@dntcare) = localtime($mtime);
$yr = ($yr>=70) ? $yr+1900 : $yr+2000;
$yr=$yr%100;
$mon+=1;
printf ("%d%02d%02d-%02d-%02d-%02d", $yr,$mon,$day,$hr,$min, sec); ' "$1"
}
for i in *
do
newname=${i%%.}$(filetime $i).txt
mv $i ../prc/${newname}
done
------up to this am getting correct result----
cd ../prc
for i in *
do
echo $i
newname=${i%%.
}$(filetime $i).txt
mv $i ../his/${newname}
done

---------am getting result like this-
file1100630-10-36-00100630-10-36-00.txt
file2100630-10-36-00100630-10-36-00.txt

Could you pelase help me.

Thanks