I would like to list or sort by date and time (the files are named in day and time format) where the latest file will be placed at the bottom and the earliest file be placed at the top. Can anybody help me?
My files are named in the following manner.
EG: abc_071128_144121_data
" abc " is random , " 071128 " depicts the date in yymmdd format, " 144121 " depicts the time in the hhmmss format
#!/bin/sh
for file in *
do
echo $file | cut -d '_' -f 2,3,4 >> /tmp/sort.$$
done
for file in `sort -n < /tmp/sort.$$`
do
ls -1 *_${file}
done
rm -f /tmp/sort.$$
Just to throw this out there...if the files are created in a specific time order irrespective of name, then the ls -t command would work nicely. With Solaris, the last modification time is used, but the -u option would use the file's access time.
But now i have another problem, some of the filenames come in the form of " zzz_071128_144121_data_interim ", how can i sort it out by modifying your code? Example below
I am using solaris by the way.
This code's not working for me.
Error message is telling me to input an argument for " k ". Do you have any idea on that ?
$ ls -l
total 0
-rw-r--r-- 1 raynon real 4 Jan 2 17:06 abc_071128_144121_data
-rw-r--r-- 1 raynon real 4 Jan 2 17:06 bbb_071128_113329_data
-rw-r--r-- 1 raynon real 6 Jan 2 17:06 zzz_071128_163017_data_interim
$ ls -1 |sort -t'_' -k2 -k3
sort: option requires an argument -- k
sort [-bcdfiMmnru] [-o output] [-T directory] [-ykmem] [-t char]
[+pos1 [-pos2]] [-k field_start[type][,field_end[type]] [file...]
$ ls -1 |sort -t"_" -k2 -k3
sort: option requires an argument -- k
sort [-bcdfiMmnru] [-o output] [-T directory] [-ykmem] [-t char]
[+pos1 [-pos2]] [-k field_start[type][,field_end[type]] [file...]
$ ls -l
total 0
-rw-r--r-- 1 raynon real 4 Jan 2 17:06 abc_071128_144121_data
-rw-r--r-- 1 raynon real 4 Jan 2 17:06 bbb_071128_113329_data
-rw-r--r-- 1 raynon real 6 Jan 2 17:06 zzz_071128_163017_data_interim
$ ls -1 |sort -t'_' -k2,2 -k3,3
sort: option requires an argument -- k
sort [-bcdfiMmnru] [-o output] [-T directory] [-ykmem] [-t char]
[+pos1 [-pos2]] [-k field_start[type][,field_end[type]] [file...]
$ ls -1 |sort -t"_" -k2,2 -k3,3
sort: option requires an argument -- k
sort [-bcdfiMmnru] [-o output] [-T directory] [-ykmem] [-t char]
[+pos1 [-pos2]] [-k field_start[type][,field_end[type]] [file...]
Please post the results of "which sort" and, Raynon, please also post the results of "uname -a" as rikxik did. This might help you guys get on the same page.