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:
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.
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.