Rename File Based on Created Date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rename File Based on Created Date
# 1  
Old 11-21-2009
Rename File Based on Created Date

I am trying to rename files based on the created/born date of the file. I Have a total of 4000 files that i am trying to do this with and would like it to be log_yyyymmddhh.gz right now the files are maillog.???.gz.

Can anyone point me in the right direction of how to get this done via scipt?

I know how to do it based on the current time however what i need is the created/born time. I have tried to use stat however having a problem formating the date.

Thanks
Paul
# 2  
Old 11-21-2009
So why don't you show us what you did with stat and what your formatting issue is?
# 3  
Old 11-21-2009
Is this what you are looking for?

Code:
for each in `find . -name "*" -type f`
do 
dt=`ls -l $each | awk '{ print $6" "$7" "$8 }'`; 
cnv_dt=`date -d "$dt" +%Y%m%d%H`; 
mv $each maillog_${cnv_dt}.gz; 
done

# 4  
Old 11-21-2009
Thanks for your replys

that does not seam to work for me, I am using OpenBSD So i think in my case i would use date -j for the date. When try that it sais invalid time format

stat gives me below and the date i am looking for is "Nov 17 10:00:06 2009". If i load that into date -h it does not work i am sure because it has "Nov" in it.

4868 21129858 -rw------- 1 root wheel 84642584 186447 "Nov 21 07:16:04 2009" "Nov 17 10:00:06 2009" "Nov 21 14:00:05 2009" 16384 368 0 /var/log/maillog.100.gz

If i run "stat -f "%N %m" /var/log/maillog.100.gz" it will give me "/var/log/maillog.100.gz 1258470006" however not sure how to break apart the "1258470006" into month and day.
# 5  
Old 11-21-2009
Quote:
Originally Posted by Paulb
I am trying to rename files based on the created/born date of the file.
The short answer is you can't reliably.
The large majority of file systems do not store file creation dates so there is no way to know when a file was created unless that information is included in the file itself (ex: exif photos). The few file systems I know actually storing the file creation time are BSD/UFS2/st_birthtime, Solaris/ZFS/crtime, Mac OS/HSF+/btime and Linux/Ext4/crtime.
With the other ones, the only timestamps recorded in a file metadata are the last modification and the last access time.
However, if the files you are processing are never modified after their initial creation, the last modification should be pretty close to the creation time (depending on the time spent to write the file), which is what the scripts suggested are doing.

Last edited by jlliagre; 11-21-2009 at 09:34 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Can you do remove core file based on what created them :(

Hi, Currently, we have a Perl script from a third-party vendor that is generating core dumps. It has been reported. We can't turn off the script as it does generate some diagnostic file that's required. So at the moment, we have to let it continue to do its run. I wish I can say the vendor is... (8 Replies)
Discussion started by: newbie_01
8 Replies

2. UNIX for Dummies Questions & Answers

Find the count of files by last created date based on the given date range

My unix version is IBM AIX Version 6.1 I tried google my requirement and found the below answer, find . -newermt “2012-06-15 08:13" ! -newermt “2012-06-15 18:20" But newer command is not working in AIX version 6.1 unix I have given my requirement below: Input: atr files: ... (1 Reply)
Discussion started by: yuvaa27
1 Replies

3. UNIX for Dummies Questions & Answers

How to get year part from file created date?

Hi Gurus, I need to get year part of file created date. when using ls -l , it only show month, day and time. is there any option I can add to get year portition? Thanks in advance (7 Replies)
Discussion started by: ken6503
7 Replies

4. Shell Programming and Scripting

Copy files based on last created date

Hi, I have a requirement to copy files from a windows network drive to a Linux server using shell script based on the last created date. Ex: FileName CreatedDate/Time F1 05-01-2012 3:00 PM F2 05-01-2012 3:15 PM F3 05-01-2012 2:00 PM When i run the shell script... (1 Reply)
Discussion started by: Lee_10
1 Replies

5. UNIX for Dummies Questions & Answers

Can we change the file created date?

Hi, I am creating a file in unix today. Is it possible to make the file created as 2 days older (or some past date)? P.S: i dont want to change the system date to older one and try.:rolleyes: Thanks, Pandeeswaran (6 Replies)
Discussion started by: pandeesh
6 Replies

6. Shell Programming and Scripting

mp3 tag/rename based on creation (last modified date)

Arg, I'm trying to figure out how to create a album tag based on the last modified date stamp for files which don't have a corresponding .talk file. IE. 2009 12 10 - Talk Radio.mp3 is how I want them structured, they should all have a corresponding .talk file so my mp3 player can speak the name ie... (0 Replies)
Discussion started by: mrplow
0 Replies

7. Shell Programming and Scripting

How to keep appending a newly created file based on some keywords

Hi Friends, I have to create a new log file everyday and append it with content based on some keywords found in another log file. Here is what I have tried so far... grep Error /parentfolder/someLogFile.log >> /parentfolder /Archive/"testlogfile_error_`date '+%d%m%y'`.txt" grep error... (6 Replies)
Discussion started by: supreet
6 Replies

8. UNIX for Dummies Questions & Answers

Finding the date a file was created

how do i find the date a file was created? (3 Replies)
Discussion started by: trob
3 Replies

9. Shell Programming and Scripting

Need to find created date of file in UNIX

I need to write a script which has to list all the files which are created before six months from now. kindly help on this ... (7 Replies)
Discussion started by: amirthraj_12
7 Replies

10. UNIX for Dummies Questions & Answers

File management based on date created

Hi There, I was wondering how to manage files (ie. rm, cp , mv) based on date last modified and date created. ie. Say i want to: mv ./* ./temp/* (where the date created < 29/1/2006 ) or mv ./* ./temp/* (where the date modified > 27/1/2006 && date modified < 29/1/2006) Thanks in... (1 Reply)
Discussion started by: Geehog_Rare
1 Replies
Login or Register to Ask a Question