1837248 Members
5202 Online
110115 Solutions
New Discussion

sort or awk?

 
SOLVED
Go to solution
Nik_1
Advisor

sort or awk?

Hi,

Calling all awk gurus! I am trying to sort a group of files by date order... using the following:

ls -la |sort -r -k 6

This sort the list by field 6 (the month) but it sort it alphabetically... is there anyway to sort this using the calender format? so as this is reverse, I'm hoping for:

Dec
Dec
Mar
Jan
etc... etc...

thanks.
Nik
17 REPLIES 17
Paula J Frazer-Campbell
Honored Contributor
Solution

Re: sort or awk?

Hi

Will ls -lrt not give you what you want?

Paula
If you can spell SysAdmin then you is one - anon
Carlos Fernandez Riera
Honored Contributor

Re: sort or awk?

NONE.

Use -t flag of ls


ls -t ... or ls -rt ( for reverse order)
unsupported
Deepak Extross
Honored Contributor

Re: sort or awk?

Neither :-)
"ls -lt" or "ls -lrt", depending on whether you want ascending or descending order.
Nik_1
Advisor

Re: sort or awk?

Complete moment of madness there! sorry!
of course, you're all correct... have 10 points each for taking the time to answer my question! :)
Nik_1
Advisor

Re: sort or awk?

Hang on a second... it could be more complicated than I thought...
What I'm trying to do is a find command to find the 10 most recent documents within a directory (and all subs). So I'm using the find command with -mtime and then sending all output that it finds, to a temporary file.. which I would then like to sort by month (field 6) not alphabetically but by calender... any ideas again!? potential for even more points now! :)
Justo Exposito
Esteemed Contributor

Re: sort or awk?

Hi Nik,

Try this:
find . -type d -exec ls -lrt {} \;

Regards,

Justo.
Help is a Beatiful word
Justo Exposito
Esteemed Contributor

Re: sort or awk?

Hi Again,

Or try this if you want only the first 10 occurrence:
for i in `find . -type d -print
do
ls -lrt $i | head -10
done

Regards,

Justo.
Help is a Beatiful word
H.Merijn Brand (procura
Honored Contributor

Re: sort or awk?

If the temp file can contain all files, and it is not /huge/ (iow, it would also fit in memory), it is easy to write a perl script

#!/usr/bin/perl -w

use strict;
use File::Find;

my @dir = (".");
my @files;

find (sub {
-M $_ < 0.9 and return; # Too new
-M _ > 6.4 and return; # Too old
push @files, $File::Find::name;
}, @dir);

my @sorted_by_date = sort { -M $a <=> -M $b } @files;

print "Newest 5: @sorted_by_date[0..4]\n";
print "Oldest 5: @sorted_by_date[-5..-1]\n";
Enjoy, Have FUN! H.Merijn
Nik_1
Advisor

Re: sort or awk?

thanks guys... but those sort each directory... what I am trying to achieve is an entire list of files within different directories that are given as just a list of files in date order (trying to avoid individual ls -ltr of each directory.)
steven Burgess_2
Honored Contributor

Re: sort or awk?

Hi

find . -type d -xdev | xargs ll -ltr > /tmp/file1

Steve
take your time and think things through
harry d brown jr
Honored Contributor

Re: sort or awk?

Nik,

Are you trying to say you want the MONTH's sorted in reverse, like this:

SEP
OCT
NOV
MAY
MAR
JUN
JUL
JAN
FEB
DEC
AUG
APR

which is what an APLHA sort will do??????????

Which I don't think you want. What you really need to do is look at procura's perl example and modify it for what you want.

live free or die
harry
Live Free or Die
Nik_1
Advisor

Re: sort or awk?

Hi Harry,
No I'm trying to work out how to get them sorted like...

Dec
Nov
Oct
Sep
Aug
H.Merijn Brand (procura
Honored Contributor

Re: sort or awk?

in my perl script, '@dir' can be any list of sub-folders, like

@dir = ("/tmp", "/etc", "/usr/sbin");

or

@dir = qw( /tmp/blah /tmp/foo/ahhrg /stand /usr/etc );
Enjoy, Have FUN! H.Merijn
harry d brown jr
Honored Contributor

Re: sort or awk?

Nik:

#!/usr/local/bin/perl
#
# written by harry d brown jr
#
# get the "option"
#
$opts = $ARGV[0];
#
if ( ( $#ARGV < 0 ) || ( $opts !~ /-atime|-ctime|-mtime/ ) ) {
print "Usage : ./o [-atime|-ctime|-mtime] [dirpath ...]\n";
exit 1;
}
#
# get directories to list
#
$DIRS2list = "";
for ( $i=1 ; $i le $#ARGV ; $i++ ) {
$DIRS2list .= $ARGV[$i] . " ";
}
#
# find the files, and not we are only using real files, to use all
# remove the "-type f"
#
open(FILELIST,"find $DIRS2list -type f|");
#
$filemat="";
#
# a little trickery to get the optins and LS commands
#
$LSOPTS = "u c";
$OPTSTR = "amc";
$DATEIDX = index($OPTSTR,substr($opts,1,1));
$DATECHAR = substr($LSOPTS,$DATEIDX,1);
#
# read the file list from find
#
while () {
chop;
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$
blocks) = stat $_;
$DATEFIELD[0] = $atime;
$DATEFIELD[1] = $mtime;
$DATEFIELD[2] = $ctime;
$date2use = $DATEFIELD[$DATEIDX];
push @filemat,$date2use . " " . $_;
}
#
# sort the array on the time the user choose
#
#
@newmat = sort { $b <=> $a } @filemat;
#
# spit the listing out
#
foreach $filestuff ( @newmat ) {
($filetime,$filename) = split(/ /,$filestuff);
print `ls -l$DATECHAR $filename`;
}
# end of script


You might have to change the PATH to perl, and the options are this:


./prog -atime /tmp /var/tmp /whatever

-atime = last access time
-mtime = last file change
-ctime = last INODE change

use a "." (period) for the current directory.


procura, what do ya think?

live free or die
harry
Live Free or Die
SHABU KHAN
Trusted Contributor

Re: sort or awk?

Nik,

If you still want the find command to do what you are looking for, then here it is:

ls -lt `find . -mtime +10 -type f -print`

This will find all files older than 10 days from current date and will sort in the order that you need ...

Thanks,
Shabu
SHABU KHAN
Trusted Contributor

Re: sort or awk?

Nik,

I forgot to mention one more thing ... there is a catch ... if this doesn't find any files older than 10 days then it would list everything in the current directory or from wherever you are executing it ...

-Shabu
James R. Ferguson
Acclaimed Contributor

Re: sort or awk?

Hi Nik:

Hi Nik:

'sort' will serve quite well for your purpose. You simply need to describe the sort key with the '-M' option to cause the order of the month names to be compared such that JAN < FEB < ... < DEC.

Have a look at the man pages for 'sort'.

Regards!

...JRF...