Year of file creation


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Year of file creation
# 1  
Old 11-09-2004
Year of file creation

How do I know the year of creation of a file modified less than 6 months from now. Since I "know" that the file has been created
in 2004 since ls -l does not show the year of its creation but can I get the same information through a command etc.
From the ls man page:
Quote:
-l Lists in long format, giving mode, ACL indication,
number of links, owner, size in bytes, and time of
last modification for each file. If the file is a spe-
cial file the size field will instead contain the
major and minor device numbers. If the time of last
modification is greater than six months ago, it is
shown in the format `month date year'; files modified
within six months show `month date time'. If the file
is a symbolic link, the pathname of the linked-to file
is printed preceded by `->'.

Am I clear enough??

Chill
enc.
# 2  
Old 11-09-2004
If the file is created in the current year ( i.e 2004 ) it is not showing the year by ' ls -l ' listing

But if it passes 2004 year ... it will show the year of creation or modification time ;

See the following o/p for one of my directory :

drwxr-xr-x 2 bhargav staff 512 Dec 27 2002 CORBA
-rwxr-xr-x 1 bhargav staff 3353 Jan 15 2003 set_env
-rwxrwxrwx 1 bhargav staff 7222 Jan 21 2003 variables.mk
-rw-r--r-- 1 bhargav staff 740 Nov 09 10:41 x



If it is not showing the year in 'ls -l ' listing ... then it is abvious that the file is modified last time in the same year ;
# 3  
Old 11-09-2004
Code:
filetime.pl t.lis | awk 'BEGIN{FS=":"} {print $1}'

Gives you the year of the last time a file was modified
Here is filetime.pl:
Code:
#!/usr/contrib/bin/perl
#^###################################################################
#^ $Header: filetime.pl,v 1.1 97/01/20 10:17:00 http srvr? $
#^###################################################################
#^ PROGRAM DESCRIPTION
#^ -------------------
#^ This program prints the modification dates of files
#^ It uses the following format: filetime.pl filename
#^ It will accept:  filetime.pl filename1 filename2 filename3
#^                  filetime.pl /tmp/file*
#^ The format of the output is: YYYY:MM:DD:hh:mm:ss filename
#^ example:
#^          
#^           1996:11:15:10:54:25 filename
#^           
############################################
# loop
############################################
while ($curfile = $ARGV[0])
{
   #################################################
   # Do following code block only if $curfile exists
   #################################################
   if (-e $curfile)
   {
      ###############################
      # stat structure into variables
      ###############################
      ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
      $atime,$mtime,$ctime,$blksize,$blocks)
      = stat("$curfile");
      ###############################
      # time structure into variables
      ###############################
      local($sec,$min,$hr,$day,$mon,$yr,$wday,@dntcare) = localtime($mtime);
      $yr = ($yr>=70) ? $yr+1900 : $yr+2000;
      $yr="$yr";
      $mon = (++$mon < 10) ? "0$mon" : "$mon";
      $day = ($day < 10) ? "0$day" : "$day";
      $hr  = ($hr < 10) ? "0$hr" : "$hr";
      $min = ($min < 10) ? "0$min" : "$min";
      $sec = ($sec < 10) ? "0$sec" : "$sec";
      ##################################################################
      # Rearrange in the YYYYMMDDhhmmss format and assign to $dte variable
      ##################################################################
      $dte = join(':',$yr,$mon,$day,$hr,$min,$sec);
      ######################################
      # Print modification date and filename
      ######################################
      print ("$dte $curfile\n");
      }
   ########################################
   # Shift to next position in command line
   ########################################
   shift (@ARGV);
}

# 4  
Old 11-09-2004
Quote:
If it is not showing the year in 'ls -l ' listing ... then it is abvious that the file is modified last time in the same year ;
That's true but what if I want the year of file creation in a variable
which I can use dynamically. I could have done
VARIABLE=`ls -l|awk '{print $6$7$8,2004}'`
for getting the dd:mm:yyyy format for the file but here the
value for 2004 is static and would be useless once the year changes.

For Jim's code I still have not tested it but looks promising to me.

Chill
enc
# 5  
Old 04-22-2005
Problem faced with files greater than 2 GB

Guys,
I used the following snippet as suggest above.

#!/usr/bin/perl -w
use strict;

foreach (@ARGV) {
my $mtime=(stat($_))[9];
my ($ss,$mm,$hh,$DD,$MM,$YY)=localtime($mtime);
printf "%04d %s\n",$YY+1900,$_;
}


Problem is when the file name passed as argument is greater then 2 Gb.

Any suggestions or a roundabout will be of great help. As most of my files I want are of huge size.

My OS is Sun Solaris 5.8.

thanks
# 6  
Old 04-22-2005
Try this script:
Code:
#! /usr/bin/ksh

typeset -Z2 d m
Jan=1 Feb=2 Mar=3 Apr=4 May=5 Jun=6 Jul=7 Aug=8 Sep=9 Oct=10 Nov=11 Dec=12
date "+%Y %m" | read year month

for i ; do
        line=$(ls -dl $i)
        line=${line##+([! ])+([ ])}
        line=${line##+([! ])+([ ])}
        line=${line##+([! ])+([ ])}
        line=${line##+([! ])+([ ])}
        line=${line##+([! ])+([ ])}
        set -A stamp $line
        d=${stamp[1]}
        eval m=\$${stamp[0]}
        y=${stamp[2]}
        ((${#y} != 4)) && ((y=year-(m>month)))
        echo $y $m $d $i
done
exit 0

# 7  
Old 04-23-2005
MySQL Thanks

Thanks!

It really works... trying to decipher the code Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Display lines for a particular year in a file using grep

hi, I have a log file with data for more than 3 years, i want only the rows for the year 2017, say for example. My file has the data like this 08-OCT-2015 11:17:35 AAA, BBBB 08-OCT-2017 11:17:35 AAA,Bdfdfd,dfdfd,dfd 08-Nov-2017 11:17:35 AAA,Bdfdfd,dfdfd,deree i want the rows... (2 Replies)
Discussion started by: skoshekay
2 Replies

2. Shell Programming and Scripting

Shell script rm last 1 year file

HI I am using below script to remove only last year files and also need deleted files i want to keep in log file or history for future reference But it is giving wrong output please help or let me any other way for example 2016 if am using this command it is removing 2016 and 2017 files also... (2 Replies)
Discussion started by: Kalia
2 Replies

3. UNIX for Dummies Questions & Answers

Sort file by day of year

Hello, It's a shame to not be able to do what I need, but I am sure you will : Here is what I have in my log file : New File: 95106 Jun 6 48 TAG__KSO__2012092_0.TAB New File: 95106 Mar 26 48 TAG__KSM__2012020_0.TAB New File: 95106 Mar 26 48 TAG__KSO__2012020_0.TAB New File: 95106 May... (3 Replies)
Discussion started by: Aswex
3 Replies

4. Shell Programming and Scripting

want to get last year and month from the file

Hi I have files like abc_cd_20110302_123423 abc_cd_ef_20110301_123423 abc_cd_ef_20110403_123423 abc_ef_20110401_123423 I want to extract the the year and month associated with each file. I tried logfileyearmonth=`echo $logfile | awk -F_'{print $NF}'` Any other way can I... (6 Replies)
Discussion started by: dgmm
6 Replies

5. AIX

not showing the year of file

Hi I am facing strange issue in one direcotry it is not showing year of file, can you please suggest me wheather there is any limitation on year, on some other file though it is showing the dates. Regards, Manoj. (1 Reply)
Discussion started by: manoj.solaris
1 Replies

6. Shell Programming and Scripting

File created year

Hi I need to get the File creation date (MM,DD,YYYY) using ls -ltr am getting only Month and Day only, I need year also when the file is modified. Thanks (3 Replies)
Discussion started by: KiranKumarKarre
3 Replies

7. Shell Programming and Scripting

move file by year/mouth

Hello experts, I have a directory which the files inside have different date. Now I'd like to move them to folder year/mouth (2007/01 , 2007/02) Have any suggestion? thanks in advance for reading or anyposts. (3 Replies)
Discussion started by: mmm951
3 Replies

8. Shell Programming and Scripting

Move files based on year of creation

Hi All, I have a directory which has crores of files since from 2003 till now. I want to move only the 2003 files to another directory. Please help. Thanks (2 Replies)
Discussion started by: IHK
2 Replies

9. Shell Programming and Scripting

How do i get the year of the file

When i use, 'ls -ltr' I only see the month and day (timestamp) of the file. How do i see the year also. Thanks and Regards, Ram (1 Reply)
Discussion started by: ramky79
1 Replies

10. UNIX for Dummies Questions & Answers

Year of file creation

Hello guys, I want to extract year of file creation. Is there a direct way to get that information. "ls -l" do give required information but if the file created is not older then 6 months it will not display the year. To get year one possible way will be do a math with date extracted by ls. ... (2 Replies)
Discussion started by: RishiPahuja
2 Replies
Login or Register to Ask a Question