Linux / Unix: Sort Specific Field or Column

by on November 17, 2011 · 0 comments· LAST UPDATED November 17, 2012

in

How do I sort output of find . -type f -iname "pattern*" -ls command by seventh column in Linux? Sample input:

7782638 15084 -rw-r--r-- 1 tom tom 15422474 Aug 1 2011 ./logserver42/2011/hsbcreports_201107.html
7782637 14504 -rw-r--r-- 1 tom tom 14830464 Jul 1 2011 ./logserver42/2011/hsbcreports_201106.html
7782643 14408 -rw-r--r-- 1 tom tom 14730488 Dec 1 2011 ./logserver42/2011/hsbcreports_201111.html
7782640 19656 -rw-r--r-- 1 tom tom 20101379 Oct 1 2011 ./logserver42/2011/hsbcreports_201109.html
7782634 17588 -rw-r--r-- 1 tom tom 17985190 Apr 1 2011 ./logserver42/2011/hsbcreports_201103.html
7782641 20796 -rw-r--r-- 1 tom tom 21263382 Nov 1 2011 ./logserver42/2011/hsbcreports_201110.html
7798789 13296 -rw-r--r-- 1 tom tom 13590695 Mar 1 2012 ./logserver42/2012/hsbcreports_201202.html
7798788 12220 -rw-r--r-- 1 tom tom 12495686 Feb 1 2012 ./logserver42/2012/hsbcreports_201201.html
5701709 1648 -rw-r--r-- 1 tom tom 1681600 Nov 1 11:14 ./logserver42/2012/hsbcreports_201210.html
7798802 8944 -rw-r--r-- 1 tom tom 9138603 Jun 1 10:13 ./logserver42/2012/hsbcreports_201205.html
5701710 648 -rw-r--r-- 1 tom tom 657811 Nov 10 11:13 ./logserver42/2012/hsbcreports_201211.html
7798805 7360 -rw-r--r-- 1 tom tom 7523891 Sep 1 10:11 ./logserver42/2012/hsbcreports_201208.html
7323999 776 -rw-r--r-- 1 tom tom 787924 Jun 12 2006 ./logserver42/2006/hsbcreports_200605.html
7324001 140 -rw-r--r-- 1 tom tom 139058 Aug 22 2006 ./logserver42/2006/hsbcreports_200607.html
7324000 44 -rw-r--r-- 1 tom tom 41447 Jun 12 2006 ./logserver42/2006/hsbcreports_200606.html
7324003 1972 -rw-r--r-- 1 tom tom 2014614 Oct 1 2006 ./logserver42/2006/hsbcreports_200609.html
7356496 3136 -rw-r--r-- 1 tom tom 3205737 Dec 1 2007 ./logserver42/2007/hsbcreports_200711.html

I need the following output:

7324000 44 -rw-r--r-- 1 tom tom 41447 Jun 12 2006 ./logserver42/2006/hsbcreports_200606.html
7324001 140 -rw-r--r-- 1 tom tom 139058 Aug 22 2006 ./logserver42/2006/hsbcreports_200607.html
5701710 648 -rw-r--r-- 1 tom tom 657811 Nov 10 11:13 ./logserver42/2012/hsbcreports_201211.html
7323999 776 -rw-r--r-- 1 tom tom 787924 Jun 12 2006 ./logserver42/2006/hsbcreports_200605.html
5701709 1648 -rw-r--r-- 1 tom tom 1681600 Nov 1 11:14 ./logserver42/2012/hsbcreports_201210.html
7324003 1972 -rw-r--r-- 1 tom tom 2014614 Oct 1 2006 ./logserver42/2006/hsbcreports_200609.html
7356496 3136 -rw-r--r-- 1 tom tom 3205737 Dec 1 2007 ./logserver42/2007/hsbcreports_200711.html
7798805 7360 -rw-r--r-- 1 tom tom 7523891 Sep 1 10:11 ./logserver42/2012/hsbcreports_201208.html
7798802 8944 -rw-r--r-- 1 tom tom 9138603 Jun 1 10:13 ./logserver42/2012/hsbcreports_201205.html
7798788 12220 -rw-r--r-- 1 tom tom 12495686 Feb 1 2012 ./logserver42/2012/hsbcreports_201201.html
7798789 13296 -rw-r--r-- 1 tom tom 13590695 Mar 1 2012 ./logserver42/2012/hsbcreports_201202.html
7782643 14408 -rw-r--r-- 1 tom tom 14730488 Dec 1 2011 ./logserver42/2011/hsbcreports_201111.html
7782637 14504 -rw-r--r-- 1 tom tom 14830464 Jul 1 2011 ./logserver42/2011/hsbcreports_201106.html
7782638 15084 -rw-r--r-- 1 tom tom 15422474 Aug 1 2011 ./logserver42/2011/hsbcreports_201107.html
7782634 17588 -rw-r--r-- 1 tom tom 17985190 Apr 1 2011 ./logserver42/2011/hsbcreports_201103.html
7782640 19656 -rw-r--r-- 1 tom tom 20101379 Oct 1 2011 ./logserver42/2011/hsbcreports_201109.html
7782641 20796 -rw-r--r-- 1 tom tom 21263382 Nov 1 2011 ./logserver42/2011/hsbcreports_201110.html


You need to use sort command as follows:
sort -n -k 7 /path/to/input
OR
find . -type f -iname "pattern*" -ls |sort -n -k 7
OR
find . -type f -iname "pattern*" -ls |sort -r -n -k 7

Tutorial details
DifficultyEasy (rss)
Root privilegesNo
Requirementssort command
Estimated completion timeN/A

Where,

  1. -r - Reverse the result of sort.
  2. -n - Numeric sort
  3. -k 7 - Start a key at POS1 i.e. start sort key at # 7
  4. -s - Stabilize sort by disabling last-resort comparison
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: