Sunday 29 January 2012

tail


tail

tail command is one of my favourite commands because of  its  usefulness  in viewing the last few lines of a required file and also what is being written into a file ( using –f option).it can also be used to see the progress of cp, mv, tar and other commands.

tail by default shows last ten lines of a file.it can be made to view any last ‘n’ lines where n is a number

Example 1)
to view the last 1000 lines of a huge  script page wise ,
/home/k109$ tail -1000 valuefind.sh | pg
.................
.................. #lines of scripts
...........
.
.
.
.................
Standard input      <--- #here pg waits and asks for you to enter a key before which it displays the next page. This is helpful in analysing a script or debugging it page wise.


Example 2)
To view the file contents while it is being written, use –f option. If an application is writing continuously into a file and to know what is being written, this option can be used.
One disadvantage of this command is however that it cannot be used inside scripts because once
The application stops writing to a file the command does not come out of tail directly  and requires some signal like stop or kill to come out.

    Suppose a file is being written by cp command, you can use tail –f  <destination file> to know
exactly what is being written.

Example 3)
To view the contents of the file starting from a particular line, use  tail + <number>  <file1>
Suppose you have a file  worldsport.txt

/home/k109$cat  worldsport.txt
The game is played on a rectangular field of grass or green artificial turf, with a goal in the middle of each of the short ends. The object of the game is to score by driving the ball into the opposing goal. In general play, the goalkeepers are the only players allowed to touch the ball with their hands or arms, while the field players typically use their feet to kick the ball into position, occasionally using their torso or head to intercept a ball in midair. The team that scores the most goals by the end of the match wins. If the score is tied at the end of the game, either adraw is declared or the game goes into extra time and/or a penalty shootout, depending on the format of the competition.

If it is required to view the file starting from second line,use

/home/k109$tail  +2  worldsport.txt
short ends. The object of the game is to score by driving the ball into the opposing goal. In general play, the goalkeepers are the only players allowed to touch the ball with their hands or arms, while the field players typically use their feet to kick the ball into position, occasionally using their torso or head to intercept a ball in midair. The team that scores the most goals by the end of the match wins. If the score is tied at the end of the game, either adraw is declared or the game goes into extra time and/or a penalty shootout, depending on the format of the competition.

No comments:

Post a Comment