Lệnh hiển thị các dòng TEN cuối cùng trong tệp là gì: / var / log / syslog


17

Tôi muốn biết lệnh nào để hiển thị các dòng TEN cuối cùng trong tệp: /var/log/syslog


1
Chào mừng bạn đến hỏi Ubuntu! @ user260487: Nếu câu trả lời của tôi hữu ích cho bạn, thì vui lòng xem xét việc đánh dấu nó là câu trả lời được chấp nhận để người khác có thể dễ dàng tìm thấy nó hơn trong tương lai. Đây cũng là một cách lịch sự để cảm ơn người trả lời câu hỏi của bạn đã giúp bạn.
Danatela

Câu trả lời:


33

Thử

tail /var/log/syslog

Thí dụ:

$ tail /var/log/syslog
Mar 21 11:41:32  whoopsie[1194]: last message repeated 15 times
Mar 21 11:42:32  whoopsie[1194]: last message repeated 20 times
Mar 21 11:43:32  whoopsie[1194]: last message repeated 16 times
Mar 21 11:44:32  whoopsie[1194]: last message repeated 14 times
Mar 21 11:45:32  whoopsie[1194]: last message repeated 11 times
Mar 21 11:46:32  whoopsie[1194]: last message repeated 16 times
Mar 21 11:47:32  whoopsie[1194]: last message repeated 15 times
Mar 21 11:48:32  whoopsie[1194]: last message repeated 14 times
Mar 21 11:49:32  whoopsie[1194]: last message repeated 16 times
Mar 21 11:50:32  whoopsie[1194]: last message repeated 22 times

Đối với mặc định, tailhiển thị 10 dòng cuối cùng của tệp đầu vào. Để hiển thị nhiều hơn, có một tùy chọn -n.
Từ man tail:

-n, --lines=K
       output the last K lines, instead of the last 10; or use -n +K to
       output lines starting with the Kth

Ví dụ:

$ tail -n 15 /var/log/syslog
Mar 21 11:56:45 Karimov-Danil named[1122]: error (network unreachable) resolving './DNSKEY/IN': 2001:dc3::35#53
Mar 21 11:56:45 Karimov-Danil named[1122]: error (network unreachable) resolving './NS/IN': 2001:dc3::35#53
Mar 21 11:56:48 Karimov-Danil named[1122]: managed-keys-zone: Unable to fetch DNSKEY set '.': timed out
Mar 21 11:56:51 Karimov-Danil whoopsie[1194]: online
Mar 21 11:57:54  whoopsie[1194]: last message repeated 17 times
Mar 21 11:58:58  whoopsie[1194]: last message repeated 16 times
Mar 21 12:00:03  whoopsie[1194]: last message repeated 10 times
Mar 21 12:01:03  whoopsie[1194]: last message repeated 16 times
Mar 21 12:02:03  whoopsie[1194]: last message repeated 10 times
Mar 21 12:03:03  whoopsie[1194]: last message repeated 17 times
Mar 21 12:04:03  whoopsie[1194]: last message repeated 14 times
Mar 21 12:05:03  whoopsie[1194]: last message repeated 17 times
Mar 21 12:06:03  whoopsie[1194]: last message repeated 13 times
Mar 21 12:07:04  whoopsie[1194]: last message repeated 16 times
Mar 21 12:08:04  whoopsie[1194]: last message repeated 16 times

Câu trả lời là chính xác, nhưng bạn có thể mở rộng nó. Kể về tailnhiều hơn. Ví dụ: tail -15 /var/log/syslogsẽ hiển thị 15 dòng cuối, v.v ...
c0rp

1

taillà cách để đi, tuy nhiên trong trường hợp vì bất kỳ lý do gì bạn không có tail, bạn có thể sử dụng tac+awk

tac input.txt | awk 'NR <=10' 
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.