Command Line

Some Fun With Sorting The Output Of ‘ls’

Today’s article will be relatively short and easy, as we have some fun with sorting the output of ‘ls’. It’ll be a pretty short and simple article, but that doesn’t mean it’s not useful. Read on and learn about sorting the output of ‘ls’!

The last article I wrote was:

Some Fun With Formatting The Output From ‘ls’

This article will be remarkably similar, but isn’t about formatting the output of ‘ls’, it’s about sorting the output of ‘ls’. Yes, there is a difference! This is useful for a variety of reasons, but it’s mostly useful for viewing your files categorically. There are just a handful of sorting options, but we’ll cover them all.

Like always, it’s important to know that you should not to parse the output of ‘ls’. Parsing the output of ‘ls’ if full of risks, so you shouldn’t do it! The link explains it better than I can, and it tells you what to do instead of parsing the output of ‘ls’.

Obviously, and again, we’ll be using the ‘ls’ command. The ‘ls’ command defines itself fairly accurately as:

ls – list directory contents

So, just like the last article, there’s no real reason to make the intro longer than it needs to be. Let’s just jump right into the article.

Sorting The Output Of ‘ls’:

This article requires an open terminal, like many other articles on this site. If you don’t know how to open the terminal, you can do so with your keyboard – just press CTRL + ALT + T and your default terminal should open.

With your terminal now open, you can stay right there in your /home directory, or you can ‘cd‘ into a directory of your choice – preferably one with a variety of files in it.

When you’re done picking a directory, you have the following choices:

ls --sort=extension
ls --sort=size
ls --sort=time
ls --sort=version
ls --sort=none

See? I even alphabetized them for you! The list of what they do should be fairly obvious, with ls --sort=version being the most unusual and only applying to versioned files.  Also, while included, there’s not a whole lot of reason to use ls --sort=none, as that’s just the default output.

I find sorting by time to be pretty handy, as well as sorting by size. I don’t have much of a reason to use the rest, but they’re there if you need them. I figured I’d include ’em all, rather than just those that I find useful.

Closure:

There you have it, another short article! It’s also another article about the ‘ls’ command, but this time we’re sorting the output of ‘ls’. I figured I’d do this article right after the other one, mostly so that I’d remember to actually cover it. 

Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your own site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

KGIII

Retired mathematician, residing in the mountains of Maine. I may be old and wise, but I am not infallible. Please point out any errors. And, as always, thanks again for reading.

Recent Posts

How To: Install Wine In Lubuntu

Today's article isn't going to be complicated or long as we are going to cover…

15 hours ago

Update Python Packages (PIP)

We've had a run of Python packages recently and you can tell that I'm a…

3 days ago

Save A Command’s Output To A File (While Showing It In The Terminal)

The title is the best I can come up with to describe this exercise as…

5 days ago

Demystifying journalctl: A Comprehensive Guide to Linux System Logging

It was suggested that I write an article about journalctl, which seemed like a large…

7 days ago

Extract Text From Multiple File Types

Today we will have a fairly simple exercise as we're going to just use a…

1 week ago

Meta: I’ve Been At This For Three Years!

I have not done a meta article lately. I don't find them interesting to write…

2 weeks ago