Linux / UNIX find the smallest directories or files in current directory

by on July 31, 2007 · 0 comments· LAST UPDATED July 31, 2007

in , ,

Q. How do I list or find the smallest directories or files in the current directory under Linux or UNIX like operating system?

A. There is no direct command exists for this task. However by using shell pipes and combination of other commands one can produced the desired result.

Task: Display list of smallest files

You need to use ls command and pass the option -l (long format) -S (sort) -r (in reverse order), enter:
$ ls -lSr
$ ls -lSr
$ ls -lSr | head
$ ls -lSr | head -5

Output:

-rw-r--r--  1 root root         0 May 29 07:08 Muttrc.local
-rw-r--r--  1 root root         0 Jan 12  2000 motd
-rw-r--r--  1 root root         0 Jan 12  2000 exports
-rw-r--r--  1 root root         0 Nov 28  2006 environment
-rw-rw-r--  1 root disk         0 Aug  7  2006 dumpdates
-rw-r--r--  1 root root         0 Jul 10 08:50 cron.deny
-rw-------  1 root root         1 Aug 23  2006 at.deny
lrwxrwxrwx  1 root root         7 Jul 10 08:50 rc -> rc.d/rc
lrwxrwxrwx  1 root root        10 Jul 10 08:50 rc6.d -> rc.d/rc6.d
lrwxrwxrwx  1 root root        10 Jul 10 08:50 rc5.d -> rc.d/rc5.d
lrwxrwxrwx  1 root root        10 Jul 10 08:50 rc4.d -> rc.d/rc4.d
lrwxrwxrwx  1 root root        10 Jul 10 08:50 rc3.d -> rc.d/rc3.d
lrwxrwxrwx  1 root root        10 Jul 10 08:50 rc2.d -> rc.d/rc2.d

Task: Display list of smallest directories

You need to use du command to display sorted (-S option) output. Use pipe to send du command output to sort command for sorting purpouse:
$ du -S . | sort -n
$ du -S . | sort -n | head -10

Output:

du -S . | sort -n | head -10
4       ./lighttpd/ssl
4       ./monit.d
8       ./acpi
8       ./acpi/actions
8       ./alchemist
8       ./alchemist/namespace
8       ./alternatives
8       ./desktop-profiles
8       ./dev.d
8       ./dev.d/default

Read the man page of ls, sort and du for more options:
man ls
man du
man sort

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 0 comments… add one now }

Leave a Comment

Tagged as: , , , , , , ,

Previous Faq:

Next Faq: