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
$ 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,
%pindicates the file name.
\nindicates new line.
sort | head -n 1- The
sortcommand sorts the output and sends the output to
headcommand to display the oldest file. Here,
-n 1indicates 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
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 entire root (
/) file system, run:
$ sudo 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
Hope this was useful.
ls -lt | tail -1 Oldest
ls -ltr | tail -1 Newest
Thanks. I never know this commands.
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.
Works for a single directory, but cannot be combined with -R, for a directory three.
Will List 20 newest files>>> ls -ltr | tail 20
Will List 20 oldest files>>> ls -ltr | head 20
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.
Will this ls option return directories too?
Yes it will if you use “-type d” instead of “-type f” parameter in the above command.
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.