Print a file's last modified date in Bash
I can't seem to find how to print out the date of a file. I'm so far able to print out all the files in a directory, but I need to print out the dates with it.
I know I need to attach a date format with the echo of the entry, but all I can't find the correct format.
echo "Please type in the directory you want all the files to be listed"
read directory
for entry in "$directory"/*
do
echo "$entry"
done
Isn't the 'date' command much simpler? No need for awk, stat, etc.
date -r <filename>
Also, consider looking at the man page for date formatting; for example with common date and time format:
date -r <filename> "+%m-%d-%Y %H:%M:%S"
You can use the
stat
command
stat -c %y "$entry"
More info
%y time of last modification, human-readable
Alternatively, you may try also :
date -r filename +"%Y-%m-%d %H:%M:%S"