tail command is a command-line utility for outputting the last part of files given to it via standard input. It writes results to standard output. By default
tail returns the last ten lines of each file that it is given. It may also be used to follow a file in real-time and watch as new lines are written to it.
To view the last ten lines of a file pass the name of a file to the
tail command. The last ten lines of the file will be printed to standard output.
tail /usr/share/dict/words zygote's zygotes zygotic zymurgy zymurgy's Zyrtec Zyrtec's Zyuganov Zyuganov's Zzz
To set the number of lines to show with
tail pass the
-n option followed by the number of lines to show.
tail -n 1 /usr/share/dict/words Zzz
To limit the number of bytes shown with
tail pass the
-c option. Instead of limiting by number of lines this will limit by the number of bytes passed to the
-c option. In the following example the output is limited to 16 bytes.
tail -c 24 /usr/share/dict/words Zyuganov Zyuganov's Zzz
To show the last ten lines of multiple files pass more than one filename to the
tail command. This will output the last ten lines of each file to standard output with a header indicating which file is being shown.
tail /usr/share/dict/words /usr/share/dict/french ==> /usr/share/dict/words <== zygote's zygotes zygotic zymurgy zymurgy's Zyrtec Zyrtec's Zyuganov Zyuganov's Zzz ==> /usr/share/dict/french <== zoos zouave zouaves zozoter zéro zéros zyeuter zézaiement zézaiements zézayer
To suppress the header line pass the
-q option. This can be useful to combine files.
tail -q /usr/share/dict/words /usr/share/dict/french zygote's zygotes zygotic zymurgy zymurgy's Zyrtec Zyrtec's Zyuganov Zyuganov's Zzz zoos zouave zouaves zozoter zéro zéros zyeuter zézaiement zézaiements zézayer
To watch a file for changes with the
tail command pass the
-f option. This will show the last ten lines of a file and will update when new lines are added. This is commonly used to watch log files in real-time. As new lines are written to the log the console will update will new lines.
tail -f /var/log/nginx/access.log 18.104.22.168 - - [03/Oct/2016:21:20:09 +0100] "GET / HTTP/1.1" 200 2213 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/601.7.7 (KHTML, like Gecko)" ...
Newer versions of
tail also support watching multiple files. As the file updates a header will show which line the update is from.
tail -f /var/log/nginx/access.log /var/log/nginx/access.log ==> /var/log/nginx/access.log <== 22.214.171.124 - - [03/Oct/2016:21:23:09 +0100] "GET /apple-touch-icon-precomposed.png HTTP/1.1" 404 162 "-" "Safari/11601.7.7 CFNetwork/760.6.3 Darwin/15.6.0 (x86_64)" ==> /var/log/nginx/error.log <== 2016/10/03 21:23:53 [error] 30632#30632: *1737 access forbidden by rule, client: 126.96.36.199, server: shapeshed.com, request: "GET /wp-login.php HTTP/1.1", host: "shapeshed.com"
tail command can be piped to from other commands. In the following example the output of the
ls command is piped to
tail to only show the five files or folders modified the longest time ago.
ls -t /etc | tail -n 5 login.defs request-key.conf libao.conf mime.types pcmcia
Have an update or suggestion for this article? You can edit it here and send me a pull request.