rename files in a folder with date&time stamp


 
Thread Tools Search this Thread
Operating Systems Linux rename files in a folder with date&time stamp
# 1  
Old 06-29-2010
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,
# 2  
Old 06-29-2010
You do know about cp -p - preserves file times and permissions?

Code:
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

# 3  
Old 06-30-2010
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Files with date and time stamp

Hi Folks, Need a clarification on files with date and time stamp. Here is my requirement. There is a file created everyday with the following format "file.txt.YYYYMMDDHHMMSS". Now i need to check for this file and if it is available then i need to do some task to the file. I tried... (6 Replies)
Discussion started by: jayadanabalan
6 Replies

2. Shell Programming and Scripting

List Files Based On Time & Date

Hi All, I am using HP Unix. I want to list files which are created 5 minutes before on the same day as well as before today's date. I checked all the forums and the commands provided there does not work on HP Unix. Can you please help me on this? Your help is highly aprreciated. Thanks and... (3 Replies)
Discussion started by: angshuman
3 Replies

3. Shell Programming and Scripting

Display files created on particular date & time

Hi , I have BASH system & i am trying to display the files created on a particular date and time, and after displaying those files I also want to delete all those files.Can anyone of you help me out for this............. Thanx Original post contents restored... Please do not erase the question... (3 Replies)
Discussion started by: rakeshtomar82
3 Replies

4. Emergency UNIX and Linux Support

Is there any way to set the files modified date and stamp to last modifies time?

Actually i did modification in a file on server by mistake, now its showing current time stamp, is there any way to set the files modified date and stamp to last modifies time. Please advice here.Thanks in advance.:b: (7 Replies)
Discussion started by: saluja.deepak
7 Replies

5. Shell Programming and Scripting

If(Condition) Rename a file with (Date+Time) Stamp

Hi! Please see our current script: #!/usr/bin/ksh if (egrep "This string is found in the log" /a01/bpm.log) then mailx -s "Error from log" me@email.com, him@email.com </a01/bpm.log fi To the above existing script, we need to add the following change: 1) After finding the string,... (7 Replies)
Discussion started by: atechcorp
7 Replies

6. Shell Programming and Scripting

command to list files with path & date in a folder

Hi, I need command to display files with full path and date of files where are generated at every 5hrs in a folder. eg: /u01/app/test/orjthsd_1_1 Sun May 10 19:03:26 2009 /u01/app/test/weoiusd_1_1 Sun May 10 21:00:26 2009 thanks saha (3 Replies)
Discussion started by: saha
3 Replies

7. UNIX for Dummies Questions & Answers

How to Zip the files from date Stamp to end date Stamp

Hi, I need to zip the list of files using from date Stamp to end date Stamp, How can I filter and make FromDate_EndDate.gzip? any idea? (1 Reply)
Discussion started by: redlotus72
1 Replies

8. UNIX for Dummies Questions & Answers

Date/Time Stamp

Hi All, Wondering if there is have a date added at the end of a test string. I have a hypothetical text file day one: John Paul George When the file day one is output, I'd like it to read something like this: John 101406 Paul 101406 George 101406 Day two, when the same text file... (0 Replies)
Discussion started by: JimmyFlip
0 Replies

9. UNIX for Dummies Questions & Answers

Inserting Date&Time Stamp In Existing Log File

I am trying to insert a line with a date stamp in a file that is used to monitor activity in one of our directories. By doing this, I want to grep that file each day and go to the last entry for each time a error occurred and pull all errors generated if any exist. If error exists I want that error... (3 Replies)
Discussion started by: shephardfamily
3 Replies

10. Shell Programming and Scripting

Date Time Stamp

I'm trying to write a script that checks the DTS of a file the compares it to the current time. If greater that 60 mins has gone by and the file has not been written to alert. So far I have the time pulled from the file but I dont know how to compare the times against a 60 min difference. ... (2 Replies)
Discussion started by: jarich
2 Replies
Login or Register to Ask a Question