≡ Menu

sort command

Linux / UNIX Shell: Sort IP Address

Q. I'd like to sort a list of IP address stored in a text file. How do I sort by last notation or entire address under Linux or UNIX operating systems?
[click to continue…]

Diagnosing a dos Attack Under Linux

Q. How do I find out if I Linux box is under DoS attack or not form a shell prompt?
[click to continue…]

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

How do I find the largest top files and directories on a Linux or Unix like operating systems?
[click to continue…]