tail Commandtail命令

Display the end of files显示文件的结尾部分

What is tail?什么是tail?

The tail command outputs the last part of files. By default, it shows the last 10 lines. The -f (follow) option is particularly powerful for monitoring log files in real-time as new entries are added.

tail命令输出文件的结尾部分。默认情况下,它显示最后10行。-f(follow)选项对于实时监控日志文件特别有用,因为它可以在添加新条目时持续显示。

Basic Usage基本用法

$ tail file.txt
# Shows last 10 lines (default)
$ tail -n 20 file.txt
# Shows last 20 lines
$ tail -f /var/log/syslog
# Follows the file as it grows (live monitoring)
$ tail file.txt
# 显示最后10行(默认)
$ tail -n 20 file.txt
# 显示最后20行
$ tail -f /var/log/syslog
# 跟踪文件增长(实时监控)

Common Options常用选项

Option选项 Full Name完整名称 Description描述 Example示例
-n N lines行数 Print last N lines打印最后N行 tail -n 5 file.txt
-f follow跟踪 Output appended data as file grows文件增长时输出追加的数据 tail -f logfile.txt
-F follow=name跟踪名称 Follow even if file is rotated即使文件被轮转也跟踪 tail -F syslog
-n +N skip跳过 Start from Nth line to end从第N行开始到末尾 tail -n +10 file.txt
-c N bytes字节 Print last N bytes打印最后N字节 tail -c 100 file.txt

Practical Scenarios实际场景

Real-time Log Monitoring实时日志监控

$ tail -f /var/log/apache2/access.log
# Monitor web server access logs in real-time
$ tail -f -n 100 /var/log/syslog
# Show last 100 lines, then follow
$ tail -f /var/log/apache2/access.log
# 实时监控Web服务器访问日志
$ tail -f -n 100 /var/log/syslog
# 显示最后100行,然后跟踪

Skip Header Lines跳过标题行

$ tail -n +2 data.csv
# Skip first line (header), show rest
$ tail -n +2 data.csv
# 跳过第一行(标题),显示其余部分

Memory Tricks记忆技巧

🐕 "tail" = the tail/end of something - Think of a dog's tail at the back!

🐕 "tail" = 某物的尾部/末端 - 想想狗的尾巴在后面!

👀 -f = Follow - Keep following the file like a shadow!

👀 -f = Follow(跟随) - 像影子一样持续跟随文件!

➕ -n +N = Start from N - Positive N means from line N to end

➕ -n +N = 从N开始 - 正N意味着从第N行到末尾