Operating System - Linux
1752509 Members
4839 Online
108788 Solutions
New Discussion юеВ

Re: How to get a file's year of last modification by SHELL?

 
MA Qiang
Regular Advisor

How to get a file's year of last modification by SHELL?

I can only get month and day by 'ls -l'.

Thanks.
10 REPLIES 10
MarkSyder
Honored Contributor

Re: How to get a file's year of last modification by SHELL?

You only get month and day if the file is less than six months old. Any older and it gives you the year.

Mark Syder (like the drink but spelt different)
The triumph of evil requires only that good men do nothing
Arunvijai_4
Honored Contributor

Re: How to get a file's year of last modification by SHELL?

http://linuxcommand.org/lts0030.php

Modification Time
The last time the file was modified. If the last modification occurred more than six months in the past, the date and year are displayed. Otherwise, the time of day is shown.

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Muthukumar_5
Honored Contributor

Re: How to get a file's year of last modification by SHELL?

If the file is getting old more than 6 months then it will lost it's time details. man ls says that.

To work around this,

1) find -type f -exec ls -l {} \+ 1>/tmp/file_$(date +'%d_%m_%y)

It will update the file with time details to that you can old file informations from this. :) Just select required directory with part in find command.

hth.
Easy to suggest when don't know about the problem!
Stephen Keane
Honored Contributor

Re: How to get a file's year of last modification by SHELL?

Or you could compile the attached code (using the bundled cc compiler)

cc getmtime.c -ogetmtime

Then run it as

getmtime file

I'm sure you can change it to suit your needs.
VEL_1
Valued Contributor

Re: How to get a file's year of last modification by SHELL?

Hi,

You can use perl script also:

#!/usr/local/bin/perl
use File::stat;

$file = "test.txt";

$file_stat = stat($file) or die "No $file: $!";
$original_time = $file_stat -> mtime;

$file_stat = stat($file);
$new_time = scalar localtime $file_stat -> mtime;
print "Time: " . $new_time . "\n";


Thanks.
Muthukumar_5
Honored Contributor

Re: How to get a file's year of last modification by SHELL?

durai,

It is great. Working like a charm. Tested with

# touch -t 8907140000 file
# ls -l file
-rw-r--r-- 1 root root 0 Jul 14 1989 file
# perl tst.pl
Time: Fri Jul 14 00:00:00 1989
# touch -t 8907140030 file
# perl tst.pl
Time: Fri Jul 14 00:30:00 1989

Keep sharing.

P.S: Assing 0 points.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: How to get a file's year of last modification by SHELL?

Qiang ( Am I right),

To use durai's solution, you have to have File/stat.pm (perl module). Check stat.pm in your system. If you are having then good else download File/stat.pm and use it.

hth.
Easy to suggest when don't know about the problem!
MA Qiang
Regular Advisor

Re: How to get a file's year of last modification by SHELL?

Thanks for all your responses. But I would like a simple shell script to get it.
Muthukumar_5
Honored Contributor

Re: How to get a file's year of last modification by SHELL?

By default shell ulities, we may not achive this. You have to write your own utility and put is under /usr/bin/ locaiton as like /usr/bin/ls or /sbin/ls binaries.

Use perl script with recrusive find.pm to achive this.

hth.
Easy to suggest when don't know about the problem!