View Linux File Content one day

Source: Internet
Author: User

 

To view the content in the file, you can use many commands. Today I will introduce several commonly used commands for viewing the file content. Of course, there is an ace command (Tool) vim on the Linux system. The use of this tool has been described in detail in vi/vim, you can also edit it easily. Today, we mainly look at the content in the file, not editing it.

 

 

Common commands for viewing File Content

Cat displays the file content starting with the first line

Tac is displayed from the last line, and tac is written by cat

Nl display travel number

More one page displays the File Content

Less is similar to more, but it is better than more.

Head only looks at the first few rows

Tail only looks at the last few lines

Od reads File Content in binary mode

 

Cat (concatenate)

Common Parameters

-B: list the row numbers. Only display row numbers for non-blank rows.

-N: list the row numbers. The row number is displayed together with the blank row, and the blank row is also displayed. Different from the-B Option

Note: If the file is too long, the content of the article will quickly roll over the screen, and you may not be able to see what you want. To prevent this situation, you can use pipelines (|) combined with the less command, the file will be displayed page by page, and you can also use the up or down arrow or flip key to move forward or backward.

 

Tac is written backwards by cat, and the effect is displayed from the last row to the first row. Similar to cat, the display sequence is different. I will not explain it in detail here.

 

Nl

This command is similar to the above cat-B-n parameter, but in actual use, it is not used much. If you are interested, you can play it.

Common Parameters

-B specifies the row number in two ways:

-B a: indicates that the row number (similar to cat-n) is also listed no matter whether the row is empty or not)

-B t: if there are empty rows, do not list the row numbers in the empty row (default)

-N: There are three methods for listing row numbers:

-N ln: the row number is displayed on the leftmost side of the screen.

-N rn: the row number is displayed on the rightmost side of the field without 0.

-N rz: the row number is displayed on the rightmost side of the field and 0 is added.

-W: number of digits occupied by the row number field

 

The above three commands show all the content at a time, so it is inconvenient for us to view the content we want. To solve this problem, we have the following two commands more and less.

 

More page by page

After using this command, there will be corresponding prompts at the bottom of the screen, and there will be several frequently used buttons for us to use.

Space key: Indicates turning one page down

Enter: Indicates turning one row down

/String: indicates that the keyword "string" is searched down in the displayed content.

: F: The file name and the number of currently displayed rows are displayed immediately.

Q: It means to leave more immediately and the file content is no longer displayed.

B or [ctrl] + B: Indicates turning up the page

 

 

 

Less page by page

The usage of less is more user-friendly than that of more. When we use more, we cannot flip the pages forward and only view them backwards. If less is used, you can use the buttons such as PageUp and PageDown to read the file forward or backward. This makes it easier.

Space key: one page down

PageDown: one page down

PageUp: page up

/String: Search down for "string"

? String: search for "string"

N: Repeat the previous search.

N: Repeat the previous search in reverse order.

Q: Exit less.

Is this similar to vi/vim! Haha ......

 

The above is to view the full text, so I just want to see a part of a file, is there any quick way? Of course, see the following introduction.

 

The head displays the content of the previous lines.

-N: Followed by a number, indicating the number of rows. 10 rows by default

If the following number is a negative number, it indicates that the number of rows after the file is not displayed, and all others are displayed. That is to say, all content between the first line and the last n rows is displayed. If this is hard to understand, you will understand it after a hands-on experiment.

For example

Head-n 10 file = head-n + 10 file

Displays the content of the first 10 rows.

Head-n-10 file

Display from scratch to the last 10 rows

 

Tail displays the last few lines of content

-N: Followed by a number, indicating the meaning of the last few rows. 10 rows by default

-F: displays the added content continuously. you must press Ctrl + c to finish viewing the tail. This function is usually used when observing logs.

 

For example

Tail-n 10 file = tail-n-10 file

The last 10 items are displayed.

Tail-n + 10 file

The display starts from row 10th and ends with the last display.

This is the same as head. If you want to extract a part of a file, I believe you can achieve it through the above two learning methods.

 

Od: view the content of a non-plain file

All of the above are plain text files. Of course, some files in the system are not plain text files. If you use the preceding command to view these files, they are all garbled characters, nothing can be seen. At this time, we will use the od command.

Option

-T can be followed by various types of output

A: use the default character output

C: use ASCII characters for output

D [size]: Outputs Data in decimal format.

F [size]: Outputs Data Using floating

O [size]: Outputs Data Using octal

X [size]: Outputs Data in hexadecimal format (hexadecimal ).

You can experiment with the/usr/bin/passwd file. We recommend that you back up the file before the experiment.

Although this command may be of little use to general users, for engineers, it can make a rough output of binary content, and they can see what it is. Unfortunately, I am not an engineer.

 

File command

Here, by the way, we can see the following three examples. This file command also has many parameters, which can be researched by yourself.

File ~ /. Bashrc

File/var/lib/mlocate. db

File/usr/bin/passwd

From Yu Fei's blog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.