Linux and tail command

Linux tail command

On Unix-like operating systems, the tail command reads a file, and outputs the last part of it (the «tail»).

The tail command can also monitor data streams and open files, displaying new information as it is written. For example, it’s a useful way to monitor the newest events in a system log in real time.

This page covers the GNU/Linux version of tail.

Description

By default, tail prints the last 10 lines of each file to standard output. If you specify more than one file, each set of output is prefixed with a header showing the file name.

If no file is specified, or if file is a dash (««), tail reads from standard input.

Syntax

Options

Option Description
-c [+]num,
—bytes=[+]num
Output the last num bytes of each file.

You can also use a plus sign before num to output everything starting at byte num. For instance, -c +1 prints everything.

A multiplier suffix can be used after num to specify units: b (512), kB (1000), K (1024), MB (1000*1000), M (1024*1024), GB (1000*1000*1000), G (1024*1024*1024), and so on for T (terabyte), P (petabyte), E (exabyte), Z (zettabyte), Y (yottabyte). -f,
—follow[=<name|descriptor>] This option causes tail to loop forever, checking for new data at the end of the file(s). When new data appears, it will be printed.

If you follow more than one file, a header will be printed to indicate which file’s data is being printed.

If the file shrinks instead of grows, tail lets you know with a message.

If you specify name, the file with that name is followed, regardless of its file descriptor.

If you specify descriptor, the same file is followed, even if it’s renamed. This is the default behavior. -F «Follow and retry». Same as using —follow=name —retry. -n num,
—lines=num Output the last num lines, instead of the default (10).

If you put a plus sign before num, tail outputs all lines beginning with that line. For example, -n +1 prints every line. —max-unchanged-stats=num If you are following a file with -f or —follow=name, tail continuously checks the file to see if its size has changed. If the size has changed, it reopens the file and looks for new data to print. The —max-unchanged-stats option reopens a file, even if its size has not changed, after every num checks.

This option is useful if the file might be spontaneously unlinked or renamed, such as when log files are automatically rotated. —pid=pid When following with -f or —follow, terminate operation after process ID pid dies. -q,
—quiet,
—silent Never output headers. —retry Keep trying to open a file even if it’s temporarily inaccessible; useful with the —follow=name option. -s num,
—sleep-interval=num When following with -f or —follow, sleep for approximately num seconds between file checks. With —pid=pid, check process pid at least once every num seconds. -v,
—verbose Always print headers. —help Display a help message, and exit. —version Display version information, and exit.

Examples

Outputs the last 10 lines of the file myfile.txt.

Outputs the last 100 lines of the file myfile.txt.

Outputs the last 10 lines of myfile.txt, and monitors myfile.txt for updates; tail then continues to output any new lines that are added to myfile.txt.

The tail command follows the file forever. To stop it, press Ctrl + C .

This is a useful example of using tail and grep to selectively monitor a log file in real time.

In this command, tail monitors the file access.log. It pipes access.log‘s final ten lines, and any new lines added, to the grep utility. grep reads the output from tail, and outputs only those lines which contain the IP address 24.10.160.10.

cat — Output the contents of a file.
head — Display the first lines of a file.
more — Display text one screen at a time.
pg — Browse page by page through text files.

Источник

Tail command in Linux with examples

It is the complementary of head command.The tail command, as the name implies, print the last N number of data of the given input. By default it prints the last 10 lines of the specified files. If more than one file name is provided then data from each file is precedes by its file name.

Syntax:

Let us consider two files having name state.txt and capital.txt contains all the names of the Indian states and capitals respectively.

Without any option it display only the last 10 lines of the file specified.
Example:

Options:

1. -n num: Prints the last ‘num’ lines instead of last 10 lines. num is mandatory to be specified in command otherwise it displays an error. This command can also be written as without symbolizing ‘n’ character but ‘-‘ sign is mandatory.

Tail command also comes with an ‘+’ option which is not present in the head command. With this option tail command prints the data starting from specified line number of the file instead of end. For command: tail +n file_name, data will start printing from line number ‘n’ till the end of the file specified.

2. -c num: Prints the last ‘num’ bytes from the file specified. Newline count as a single character, so if tail prints out a newline, it will count it as a byte. In this option it is mandatory to write -c followed by positive or negative num depends upon the requirement. By +num, it display all the data after skipping num bytes from starting of the specified file and by -num, it display the last num bytes from the file specified.
Note: Without positive or negative sign before num, command will display the last num bytes from the file specified.

3. -q: It is used if more than 1 file is given. Because of this command, data from each file is not precedes by its file name.

