1

I want to extract the stretch Jul 22 from the output :

-rw-rw-r-- 1 douglas douglas  92188 Jul 22 08:47 master-terfron-21-07.log

This is the output of the command:

ls -l *.log

How would i use a regular expression to get the required portion?

6
  • 1
    Whats the final goal? To extract the last modified month and date of any file?
    – heemayl
    Jul 23, 2015 at 12:05
  • Hello, I want the file creation date.
    – Roknauta
    Jul 23, 2015 at 12:06
  • Thats the last modification date of the file, not the file creation date..
    – heemayl
    Jul 23, 2015 at 12:07
  • I understand, but it also meets me at the moment
    – Roknauta
    Jul 23, 2015 at 12:09
  • Do something like find . -name *.log -printf "%Ab %Ad".
    – Jos
    Jul 23, 2015 at 12:10

3 Answers 3

5

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).

3

To get the last modification date of your file you can use:

$ stat -c '%n: %y' *.log
alternatives.log: 2015-07-20 09:28:52.727063510 +0200
apport.log: 2015-07-23 09:32:58.282590564 +0200

See the stat man page for further options.

7
  • Sylvain, i have also thought this primarily but OP wants the output in a specific format i.e. %b-%d or perhaps %b %d..
    – heemayl
    Jul 23, 2015 at 14:38
  • @heemayl I've updated my answer, but I still prefer your date -r ;) Jul 23, 2015 at 14:43
  • Yeah, date -r seems simpler to me too in this case.. +1.. :)
    – heemayl
    Jul 23, 2015 at 14:44
  • 1
    You don't need to call stat in a loop, a single invocation will work: stat -c '%n: %y' *.log -- I wish the "human readable" timestamp was configurable though Jul 23, 2015 at 15:14
  • 1
    I'd use stat --printf="%y" in a script if I had to dump it into a variable or pass further. I'd rather use %Y (seconds passed since epoch) instead of %y and convert to format and timezone of my choice. Loved this solution by the way. Cheers!
    – Fr0zenFyr
    Jul 18, 2019 at 10:10
0

A simpler solution to use ls with cut:

ls -l filename | cut -f 6,7 -d' '

example:

$ touch file
$ ls -l file | cut -f 6,7 -d' '
Jul 23

Another solution:

 ls -l filename |awk {'print $9" was last modified   "$6" "$7'}
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .