Operating System - HP-UX
1820301 Members
2820 Online
109622 Solutions
New Discussion юеВ

Re: convert a file date to MM/DD/YYYY

 
SOLVED
Go to solution
B. Chapman
Frequent Advisor

convert a file date to MM/DD/YYYY

Greetings all!

Does anyone know of a simple way to convert a FILE DATE from the typical "ls -l" format to MM/DD/YYYY format?

Meaning, instead of something like:

# ls -l
total 0
-rw-r--r-- 1 bpc users 0 Jul 7 14:24 foo1.txt
-rw-r--r-- 1 bpc users 0 Jun 6 13:21 foo2.txt
#

I'd be looking for (time is not significant to me - only date):

# MY_ls.sh
07072004 foo1.txt
06062004 foo2.txt
#

Is there an easy way to script "MY_ls.sh"?

As always, TIA!

Later,
Ben.
chapmanb@saic.com
6 REPLIES 6
Fred Ruffet
Honored Contributor
Solution

Re: convert a file date to MM/DD/YYYY

in perl :

$cat myls
#!/opt/perl/bin/perl

$ArgFile=$ARGV[0];
open(CMD,"ls $ArgFile |");
while () {
chomp;
$FileName=$_;
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$AccessTime,$ModifTime,$ChangeTime,$blksize,$blocks)=stat($FileName);
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($ModifTime);
$year+=1900;
if ($mday < 10) { $mday="0$mday"; }
if ($mon < 10) { $mon="0$mon"; }
print "$mon$mday$year $FileName\n";
}
close(CMD);

$myls -a .
06072004 myls
04132004 touch_amc
06072004 .
06072004 ..

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
B. Chapman
Frequent Advisor

Re: convert a file date to MM/DD/YYYY

Fred,

PERFECT! If I could assign 100 points, I would...

Thanks again, Fred!

Later,
Ben.
chapmanb@saic.com
A. Clay Stephenson
Acclaimed Contributor

Re: convert a file date to MM/DD/YYYY

Probably the best way to do this is to leverage the Perl stat() and localtime() functions. I had a Perl script that was already extremely close.

Use it like this this:
ftime.pl [-a|-c|-m] file1 [file2 ...]

By default, the modification time is used but you can change to access, -a or change,-c times if you wish.

If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: convert a file date to MM/DD/YYYY

There is a small bug in Fred's script. Localtime() returns month values in the range 0-11 just like its C counterpart. You need to add 1 to the value before outputting.
If it ain't broke, I can fix that.
B. Chapman
Frequent Advisor

Re: convert a file date to MM/DD/YYYY

Clay,

Kudos for spotting that bug - it slipped right past me... Again - thanks for your post. I'm actually going to use a "hybrid" from your code and Fred's code. You guys make my job a lot easier!!!

Have a great weekend.

Later,
Ben.
Fred Ruffet
Honored Contributor

Re: convert a file date to MM/DD/YYYY

You are right Clay ! Didn't noticed that... I find it a little bit curious, but that's the way it works.

Hope it didn't do any harm,

Fred
--

"Reality is just a point of view." (P. K. D.)