Print the last modified file date in Bash

I can not find how to print the file date. I can still print all the files in the directory, but I need to print the dates with it.

I know that I need to attach a date format with an echo of the recording, but I cannot 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 
+112
date unix file bash shell
May 6 '13 at 2:28
source share
10 answers

You can use stat command

 stat -c %y "$entry" 

Additional Information

 % y time of last modification, human-readable
+120
May 6 '13 at 2:30
source share

Isn't the date command a lot easier? No need for awk, stat, etc.

 date -r <filename> 

Also, consider viewing date formatting on a manual page ; for example with a common date and time format:

 date -r <filename> "+%m-%d-%Y %H:%M:%S" 
+178
Dec 27 '13 at 20:25
source share

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

So, to indicate the file, I would use:

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

If I want to run it in a series 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 display the last time I ran the sudo periodic daily weekly monthly command when it refers to log files.




To add file names for 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 be as follows:

 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 line breaks and keep the file name added to the end of the date without adding extra lines to the script.




PS - I am using #!/usr/bin/env bash since I am a Python user during the day and have different versions of bash installed on my system, not #!/bin/bash

+13
May 01 '16 at 7:21
source share

By adding @StevePenny to the answer, you can cut off the part that is unreadable to humans:

 stat -c%y Localizable.strings | cut -d'.' -f1 
+6
May 6 '13 at 2:33
source share

Best is date -r filename +"%Y-%m-%d %H:%M:%S"

+5
Apr 18 '18 at 6:07
source share

I wanted to get the modification date of the file in the format YYYYMMDDHHMMSS . Here is how I did it:

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

Explanation. This is a 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) 
+3
Aug 30 '17 at 8:44 on
source share

EDITED: It turns out that I forgot the quotation marks needed for $entry to correctly print and not give the error "there is no such file or directory." Thanks to everyone for helping me!

Here is my last 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 

Printed as follows:

 /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 
+2
May 6 '13 at 3:18
source share

For line breaks, I edited your code to get something without line breaks.

  #!/bin/bash for i in /Users/anthonykiggundu/Sites/rku-it/*; do t=$(stat -f "%Sm" -t "%Y-%m-%d %H:%M" "$i") echo $t : "${i##*/}" # t only contains date last modified, then only filename 'grokked'- else $i alone is abs. path done 
+2
May 13 '16 at 8:35
source share

If the file name has no spaces:

 ls -l <dir> | awk '{print $6, " ", $7, " ", $8, " ", $9 }' 

This prints as the following format:

  Dec 21 20:03 a1.out Dec 21 20:04 a.cpp 

If the file names have a space (you can use the following command for file names without spaces, it just looks complicated / ugly than the first one):

  ls -l <dir> | awk '{printf ("%s %s %s ", $6, $7, $8); for (i=9; i<=NF; i++){ printf ("%s ", $i)}; printf ("\n")}' 
+1
May 6 '13 at 2:51
source share

You can use:

 ls -lrt filename |awk '{print "%02d",$7}' 

This will display the date in 2 digits.

If between 1 and 9 it adds the prefix "0", it is converted to 01–09.

Hope this meets expectations.

0
Jan 07 '19 at 14:40
source share



All Articles