Tuesday, November 27, 2012

Sort files by size

Here’s how to sort files by size in Linux.
Standard;
ls -lhS
Reverse;
ls -lhSr
Options used;
-l –> print long listing
-h –> print human readable sizes
-S –> sort by file size
-r –> reverse order
Output;
[root@kmon01 log]# ls -lhS
total 70M
-rw-r--r-- 1 root root 36M Mar 31 11:28 messages
-rw-r--r-- 1 root root 15M Mar 31 11:25 cron
-rw-rw-r-- 1 root root 15M Mar 31 04:02 maillog
-rw-rw-r-- 1 root utmp 3.8M Mar 31 11:17 wtmp
-r-------- 1 root root 2.6M Mar 31 11:17 lastlog
-rw-r--r-- 1 root root 1.4M Mar 31 11:13 boot.log
-rw-r--r-- 1 root root 68K Mar 25 04:04 prelink.log
-rw-r--r-- 1 root root 67K May 25 2007 scrollkeeper.log
-rw-r--r-- 1 root root 54K Mar 31 04:02 rpmpkgs
-rw-r--r-- 1 root root 51K Dec 24 2007 xferlog
-rw-r--r-- 1 root root 38K May 25 2007 anaconda.syslog
-rw-r--r-- 1 root root 36K Jun 11 2007 Xorg.0.log
-rw-r--r-- 1 root root 15K Feb 2 10:34 dmesg
-rw-r--r-- 1 root root 13K May 25 2007 anaconda.log

[root@kmon01 log]# ls -lhrS
total 70M
-rw-r--r-- 1 root root 0 May 25 2007 spooler
-rw-r--r-- 1 root root 0 Mar 25 14:38 secure
-rwx------ 1 postgres postgres 0 May 25 2007 pgsql
-rw-r--r-- 1 root root 0 May 24 2007 mcelog
-rw-r--r-- 1 root root 23 Feb 2 10:35 snmpd.log
-rw-r--r-- 1 root root 715 Sep 1 2009 yum.log
-rw-r--r-- 1 mysql mysql 2.0K Dec 8 2008 mysqld.log
-rw-r--r-- 1 root root 2.1K Feb 2 10:35 acpid
-rw-r--r-- 1 root root 2.7K Mar 22 11:46 btmp
drwxr-xr-x 2 root root 4.0K Mar 31 11:21 httpd
drwxr-xr-x 2 root root 4.0K Mar 10 2006 gdm
drwxr-x--- 2 exim exim 4.0K Sep 9 2005 exim
-rw-r--r-- 1 root root 67K May 25 2007 scrollkeeper.log
-rw-r--r-- 1 root root 68K Mar 25 04:04 prelink.log
-rw-r--r-- 1 root root 1.4M Mar 31 11:13 boot.log
-r-------- 1 root root 2.6M Mar 31 11:17 lastlog
-rw-rw-r-- 1 root utmp 3.8M Mar 31 11:17 wtmp
-rw-rw-r-- 1 root root 15M Mar 31 04:02 maillog
-rw-r--r-- 1 root root 15M Mar 31 11:25 cron
-rw-r--r-- 1 root root 36M Mar 31 11:29 messages