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

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

By sk
Published: Last Updated on 76.9K 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 above command returned the following output:

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 entire root (/) file system, run:

$ sudo 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

Hope this was useful.

You May Also Like

9 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
sysadmin87 April 2, 2022 - 12:17 pm

ls -t | head -1 # Newest
ls -tr | head -1 # Oldest
The advantage of head -1 over tail -1 is that head will print as soon as it gets the new line, rather than waiting for ls to be completely done.

Reply
TiTi May 6, 2023 - 1:53 pm

Works for a single directory, but cannot be combined with -R, for a directory three.

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
Ronald September 23, 2021 - 4:29 am

Note that “ls -ltr ” includes files and directories, so “ls -lt | tail -1 ” can return the oldest directory. The solution with “find -type f” only returns files.

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. By using this site, we will assume that you're OK with it. Accept Read More