preface

In Linux, we often use vim, nano and other text editor to view, add or delete the file content, there is no doubt that these tools are very convenient, I also use a lot, but the view commands below actually I also use a lot, the reason is that can match piping (|) for text add or delete, or filter, So it’s also important to understand the following commands.

The test file

To better understand the commands, here is a test file:

#Notice that the second line is blank000 | 2021-05-20 00:00:00, num: 1 = = = 2021-05-20 00:00:00, 000 | num: 2 = = = 2021-05-20 00:00:00, 000 | num: 3 = = = 2021-05-20 00:00:00, 000 | num: 4 = = =Copy the code

cat

The cat command is used to display all text contents on the terminal. Therefore, the cat command is suitable for viewing small files. Large files occupy resources easily.

Common uses of the cat command are:

#No arguments
$ cat cat_test.log000 | 2021-05-20 00:00:00, num: 1 = = = 2021-05-20 00:00:00, 000 | num: 2 = = = 2021-05-20 00:00:00, 000 | num: 3 = = = 2021-05-20 00:00:00, 000 | num: 4 = = =
#-b argument: displays line numbers, but skips blank lines
$ cat -b cat_test.log000 | 1 2021-05-20 00:00:00, num: 1 = = = 2021-05-20 00:00:00 2, 000 | num: 2 = = = 2021-05-20 00:00:00 3, 000 | num: 3 = = = 4 2021-05-20 00:00:00, 000 | num: 4 = = =
#-n: displays line numbers but does not skip blank lines
$ cat -n cat_test.log000 | 1 2021-05-20 00:00:00, num: 1 = = = 2, 3, 2021-05-20 00:00:00, 000 | num: 2 = = = 4 2021-05-20 00:00:00, 000 | num: 3 = = = 5 2021-05-20 00:00:00, 000 | num: 4 = = =Copy the code

more

Compared with the cat command, the more command does not display all contents at one time. Instead, it loads the contents one page at a time to avoid occupying too many resources.

Common uses of the more command are:

#-number controls the number of pages displayed on each page
$ more -3 cat_test.log000 | 2021-05-20 00:00:00, num: 1 = = = 2021-05-20 00:00:00, 000 | num: 2 = = =
#The +number argument controls which page to display from
$ more +3 cat_test.log000 | 2021-05-20 00:00:00, num: 2 = = = 2021-05-20 00:00:00, 000 | num: 3 = = = 2021-05-20 00:00:00, 000 | num: 4 = = =
# 

#Common Shortcut keys- Enter: next line - Space: Next page - Ctrl + F: Next page - Ctrl + B: Previous pageCopy the code

less

The less command is similar to the more command but provides more features than the more command. The less command does not need to read the entire file to display the file, so it is faster than editors such as vi and vim.

There are two differences between the less and more commands:

  • 1. The up and down keys of the less command can be used to turn pages, but the more command cannot
  • 2. The Shell does not leave the newly displayed content after less exits, while the Shell leaves the newly displayed content after more exits

Common uses of the less command are as follows:

-Enter key: next line -Y key: Previous line # Too much this - Space key: Next page - Ctrl + F: Next page - Ctrl + B: Previous pageCopy the code

head

The head command is mainly used to view the first few lines of text. Different from the more and less commands, you need to run the head command again to view the next content after the head command output.

The head usage is also simple, with -n controlling the number of lines to display:

$ head --help
head: illegal option -- -
usage: head [-n lines | -c bytes] [file ...]

#Look at the first three lines
$ head -n 3 cat_test.log000 | 2021-05-20 00:00:00, num: 1 = = = 2021-05-20 00:00:00, 000 | num: 2 = = =Copy the code

tail

The tail command, in contrast to the head command, outputs the following lines of text. This command is often used to view logs because logs usually look for the latest, or last, text.

Common uses of the tail command are:

#-n argument: Shows how many lines follow
$ tail -n 1 cat_test.log000 | 2021-05-20 00:00:00, num: 4 = = =
#The -f argument: keeps the program from exiting and continues to display new lines in the file
$ tail -n 1 -f cat_test.log000 | 2021-05-20 00:00:00, num: 4 = = =Copy the code

Note: The tail command is the one I use the most. It is used to look at logs, such as Nginx request logs.

Write in the last

The commands above have their own characteristics, and how they are used depends on different scenarios and individual usage habits.

Dear bosses, creation is not easy, but it needs to be honed and summarized constantly, welcome to pay attention to me, I am Yan Gan, I will share with you all kinds of practical programming knowledge and skills, your praise and attention is the biggest motivation for my progress and creation!!