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).
find . -name *.log -printf "%Ab %Ad"
.