I am currently writing a script to archive a couple log files and want them combined in a single archive which is named according to the first and last lines date and time in one of the log files (i.e. access.log).
But for the best of me I can't wrap my head around how to get this information from the lines and assemble it to a file name.
the lines in question are from an apache.log file which i simply could get with head
and tail
:
Example:
$ head -n1 /home/server/log/access.log.1
84.1.11.243 - - [21/Jan/2017:14:53:49 +0000] "GET /index.php/2016/05/26/tutorial-how-to-install-ubuntu-and-other-debian-based-distributions-via-debootstrap/ HTTP/1.1" 200 18413 "https://www.google.hu/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"
$ tail -n1 /home/server/log/access.log.1
71.3.17.120 - - [20/Dec/2017:16:17:50 +0000] "POST / HTTP/1.1" 200 27639 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; LCTE; rv:11.0) like Gecko"
The expected file name result should be including the date and time stamps from those messages.
Example line I would like to use, but can change that according to the answers results:
tar -caf "backup-logfiles-$start-til-$end.tar.gz" access.log error.log ftp.log
Any solution is welcome to extract that values into $start
and $end
.
Best Answer
Here's a horribly convoluted shell one-liner (using the date format you mentioned in chat):
To get the start and end variables separately, do:
Or, if you want a numerical date: