As you want only the last modification time (month and date) of the .log
files in the current directory you can simply use the -r
option of date
. There is no need for any regex or other superfluous commands.
You can use this shell one-liner :
for i in *.log; do echo "$i: $(date '+%b-%d' -r "$i")"; done
For example i am finding the month and date of last modification of all .txt
files in the current directory :
$ for i in *.txt; do echo "$i: $(date '+%b-%d' -r "$i")"; done
file.txt: Feb-12
new.txt: Jul-23
The main command involved here is :
date '+%b-%d' -r file.txt
$ date '+%b-%d' -r file.txt
Feb-12
If you want to do the operation recursively, use find
:
find . -type f -name '*.txt' -printf '%p: ' -exec date '+%b-%d' -r {} \;
Example :
$ find . -type f -name '*.txt' -printf '%p: ' -exec date '+%b-%d' -r {} \;
./foo/list.txt: Jul-16
./new.txt: Jul-15
./file.txt: Jul-23
EDIT :
For the sake of answering the original regex
way :
$ ls -l *.txt | grep -Po '.*[[:digit:]]+ \K[[:alpha:]]{3} [[:digit:]]{2}'
Jul 23
Jul 15
grep -Po
indicates we will use PCRE and only take the matched portion
.*[[:digit:]]+
matches upto our desired portion and \K
discards the match
[[:alpha:]]{3} [[:digit:]]{2}
matches three alphabetic characters (month) followed by a space and two digits (date).
date
command doesn't know "EDT" timezone. If a timezone which isn't supported is passed in TZ the output will indeed show with entered time zone but time shown will be UTC.
Supported time zones are listed in /usr/share/zoneinfo/. As of now time zones that can be used as a value for "TZ" are:
Africa Cuba GMT0 Japan Pacific Turkey
America EET GMT-0 Kwajalein Poland UCT
Antarctica Egypt GMT+0 leap-seconds.list Portugal Universal
Arctic Eire Greenwich Libya posix US
Asia EST Hongkong localtime posixrules UTC
Atlantic EST5EDT HST MET PRC WET
Australia Etc Iceland Mexico PST8PDT W-SU
Brazil Europe Indian MST right zone1970.tab
Canada Factory Iran MST7MDT ROC zone.tab
CET GB iso3166.tab Navajo ROK Zulu
Chile GB-Eire Israel NZ Singapore
CST6CDT GMT Jamaica NZ-CHAT SystemV
The time zone for EDT is "EST5EDT". The output of TZ="EST5EDT" date
as of 10 May 2019 02:11:26 UTC:
Thu May 9 22:11:26 EDT 2019
Or use city's name where EDT is used. Examples: America/Kentucky/Louisville, America/Kentucky/Monticello, America/New_York, Canada/Eastern, etc.
Why EDT is EST5EDT?
In short, it is a standard. Time zones are written like Timezone-Offset. Here - is minus or it can be said for time zones which are ahead of UTC are written like Timezone-Offset and for time zones that are behind UTC, it is written as TimezoneOffset. If the time zone has a pretty name like Eastern Standard Time, it can be written as EST or EST5. Both will produce same results.
Now, Daylight Saving Time (DST) isn't observed everywhere (for example, India). For time zones where it is observed, time zones are written as Timezone-OffsetTimezoneDST, example: EET-2EETDST (Cyprus) but if DST has a fancy name like BST (British Summer Time), it is written as Timezone-OffsetFancy_name, i.e. GMT0BST. Similary, EDT is EST5EDT. On this Oracle document you can see all standardized time zones.
Best Answer
Don't use
ls
, this is a job forstat
:-c
lets us to get specific output, here%y
will get us the last modified time of the file in human readable format. To get time in seconds since Epoch use%Y
:If you want the file name too, use
%n
:Set the format specifiers to suit your need. Check
man stat
.Example:
If you want the output like
Tue Jul 26 15:20:59 BST 2016
, use the Epoch time as input todate
:Check
date
's format specifiers to meet your need. Seeman date
too.