General

Let’s Use ‘ls’ To Sort Files By Time

Today’s article is going to tell you how to sort files by time while using the ‘ls’ command in the terminal. I find sorting by time is often easiest when working with a lot of files that have less-than-helpful filenames. For example, it’s nice to sort by time when I’m dealing with screenshots, knowing that I took a new screenshot that’s helpfully named something like ‘kgiii@kgiii-msi: ~-Downloads_032.png’.

It’s usually easy enough to graphically sort files by time and date. In your file manager, you might need to select list view, add the column, and then select at least one of the time options. Different GUI file managers will have different options, and may not include all possible time values stored in the file’s meta information. Speaking of screenshots, it might look something like this:

In this case, arranging items by ‘modification time’ is an available option.

The file’s metadata has several time options and we can sort by those with the ‘ls’ command while in the terminal. The output of ‘ls’ is usually sorted alphabetically. It’s not terribly difficult and will help you along your Linux trails. I find it useful when picking among a large number of files.

Parsing the output of the ‘ls’ command is generally considered a bad idea. We won’t really be doing that, but this is a good time to mention it. If you don’t know why, click this link. They explain it better than I can. That’ll save some time!

Today we will learn to use ‘ls’ to sort files by a few time value stored in the file’s meta information. It’s an easy enough process and a handy tool for your growing toolbox of Linux commands.

Sort Files By Time:

Obviously, you’ll need an open terminal. You can do so with your keyboard – just press CTRL + ALT + T and your default terminal should open.

Once you have it open, the /home/user directory is a fine choice. We won’t need to change directories – but you can. If you have a lot of screenshots saved in ~/Pictures, then you may find that directory more informative. Either way, let’s start with the basics:

This first one will show the last time the file was modified – which may be the creation date and time. That command is (the -l used in each command means use the long listing output):

ls -lt

You can sort by access time, sorting by the last time the file was opened. (Yes, this is all part of a file’s metadata.) To do that, you just use:

ls -ltu

With that done, we have one more. This one shows the last time the metadata was changed for the files listed. If you were to use touch to change the last modified time, this would show when you did that. Make sense?

ls -ltc

And there you have it! If you want to show the output in reverse order, you just use a -r flag and you can still use the -a flag to show hidden files, should you need to do something like that.

Closure:

That’s about it. You can now sort files by time. It’s a pretty handy tool and one you may find yourself using often. As mentioned above, I find it handiest (with my particular uses) when working with all the screenshots I take. I take a whole lot of screenshots. I assume I take more screenshots than most folks.

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

A Quick Look At The Shutdown Command

This is going to be just a quick article about a command many of you…

20 hours ago

Setting Up Coding Environments on Linux for Educational Use

With so many strong attributes, such as robustness and flexibility, Linux stands as a powerful…

3 days ago

View Detailed Hardware Information

There are many tools for showing your hardware information and today we'll get to view…

5 days ago

How To: Install Wine In Lubuntu

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

7 days ago

Update Python Packages (PIP)

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

1 week 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…

2 weeks ago