I have searched through google, and this forum to try and find the answer, but alas, nothing quite hits the whole answer.
I am trying to read the last line (or lines) of some log files. I do this often.
The files are named sequentially, using the date as part of the file name, and appending the iteration so like this:
$ls -ltr |tail -6
h20150208.log.001
h20150209.log.001
h20150210.log.001
h20150211.log.001
h20150211.log.002
h20150211.log.003
And I want generally to tail -1 each of the files to see the end result
There may be 33 files - or may be 3000 files - different each time - I don't want to have to manually specify the filenames - but "*log.0??" works for a "filter". Aged files are compressed (.Z) or gzipped (.gz), and obviously I don't want to read these files.
However.
All of the methods I have tried , using find - don't sort the files into the correct order - I need it to be in chronological order, and I also need to see the filename - preferably on the same line as the resultant output - which is effectively succeeded or failure.
All the attempts I have made using ls -ltr error:
tail -1 `ls -ltr *.log.0??`
ls -ltr *.log.0??|xargs tail -f
Just to make this a little more challenging - this will be across multiple different O/Ss, including HP-UX, SunOS, RHEL and more.
I would prefer a solution which doesn't need to create a script file - but any amount of typing (or pasting) at the command prompt is acceptable.
The result I'm looking to achieve is rather like:
h20150208.log.001 Output from last line of file
h20150209.log.001 Output from last line of file
h20150210.log.001 Output from last line of file
h20150211.log.001 Output from last line of file
h20150211.log.002 Output from last line of file
h20150211.log.003 Output from last line of file
So far, the closest I have to what I need is:
for f in $(find . -name '*.log.0??'); do printf $f && cat $f | tail -1; done
But this fails because it isn't sorted in the correct way, and it concatenates the filename and timestamp (with no gap) like so:
./h20150131.log.00123:23:06 completed successfully
./h20150201.log.00123:01:47 completed successfully
./h20150208.log.00104:47:18 completed successfully
./h20150205.log.00102:55:49 completed successfully
./h20150126.log.00123:52:02 completed successfully
./h20150207.log.00105:50:30 JOBFAILURE: COMPLETED WITH ERRORS RC=201
If anyone has any fabulous ideas or can point me in the right direction, I'd be most grateful...
Background:
For anyone who's interested, I'm a DBA currently working in 2nd line support - looking after about 8000 database instances over 3500 hosts. This particular query is to look at backup log files, and try & determine where it all went wrong....
------
Regards,
Ian