Sort ls -l output

To sort the command "ls -l" output by each column in Linux use the following commands:

user@server the_directory> ls -l | sort -k3

Let's break this down:

ls - lists the files in the current directory
-l - tells the "ls" command to list all the info about the files which outputs something like the following:

-rw-r--r-- 1 nelson telson 33 Jan 17 09:16 dummy-file.txt
-rw-r--r-- 1 jonson telson 33 Jan 17 09:16 original-file.txt

| - when then pipe this output to the program "sort" with the pipe (|) symbol
sort - takes in the entire output and structures it into a spread sheet like data structure with columns and rows
-k3 - says to alpha numerically sort the data structure by the contents of column 3

Which will then output something like this:

-rw-r--r-- 1 jonson telson 33 Jan 17 09:16 original-file.txt
-rw-r--r-- 1 nelson telson 33 Jan 17 09:16 dummy-file.txt

Et Voila! You have sorted by username.

to sort by user group - ls -l | sort -k4
to sort by size - ls -l | sort -k5
to sort by month - ls -l | sort -k6
to sort by filename - ls -l | sort -k9

you get the picture.


comments powered by Disqus