How do you combine these ls commands so that I can have the outputs by name, time stamp, and size?
ls -al |grep name_of_file
ls -al | sort +4nr
ls -l -t
Please advise.
How do you combine these ls commands so that I can have the outputs by name, time stamp, and size?
ls -al |grep name_of_file
ls -al | sort +4nr
ls -l -t
Please advise.
Surprising post for someone with your experience.
It always helps to post what Operating System and version you are running and what Shell you prefer:
I'll assume some sort of mainstream unix and a mainstream Shell. Hope this is what you mean:
(
echo "Directory listing by name (alphabetic)"
ls -la
echo "Directory listing by timestamp (ascending)"
ls -latr
echo "Directory listing by size (descending)"
ls -al | sort -n -r +4
) | more
In each of these commands you can insert a grep -v total
to get rid of the total
line from ls
.
Sorry, I am on AIX 6.1 with KSH.
I should have clarified better...
I was wondering if we can combine all of the three ls commands together so that the output shows, i.e.
aaa* listed by the timestamp (desc order) and then size.
then move onto the next
bbb* listed by the timestamp (desc order) and then size.
then move onto the next
ccc* ....
Wildcard search would be helpful as to the file names might have different ends, i.e.
dm_rmc_rdds_mail100506.log, dm_rmc_rdds_mail100527.log, cpmqlen_app2_apr13.csv, cpmqlen_app2_apr23.csv, etc.
Please advise.
In order to sort by time you have to use mtime in seconds. That means perl or C.
Here is a simple perl subroutine to get mtime plus some shell
#!/bin/ksh
tags()
{
perl -e '
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks)
= stat("$ARGV[0]");
print "$ARGV[0] $mtime $size", "\n"; ' $1
}
ls | grep -v total |
while read fname
do
tags $fname
done | sort -k1 -k2n -k3n |
while read f dummy
do
ls -l $f
done
This gives a full listing ordered by name, mtime and size.
This becomes a bit tricky. many files i.e. have different suffixes, which makes the sorting a bit difficult to read..
Is there any way to ignore the suffix and sort them?
-rwxrwxrwx 1 d_gold d_gold 308 May 14 2011 vte_med_hx_15057889.rtf
-rwxrwxrwx 1 d_gold d_gold 308 May 10 2011 vte_med_hx_15058605.rtf
-rwxrwxrwx 1 d_gold d_gold 308 May 12 2011 vte_med_hx_15058885.rtf
-rwxrwxrwx 1 d_gold d_gold 365 May 11 2011 vte_med_hx_15059132.rtf
-rwxrwxrwx 1 d_gold d_gold 308 May 10 2011 vte_med_hx_15059305.rtf
-rwxrwxrwx 1 d_gold d_gold 308 May 25 2011 vte_med_hx_15059371.rtf
-rwxrwxrwx 1 d_gold d_gold 308 Jun 11 2011 vte_med_hx_15059865.rtf
-rwxrwxrwx 1 d_gold d_gold 308 Jun 11 2011 vte_med_hx_15059901.rtf
-rwxrwxrwx 1 d_gold d_gold 308 Jun 11 2011 vte_med_hx_15059938.rtf
-rwxrwxrwx 1 d_gold d_gold 308 Jun 11 2011 vte_med_hx_15059987.rtf
-rwxrwxrwx 1 d_gold d_gold 308 Jun 11 2011 vte_med_hx_15060069.rtf
-rwxrwxrwx 1 d_gold d_gold 365 Jun 13 2011 vte_med_hx_15060094.rtf
-rwxrwxrwx 1 d_gold d_gold 308 Jun 12 2011 vte_med_hx_15060167.rtf
Please advise.