Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print a file's last modified date in Bash

People also ask

How do I find the last modified date?

Windows file propertiesRight-click the file and select Properties. In the Properties window, the Created date, Modified date, and Accessed date is displayed, similar to the example below.

How do I print the last modified file in Linux?

Finding Files Modified on a Specific Date in Linux: The flag -t is used to list last modified files, newer first. Then you can combine ls -lt with grep to print all files which were modified on a specific date.

How do you get the last modified date of a file in Linux?

The syntax is pretty simple; just run the stat command followed by the file's name whose last modification date you want to know, as shown in the example below. As you can see, the output shows more information than previous commands.


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"

On OS X, I like my date to be in the format of YYYY-MM-DD HH:MM in the output for the file.

So to specify a file I would use:

stat -f "%Sm" -t "%Y-%m-%d %H:%M" [filename]

If I want to run it on a range of files, I can do something like this:

#!/usr/bin/env bash
for i in /var/log/*.out; do
  stat -f "%Sm" -t "%Y-%m-%d %H:%M" "$i"
done

This example will print out the last time I ran the sudo periodic daily weekly monthly command as it references the log files.


To add the filenames under each date, I would run the following instead:

#!/usr/bin/env bash
for i in /var/log/*.out; do
  stat -f "%Sm" -t "%Y-%m-%d %H:%M" "$i"
  echo "$i"
done

The output would was the following:

2016-40-01 16:40
/var/log/daily.out
2016-40-01 16:40
/var/log/monthly.out
2016-40-01 16:40
/var/log/weekly.out

Unfortunately I'm not sure how to prevent the line break and keep the file name appended to the end of the date without adding more lines to the script.


PS - I use #!/usr/bin/env bash as I'm a Python user by day, and have different versions of bash installed on my system instead of #!/bin/bash


Adding to @StevePenny answer, you might want to cut the not-so-human-readable part:

stat -c%y Localizable.strings | cut -d'.' -f1

I wanted to get a file's modification date in YYYYMMDDHHMMSS format. Here is how I did it:

date -d @$( stat -c %Y myfile.css ) +%Y%m%d%H%M%S

Explanation. It's the combination of these commands:

stat -c %Y myfile.css # Get the modification date as a timestamp
date -d @1503989421 +%Y%m%d%H%M%S # Convert the date (from timestamp)

EDITED: turns out that I had forgotten the quotes needed for $entry in order to print correctly and not give the "no such file or directory" error. Thank you all so much for helping me!

Here is my final code:

    echo "Please type in the directory you want all the files to be listed with last modified dates" #bash can't find file creation dates

read directory

for entry in "$directory"/*

do
modDate=$(stat -c %y "$entry") #%y = last modified. Qoutes are needed otherwise spaces in file name with give error of "no such file"
modDate=${modDate%% *} #%% takes off everything off the string after the date to make it look pretty
echo $entry:$modDate

Prints out like this:

/home/joanne/Dropbox/cheat sheet.docx:2012-03-14
/home/joanne/Dropbox/Comp:2013-05-05
/home/joanne/Dropbox/Comp 150 java.zip:2013-02-11
/home/joanne/Dropbox/Comp 151 Java 2.zip:2013-02-11
/home/joanne/Dropbox/Comp 162 Assembly Language.zip:2013-02-11
/home/joanne/Dropbox/Comp 262 Comp Architecture.zip:2012-12-12
/home/joanne/Dropbox/Comp 345 Image Processing.zip:2013-02-11
/home/joanne/Dropbox/Comp 362 Operating Systems:2013-05-05
/home/joanne/Dropbox/Comp 447 Societal Issues.zip:2013-02-11