Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

year in ls command 2

Status
Not open for further replies.

cram11

MIS
Apr 23, 2004
5
FR
Hi,
In the man of ls, we can see :
"If the time of last modification is greater than six months ago, the time field is shown in the format month date year where as files modified within six months the time field is shown as month date time format."

My problem is that I would like to list all the files of a directory (ls -lrt) and to print the name, the size and the complete date (DD/MM/YYYY).

Could you have a look for my problem ?
Thanks in advance.
Nicolas
 
Do you want to do this in the command line or will a script work?

 
You'll need a script.

I'd use perl. The stat() and localtime() functions, along with File::Find, should be enough to get all the information you want.

Trying to manipulate the "ls -ltr" ouput into the form you want would unnecessarily complicate things.


Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L

 
Thanks for your responses
I'd prefer a command line (i don't know Perl) or (if it's not possible via a command line) a script.

Nicolas
 
Thank you iribach but "find . -ls" doesn't return the year for the most recent files (< 6months).
 
You'll have to use a script. I don't know perl either, I wish I had time to learn. I'd use korn.

You could do the standard ls -ltr and assume if the year isn't listed it's the current year. Again.... gonna need a script.
 
In ls output, for the first half of the year, there will be files from the previous year without a listed year. Plus you'd need to convert the short month names to numeric. That's why I said manipulating ls output would be overly complicated.

Even though you don't know perl, I'll post the code, as it would be entered on the command line, just in case you want to play with it.

Code:
ls -tr| perl -ne '
chomp;
$filename = $_;
if ( -f $filename )
        {
        ($size, $mod_time) = (stat $filename)[7,9];
        ($day, $month, $year) = (localtime $mod_time)[3,4,5];
        printf("%-30s\t%-15d\t%0.2d/%0.2d/%d\n",
                $filename,
                $size,
                $day,
                $month+1,
                $year+1900);
        }
'

Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top