Skip to content

[Linux] Use cat, head, tail commands to print text

If we want to print out text in Linux system, we can use vi/vim to edit the text file.

But today if we just only want to check the file content, and don’t need to change any content, we can use other commands to print out the file content.

Today, I want to record cat, head, tail commands.


cat

cat is a common Linux command, it can print out all content of file. For example, I want to check a file named wiki_rainbow.txt file:

cat wiki_rainbow

Output:

Too long so not all printed

In addition to printing directly, the cat command also has the following parameters that can be set:

  • -n: Number of rows (starting from 1)
  • -b: Blank lines are not numbered
  • -s: Reduce consecutive blank lines to a single blank line

head

However, since cat just can print out the entire file content, we can use head to print out the beginning of the file when the content is too large or we only need part of the content.

There are two main display modes for head:

  • -c: Decide the number of characters to be printed
  • -n: Determine the number of lines to be printed

for example:

head -n 5 wiki_rainbow.txt

Output:

As you can see, compared with just now, we only printed the first five lines.


tail

The tail and head commands are like instructions with opposite functions: head mainly prints the beginning, and tail prints the end.

Like head, tail also has the following parameters:

  • -c: Decide the number of characters to be printed
  • -n: Determine the number of lines to be printed
tail -n 4 wiki_rainbow.txt

Output:

It seems that the newline symbol “\n” is used to determine the number of lines.


References


Read More

Tags:

Leave a Reply