Home Command line utilities How To Find The Oldest File In A Directory Tree In Linux
Find The Oldest File In A Directory Tree In Linux

How To Find The Oldest File In A Directory Tree In Linux

By sk
3852 Views

The other day a fellow Linux user asked me how to find the oldest file in a directory from command line in Linux. We could tell the oldest file in a directory by using the date and time stamps in GUI mode. But, I don't know how to find it from the command line. Fortunately, I found this solution after a bit of digging in Google. If you ever wondered how to find the oldest file in a directory or in the entire file system, read on. It is not that difficult.

Find The Oldest File In A Directory Tree In Linux

To find the oldest file in a directory, for example /home/sk/ostechnix/, run:

$ find /home/sk/ostechnix/ -type f -printf '%T+ %p\n' | sort | head -n 1

Let us break down this command and see what each command line argument does.

Here,

  • find - Search for files in a directory hierarchy.
  • /home/sk/ostechnix/ - Search location.
  • type -f - Searches only the regular files.
  • -printf '%T+ %p\n' - Prints the file’s last modification date and time in separated by + symbol. (Eg. 2015-07-22+13:42:40.0000000000). Here, %p indicates the file name. \n indicates new line.
  • sort | head -n 1 - The sort command sorts the output and sends the output to head command to display the oldest file. Here, -n 1 indicates only one file i.e oldest file.

As you might already know, Explainshell helps you to find what each part of a Linux command does.

The Sample output for the above command would be:

2015-07-22+13:42:40.0000000000 /home/sk/ostechnix/Absolute FreeBSD_ 2nd Edition.pdf

As you see in the above output, Absolute FreeBSD_ 2nd Edition.pdf is the oldest file in /home/sk/ostechnix/ directory. Please note that Linux doesn't find the oldest file by using the file creation date. Instead, it uses the file modification date to find it.

Likewise, to find the top five oldest files in a given directory, just run:

$ find /home/sk/ostechnix/ -type f -printf '%T+ %p\n' | sort | head -n 5

Sample output:

2015-07-22+13:42:40.0000000000 /home/sk/ostechnix/Absolute FreeBSD_ 2nd Edition.pdf
2016-11-28+21:03:05.0000000000 /home/sk/ostechnix/Etcher-linux-x64.AppImage
2016-12-14+18:28:20.5162190000 /home/sk/ostechnix/ubuntu.jpg
2016-12-18+18:14:46.5931480000 /home/sk/ostechnix/Marconi Union - Sleepless.mp3
2017-03-17+19:28:27.8193330000 /home/sk/ostechnix/The Devops toolkit.pdf

The oldest file will be displayed first.

To find the oldest file in the root (/) file system, run:

$ find / -type f -printf '%T+ %p\n' | sort | head -n 1

Update:

A fellow Linux user has pointed out how to find the oldest or newest files in a directory in the comment section below. It is much easier than my method.

To find out the oldest file in a directory, go to that directory and run:

$ ls -lt | tail -1

To find out the newest file in a directory:

$ ls -ltr | tail -1

That's it. You know now how to find the oldest file in a given directory tree in Unix-like operating systems. I will be soon here with another interesting guide soon. If you find this helpful, please share it on your social and professional networks, so that other users can also benefit.

More good stuffs to come. Stay tuned!

Cheers!

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

6 comments

gocyclones April 11, 2017 - 8:28 pm

ls -lt | tail -1 Oldest
ls -ltr | tail -1 Newest

Reply
SK April 12, 2017 - 11:45 am

Thanks. I never know this commands.

Reply
Bro. Vin March 13, 2019 - 6:35 am

Will List 20 newest files>>> ls -ltr | tail 20
Will List 20 oldest files>>> ls -ltr | head 20

Reply
Joshua Sweet Koebler October 14, 2019 - 7:49 am

Never ever use the output of ls in a script, as its behavior is unpredictable when the filenames contain whitespaces or weird characters such as line breaks.
So if you need to use the file name in a script, then don’t use ls. Use find.

Reply
Vishal February 21, 2020 - 12:41 pm

Will this ls option return directories too?

Reply
sk February 21, 2020 - 1:31 pm

Yes it will if you use “-type d” instead of “-type f” parameter in the above command.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More