find oldest file in directory in Linux

How to Find Oldest File in Directory

Linux provides various commands to find files & directories on your disk using numerous different criteria. Sometimes you may need to find oldest file in directory. In this article, we will learn the different ways to do this in Linux.

How to Find Oldest File in Directory

We will learn a couple of different ways to find oldest file in Directory.

1. Using find command

Find command allows you to search files & directories using different criteria such as name, type, date of creation, date of modification, permissions, etc.

Here is the command to get oldest file in directory using find command.

$ find <directory path> -type f -printf '%T+ %p\n' | sort | head -n 1

In the above command,

  • <directory path> – directory path to look for required files.
  • -type f – search for files only. To search for directory, use -type d.
  • -printf ‘%T+ %p\n’ – prints the last modification date & time of file (defined by %T) and file path (defined by %p). The \n adds a new line.
  • Sort – sorts the files numerically based on modification date & time and passes its output to the head command
  • head -n 1 – displays the 1 oldest file.

For example, if you want to find the oldest file in directory /home/ubuntu, use the following command.

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

Similarly, if you want the 3 oldest files, replace 1 at the end of command with 3.

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

On the other hand, if you want to display the oldest directory and not file, use -type d instead of -type f

$ find <directory path> -type d -printf '%T+ %p\n' | sort | head -n 1

For example, if you want to list oldest directories at /home/ubuntu, here is a sample command for it.

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

Similarly, if you want to list oldest 3 directories in a directory, use head -n 3 as shown below.

$ find /home/ubuntu -type d -printf '%T+ %p\n' | sort | head -n 3

2. Using ls command

You can also use ls command to get oldest files & directories in Linux. But this command will output both files as well as directories. Here is the command to get the oldest file/directory using ls command.

$ ls -t <directory path> | tail -1

$ ls -tr <directory path> | head -1

The output of above command can be either a file or directory depending on who has the oldest date of modification. If you do not specify a directory path, it will list oldest file/directory in present working directory.

Here is an example command to list oldest file or directory in /home/ubuntu.

$ ls -t /home/ubuntu | tail -1

$ ls -tr /home/ubuntu | head -1

Similarly, you can also get the oldest 3 files or directories in Linux by replacing ‘1’ with ‘3’ as shown below.

$ ls -t /home/ubuntu | tail -3
$ ls -tr /home/ubuntu | head -3

In this article, we have learnt how to display oldest files & directories in a directory. You can use either of these commands for this purpose. But we recommend to use find command which is versatile and more powerful than ls command when it comes to finding files & directories.

Also read:

How to Show Disk Usage for Top Level Directories in Linux
How to Resolve bin/sh : 1 source not found
How to Reload /etc/hosts in Linux
How to Kill Stopped Jobs in Linux
How to Change MAC Address in Linux

Leave a Reply

Your email address will not be published.