How to Get Oldest File in Directory Linux


Finding the oldest file in a directory is a common task when managing files in Linux and there are several ways to obtain the oldest file in a directory. The process involves using commands in the Linux terminal to sort files in a directory by date and time, and then selecting the oldest file from the list. Here are some methods you can use to obtain the oldest file in a directory in Linux.

Method 1: Using the Find Command

The find command is a versatile tool for searching for files in a directory hierarchy. The -type f option specifies that only regular files should be considered, while the -printf option is used to print the file’s modification time in the format of seconds since the Unix epoch, followed by a space and the file name. Further here is the syntax for using find command to find the oldest file in any directory:

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

The -printf option is used to print out the file’s modification timestamp (%T+) and filename (%p), separated by a space and a newline. This output is then piped (|) to the sort utility, which sorts the lines of output by the modification timestamp (oldest first). The head utility is used to select the first line of output which corresponds to the oldest file in the directory. For instance, I have used the above syntax to find the oldest file in my home directory:

Advantages: This method is accurate and can handle files whose timestamps were manually changed.

Disadvantages: This method requires the use of multiple commands.

Method 2: Using the LS Command

The ls command is used to list the contents of a directory and here is the syntax for using this command to get the oldest file in Linux:

ls -ltr <directory path> | tail -<number-of-oldest-files>

This command uses the ls utility to list all files in the current directory, sorted by modification time (-t) in reverse order (-r, newest first). The output of this command is then piped (|) to the tail utility, which selects the last line of output, corresponding to the oldest file in the directory. For instance, I have used the above syntax to find the three oldest files in my home directory:

Advantages: This method is simple and requires only one command.

Disadvantages: This method may not be accurate if file modification times have been altered. Additionally, it may be less efficient than using the find command, especially for directories with many files.

Conclusion

Both the find and ls commands can be used to find the oldest file in a directory in Linux. The find command is more accurate and can handle files whose timestamps were manually changed but requires the use of multiple commands. The ls command is simpler and requires only one command but may not be accurate if file modification times have been altered and may be less efficient. Choose the method that best suits your needs based on the size of your directory and the accuracy you require.

Interested in copying the files? click here.

Print Friendly, PDF & Email