This is the second day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021

This article is part 43 of the Linux Minimalist Trivia series. This article introduces some useful and practical Linux knowledge.

The functions of these commands are described briefly:

  • Cat displays all text content to the terminal.
  • Head Displays the first n lines of a file.
  • Tail Displays the contents from the end of the file. Similarly, -n displays the contents of the last n lines.
    • If you specify the -f option, the displayed information is updated after the file content is updated.
  • Wc statistics text content information, such as the number of lines, characters, bytes and so on!

This article uses a my.txt file with 365 lines as an example for practical demonstration.

The cat command

The cat command is used to view all contents in a file.

$ cat my.txt line-1 line-2 line-3 ...... Line-362 line-363 line-364 line-365 is omittedCopy the code

It will display everything on the screen.

  • cat -nThe line numbers of all lines are displayed on output.
  • cat -bOutput line number to non-null.

Head Displays the content of the file header

  • The first 10 lines are displayed by default.
$ head my.txt
line-1
line-2
line-3
line-4
line-5
line-6
line-7
line-8
line-9
line-10
Copy the code
  • Specifies the first n lines to display
$ head -n 6 my.txt
line-1
line-2
line-3
line-4
line-5
line-6
Copy the code

-num: head -6 my.txt

Tail Displays the content at the end of a file

Tail using

The last 10 lines at the end are displayed by default.

$ tail my.txt
line-356
line-357
line-358
line-359
line-360
line-361
line-362
line-363
line-364
line-365
Copy the code
  • Specifies that the last n lines are displayed
$ tail -6 my.txt
line-360
line-361
line-362
line-363
line-364
line-365
Copy the code

Or use the -n option: tail -n 6 my.txt.

Tail -f Synchronously updates file content changes

Changes in the file are displayed synchronously:

$ tail -6f my.txt
line-360
line-361
line-362
line-363
line-364
line-365
Copy the code

tail -f logfileUseful and practical for viewing and monitoring updates to log files in real time!

Wc Statistics file information

Wc is short for word-count and is mainly used to Count words (characters).

Wc Specifies the default output

$ wc my.txt
 365  365 3177 my.txt
Copy the code

Meaning of output content:

  • Column 1: the number of lines in the text file, including blank and non-blank lines.
  • Column 2: Number of words, based on the number of words separated by space
  • Column 3: the number of bytes of the file.
  • Column 4: file name

Wc -l Displays the number of lines in the document

The –lines option.

$ wc -l my.txt
365 my.txt
Copy the code

Wc-w printed file word book

The –words option.

$ wc -w my1.txt
730 my1.txt
Copy the code

Wc-c Indicates the number of bytes in a file

Equivalent — bytes.

$ wc -c my.txt
3177 my.txt
Copy the code

Wc -m Displays the number of characters in the file

Equivalent – chars.

$ wc -m my.txt
3177 my.txt
Copy the code

Wc statistics multiple files

$ wc my.txt my1.txt
 365  365 3177 my.txt
 365  730 3177 my1.txt
 730 1095 6354 total
Copy the code

Do not display file names using standard input

With standard input, you can leave the file name out of the WC result.

$ wc < my.txt
 365  365 3177
Copy the code