Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What commands does linux have to view files?

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

This article is to share with you the introduction of the command to view files under Linux. I believe most people do not know how to use this command. In order to make you understand better, I have summarized the following content for you. Without saying much, let's read on.

Command to view the contents of a file under Linux

Commands to view the contents of the file:

Cat: displays the content from the first line and outputs all the content

Tac: displays the content in reverse order from the last line and outputs all the content

More: displays the contents of a file page by page according to the size of the window

Less: similar to more, but with the advantage of turning the page forward and searching for characters

Head: only the first few lines are displayed

Tail: show only the last few lines

Nl: similar to cat-n, output line number when displayed

Tailf: similar to tail-f

Cat and tac

Cat

The function of cat is to output the contents of the file continuously on the screen starting from the first line. But cat is not commonly used, because when the file is large and the number of lines is large, and the screen cannot hold all of it, you can only see part of the content.

Cat syntax:

-n: when displayed, the line number is output together

Cat [- n] file name

Tac

The function of tac is to reverse the file from the last line and output the content data to the screen. We can find that tac is actually written backwards by cat. This command is not commonly used either.

Tac syntax:

Tac file name

More and less (commonly used)

More

The function of more is to start the file from the first line and output the file contents appropriately according to the size of the output window. When a page cannot be fully output, you can use the enter key to flip down the line and the space bar to turn the page down. To exit the view page, press the "Q" key. In addition, more can be used with the pipe character "|" (pipe)

The syntax of more:

More file name

Enter goes down n lines and needs to be defined. The default is 1 line.

Ctrl f scrolls down one screen

The space bar scrolls down one screen

Ctrl b returns to the previous screen

= output the line number of the current line

: F output file name and line number of the current line

V call the vi editor

! Command invokes Shell and executes the command

Q exit more

Less

The function of less is similar to that of more, but you can't turn the page forward with more, only backward.

Less can use the [pageup] and [pagedown] keys for forward and backward page flipping, which looks more convenient.

The syntax of less:

Less file name

Less also has a feature that allows you to search for what you are looking for in a file. Suppose you want to find a weblogic string in a passwd file, then you can do this:

[root@redhat etc] # less passwd

Then enter:

/ weblogic

Enter

At this point, if there is a weblogic string, linux will highlight the character.

To exit the view page, press the "Q" key.

Head and tail

Head

Head and tail are usually used when you only need to read the first few lines or the next few lines of the file. The function of head is to display the first few lines of a file

The syntax of head:

Number shows the number of rows

Head [n number] file name

Tail

Tail is just the opposite of head, showing only the last few lines

The syntax of tail:

Tail [- n number] file name

Nl

The function of nl is the same as cat-n, which outputs all the contents from the first line and displays the line number.

The syntax of nl:

Nl file name

Tailf

The tailf command is almost equivalent to tail-f, and strictly speaking should be more similar to tail-- follow=name. It can also continue to track when the file is renamed, which is especially suitable for log files

Stalking. Because it saves power and reduces disk access, the tailf command is not a script, but a binary execution file compiled in C code, but some Linux do not have this command after installation

Comparison between tailf and tail-f

1. Unlike tail-f, if the file does not grow, it will not access the disk file. Tailf always reads from the beginning of the file, while tail-f starts at the end of the file.

2. When the tailf check file grows, the file name is used and the stat system call is used, while tail-f uses the open file descriptor

Note: tail can do the same thing as tracking file names; but tail always uses fstat system calls instead of stat system calls; the result is: by default, when tail text

When the item was secretly deleted, tail did not know it, while tailf knew it.

After reading the above, have you mastered the use of the file view command under Linux? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report