Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 18, 2024
There are several commands that we can use to get different kinds of information about files and folders in a directory. In this tutorial, we’ll focus on the commands to get information about the most recently updated or modified file in a directory.
Let’s create a sample directory:
$ mkdir sample_directory
Then, we navigate into it and add random files. We’ll create and name the files chronologically to keep track of them:
$ cd sample_directory $ touch file_1.txt$ touch file_2.txt$ touch file_3.txt
We should have this directory structure overall:
$ ls
file_1.txt file_2.txt file_3.txt
find is a Linux command to search for files and directories and perform operations on them. It supports searching by file type, folder, name, creation date, modification date, owner, and permissions.
Let’s use this command to display the latest file in our sample directory:
$ find /sample_directory -type f -exec ls -t1 {} + | head -1
sample_directory/file_3.txt
Here’s a breakdown of this command:
file_3.txt was the last file we created during the setup, which proves the find command works.
Furthermore, we can append the -r flag with the ls command so that find displays several recent files arranged in descending order:
$ find sample_directory/ -type f -exec ls -tr1 {} +
./file_3.txt
./file_2.txt
./file_1.txt
Like earlier, we can alter the number of recent files shown by passing it to the head command.
The ls command lists files or directories in Linux and other Unix-based operating systems. It accepts several flags that allow us to modify how files and folders are listed on the terminal.
We can use ls to view the most recent file through this command:
$ ls sample_directory/ -Artls | tail -1
0 -rw-rw-r-- 1 <user> 0 Thur 11 05:11 file_3.txt
Here’s a breakdown of the command:
Parsing data with ls is error-prone when the filenames contain special characters such as spaces or newlines because the ls command separates filenames with newlines by default.
In the output above, the size of file_3.txt is 0 bytes because it is an empty text file.
Alternatively, we can also use this ls command to get the most recent file:
$ ls -tp | grep -v /$ | head -1
file_3.txt
Here’s a breakdown of this command:
Moreover, we can also display the most recently created or modified files arranged in descending order:
$ ls -tl
total 0
-rw-rw-r-- 1 <user> 0 Thur 11 05:11 file_3.txt
-rw-rw-r-- 1 <user> 0 Thur 11 05:11 file_2.txt
-rw-rw-r-- 1 <user> 0 Thur 11 05:11 file_1.txt
A Bash script is a plain text file that contains a series of commands that we would typically have to manually type on the terminal. This can be useful when saving commands that are regularly used or running a chain of commands.
This method is relatively faster than find when performing searches in large directory trees.
Let’s create a recursive Bash script file that’ll retrieve the most recent file.
First, let’s create an executable text file in our working directory:
$ touch bash_script.sh
Next, let’s add the shebang header. This ensures that Bash will be used to interpret the script:
$ echo "#!/bin/bash" >> bash_script.sh
Next, let’s open up the file with the nano editor and paste in this code under the shebang header:
CURRENT_DIR='.'
get_recent_file () {
FILE=$(ls -Art1 ${CURRENT_DIR} | tail -n 1)
if [ ! -f ${FILE} ]; then
CURRENT_DIR="${CURRENT_DIR}/${FILE}"
get_recent_file
fi
echo $FILE
exit
}
get_recent_file
This function retrieves the most recent item in the directory. If the item is a directory, it’ll navigate into that directory and find the most recent item recursively.
Finally, we need to set the bash_script.sh file as an executable:
$ chmod +x bash_script.sh
We can now execute our script file from the terminal with this command:
$ ./bash_script.shbash_script.sh
The output shows bash_script.sh as the most recent file because it’s the last file we updated in the working directory.
perl is a general-purpose, high-level, interpreted, and dynamic programming language. It was originally developed for text processing and conversion of text files to different formats.
To get the most recent file in our directory, we’ll combine perl with the find command through pipes:
$ find sample_directory/ -type f -printf '%T@\t%p\n' | perl -ane '@m=@F if ($F[0]>$m[0]); END{print $m[1];}'
./file_3.txt
Here’s a breakdown of this command:
In this article, we’ve covered most of the Linux tools that can be used to get information about the most recently created or updated file. We also looked at how to output more than one recent file arranged in chronological order.