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