How to use head and tail linux command


Today we will learn basic linux command, which is very useful in daily use. Generally people used to forget these kind of commands.

  1.  Given a list of text lines in a file, to display first 10 lines from the begining.
    [user@localhost~]$ head foo.txt     // Here foo.txt is a file name. If the file is not present in current directory, then you have to give absoute path of file.
  2. To display last 10 lines.
    [user@localhost~]$ tail foo.txt
  3. Try -n option to specify number of lines to display
    [user@localhost~]$ tail -n3 foo.txt          // it will display last 3 lines
    [user@localhost~]$ head -n4 foo.txt       // it will display first 4 lines
  4. And don't forget '+' in the tail and '-' in the head.
    [user@localhost~]$ tail -n+10 foo.txt    // start from the 10th line till last.
    [user@localhost~]$ head -n-10 foo.txt // print all except the last 10 lines.
  5. print from line 6 to 10
    [user@localhost~]$ head -n10 foo.txt | tail -n5
SHARE

    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment