Tutorial on using tail, a UNIX and Linux command for outputting the last part of files
https://shapeshed.com/unix-tail/
Last updated
Linux and Unix tail command tutorial with examples
Tutorial on using tail, a UNIX and Linux command for outputting the last part of files. Examples of outputting the last ten lines of a file, limiting the number of lines, limiting the number of bytes, showing multiple files, watching a file for changes and using pipes.
Table of contents
What is the tail command?
The
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.How to view the last ten lines of a file
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.How to limit the number of lines to show
To set the number of lines to show with
tail
pass the -n
option followed by the number of lines to show.How to limit the number of bytes to show
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.How to show multiple files
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.
To suppress the header line pass the
-q
option. This can be useful to combine files.How to watch a file for changes
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.
Newer versions of
tail
also support watching multiple files. As the file updates a header will show which line the update is from.How to use tail with pipes
The
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.
No hay comentarios:
Publicar un comentario