4. -f: This option is mainly used by system administration to monitor the growth of the log files written by many Unix program as they are running. This option shows the last ten lines of a file and will update when new lines are added. As new lines are written to the log, the console will update with the new lines. The prompt doesn’t return even after work is over so, we have to use the interrupt key to abort this command. In general, the applications writes error messages to log files. You can use the -f option to check for the error messages as and when they appear in the log file.

5. -v: By using this option, data from the specified file is always preceded by its file name.

6. –version: This option is used to display the version of tail which is currently running on your system.

Applications of tail Command

1. How to use tail with pipes(|): The tail command can be piped with many other commands of the unix. In the following example output of the tail command is given as input to the sort command with -r option to sort the last 7 state names coming from file state.txt in the reverse order.

It can also be piped with one or more filters for additional processing. Like in the following example, we are using cat, head and tail command and whose output is stored in the file name list.txt using directive(>).

What is happening in this command let’s try to explore it. First cat command gives all the data present in the file state.txt and after that pipe transfers all the output coming from cat command to the head command. Head command gives all the data from start(line number 1) to the line number 20 and pipe transfer all the output coming from head command to tail command. Now, tail command gives last 5 lines of the data and the output goes to the file name list.txt via directive operator.
2. Print line between M and N lines


This article is contributed by Akash Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Источник

14 tail and head commands in Linux/Unix

Many people know about cat command which is useful in displaying entire file content. But in some cases we have to print part of file. In today’s post we will be talking about head and tail commands, which are very useful when you want to view a certain part at the beginning or at the end of a file, specially when you are sure you want to ignore the rest of the file content.

let’s start with the tail command, and explore all of the features this handy command can provide and see how to use it best to suit your needs. After that we will show some options that you can do and can not do with the head command.

Linux tail command syntax

Tail is a command which prints the last few number of lines ( 10 lines by default) of a certain file, then terminates.
Example 1: By default “tail” prints the last 10 lines of a file, then exits.

as you can see, this prints the last 10 lines of /var/log/messages.

Example 2: Now what about you are interested in just the last 3 lines of a file, or maybe interested in the last 15 lines of a file. this is when the -n option comes handy, to choose specific number of lines instead of the default 10.

Example 3: We can even open multiple files using tail command with out need to execute multiple tail commands to view multiple files. Suppose if you want to see first two lines of a

Example 4: Now this might be by far the most useful and commonly used option for tail command. Unlike the default behaviour which is to end after printing certain number of lines, the -f option “which stands for follow” will keep the stream going. It will start printing extra lines on to console added to the file after it is opened. This command will keep the file open to display updated changes to console until the user breaks the command.

As you can see in this example, I wanted to start the crond service, then watch the /var/log/cron log file as service starts. I used ; which a kind of command chaining in Linux inorder to execute two commands in single line. I am not interested in just a few number of lines then exit, but moreover I am interested in keeping watching the whole log file till service starts, then break it with CTRL+C.

Example 5: The same tail -f command can be replicated using less command well. Once you open a file with less

Once you open file, then press shift+f

In order to comeout from update mode in less, you have to press ctrl+c and then press q for quit.

Example 6: We have other option -s which should always be used with -f” will determine the sleep interval, whereas tail -f will keep watching the file, the refresh rate is each 1 second, if you wish to control this, then you will have to use the -s option “sleep” and specify the sleep interval

Example 7: As we seen in example 3, We can open more files using tail command. Even we can view 2 files at the same time growing using -f option as well. It will also print a header viewing which file is showing this output. the header line will be beginning with “==>”

Example 8: If you want to remove this header, use the -q option for quiet mode.

Example 9: Now what if I have a very huge /var/log/messages and I am only interested in the last certain number of bytes of data, the -c option can do this easily. observe the below example where I want to view only the last 500 bytes of data from /var/log/messages

Now, since we have been talking for a while about tail, lets talk about “head” command.

Head command in Linux

Head command will obviously on the contrary to tail, it will print the first 10 lines of the file. Till this part of the post, the head command will do pretty much the same as tail in all previous examples, with exception to the -f option, there is no -f option in head, which is very natural since files will always grow from the bottom.

head command syntax in Linux

Example 10: As mention earlier print first 10 lines.

Example 11: Print first two lines of a file.

Example 12: this option lets you print all lines starting from a line number you specify, unlike Example 11 which will show you the first number of lines you provided.

As you can notice, in this example, it printed all the lines starting after line 27.

Combine head and tail command in Linux

Example 13: As tail and head commands print different parts of files in an effective way, we can combine these two to print some advanced filtering of file content. To print 15th line to 20th line in /etc/passwd file use below example.

Example 14: Many people do not suggest above method to print from one line to other line. The above example is to show how we can combine these things. If you really want to print a particular line, use sed command as shown below.

Источник

Читайте также:  Windows 10 hamachi download
Оцените статью