Sort by name, time, and size


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort by name, time, and size
# 1  
Old 04-30-2012
Sort by name, time, and size

How do you combine these ls commands so that I can have the outputs by name, time stamp, and size?

Code:
ls -al |grep name_of_file
ls -al | sort +4nr
ls  -l -t

Please advise.

Last edited by methyl; 04-30-2012 at 04:02 PM.. Reason: Please use code tags.
# 2  
Old 04-30-2012
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:

Code:
(
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.

Last edited by methyl; 04-30-2012 at 04:21 PM..
# 3  
Old 05-01-2012
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.
PHP Code:
aaalisted 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.
PHP Code:
dm_rmc_rdds_mail100506.logdm_rmc_rdds_mail100527.logcpmqlen_app2_apr13.csvcpmqlen_app2_apr23.csvetc
Please advise.
# 4  
Old 05-01-2012
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
Code:
#!/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.
# 5  
Old 05-04-2012
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?

PHP Code:
-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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sort by file/directory size

OS : RHEL 6.6 I want to list the files/directories sorted (Ascending or Desceding) by their size. As you can see in the below example, du command doesn't sort by size. In Linux world, is there any other command or workaround using du command to list the files/directories sorted by their... (6 Replies)
Discussion started by: John K
6 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Size (Sort files using ...)

-rw-r--r-- 1 oracle oinstall 4 Jan 17 16:23 a -rw-r--r-- 1 oracle oinstall 212 Jan 17 17:51 amar -rw-r--r-- 1 oracle oinstall 32 Jan 17 17:30 b -rw-r--r-- 1 oracle oinstall 246 Jan 17 15:40 h1.tar.gz Hi, I want combination of linux command to sort out the line which has 32k size. ... (5 Replies)
Discussion started by: karthick nath
5 Replies

3. Shell Programming and Scripting

Script to sort by size of directories

Hello, I'm trying to find top 5(highest) directories by size. I did something like du -sh * > $file Where I can get all the size with respect to directories, but I need only top 5 directory from the file. How can I sort by size in the file and print top 5 sizes with the directory name??? ... (3 Replies)
Discussion started by: pjeedu2247
3 Replies

4. Shell Programming and Scripting

Help with sort word and general numeric sort at the same time

Input file: 100%ABC2 3.44E-12 USA A2M%H02579 0E0 UK 100%ABC2 5.34E-8 UK 100%ABC2 3.25E-12 USA A2M%H02579 5E-45 UK Output file: 100%ABC2 3.44E-12 USA 100%ABC2 3.25E-12 USA 100%ABC2 5.34E-8 UK A2M%H02579 0E0 UK A2M%H02579 5E-45 UK Code try: sort -k1,1 -g -k2 -r input.txt... (2 Replies)
Discussion started by: perl_beginner
2 Replies

5. Shell Programming and Scripting

Sort by segment size

fuser -OdV /temp This command returns the output below; inode=132 size=10871 fd=2 5046330 inode=570 size=292 fd=1 5439528 inode=259 size=2669 fd=1 5570758 inode=759 size=255 fd=1 6226124 inode=636 size=1035 fd=1 ... (8 Replies)
Discussion started by: Daniel Gate
8 Replies

6. Shell Programming and Scripting

sort by size

Can some one help in sorting the attached file. I used cmd: sort -r jar1.txt -o sortedjar.txt , but it didnt work. Thanks for your help in Advance. (6 Replies)
Discussion started by: sawyer
6 Replies

7. Shell Programming and Scripting

Can't sort file by size column

Hello, I've done ls -ls >fileout1 When I do the sort command for +4 it sorts it bu group. When I do +5 it sorts it by date. But it's skipping the file size column. Example: rwxr-xr-x 1 Grueben sup 65 16 Sep 13:58 cdee How can I sort it by file size? It doesn't... (2 Replies)
Discussion started by: Grueben
2 Replies

8. UNIX for Dummies Questions & Answers

Sort fdupes output by size

Hi I have a file that is a fdupes output. I'd like to sort the duplicated file by size. The format file is the following: 5996544 bytes each: /path1/to/file1.jpg /path2/to/file1.jpg /pathx/to/file1.jpg ... random number of lines /path999/to/file1.jpg 591910 bytes each:... (2 Replies)
Discussion started by: AdminLew
2 Replies

9. UNIX for Dummies Questions & Answers

sort by size in Mb and Kb

Hi I am using the command below to list the 10 biggest directories and files in my present directory du -hs * | sort +0 | tail -10 the output is 8K disk-space 16K rish 32K WINDOWS 48K tests 104K imgvdEwLa.jpg 168K 020204_aerosmith_1024768.jdk 3.2M Acdc -... (4 Replies)
Discussion started by: the.noob
4 Replies

10. UNIX for Dummies Questions & Answers

sort files by size

Is there a way to sort files by size using the ls command? thanks in advance (1 Reply)
Discussion started by: AMD
1 Replies
Login or Register to Ask a Question