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

Commands on how to view the contents of a file under linux

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

Share

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

Editor to share with you how to view the contents of the file under the linux command, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

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 according to the window size, page by page of the actual file content

Less is similar to more, but it has the advantage of turning pages forward and searching for characters.

Head displays only the first few lines

Tail displays only the last few lines

Nl is similar to cat-n and outputs the line number when displayed.

Tailf is similar to tail-f

1.cat and tac

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: cat [- n] file name (- n: when displayed, line numbers are output together)

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.

2.more and less (commonly used)

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 also be used with the pipe character "|" (pipe), for example: ls-al | more

Syntax for 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

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.

Syntax for 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.

3.head and tail

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

Syntax for head: head [n number] file name (number shows the number of lines)

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

Syntax of tail: tail [- n number] file name

4.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.

Syntax for nl: nl file name

5.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 trace when the file is renamed, which is especially suitable for log file tracking (follow the growth of a log file). Unlike tail-f, if the file does not grow, it will not access the disk file (It is similar to tail-f but does not access the file when it is not growing. This has the side effect of not updating the access time for the file, so a filesystem flush does not occur periodically when no log activity is happening. Tailf is especially suitable for tracking log files on laptops because it saves power and reduces disk access (tailf is extremely useful for monitoring log files on a laptop when logging is infrequent and the user desires that the hard disk spin down to conserve battery life.). The tailf command is not a script, but a binary execution file compiled in C code, which is not available after some Linux installations. This article provides a way to compile and install tailf commands.

Let's talk about the difference between the two:

1. Tailf always reads from the beginning of the file, while tail-f starts from the end of the file.

2. Tailf check file growth, using the file name, using stat system call; and tail-f is using the open file descriptor; Note: tail can also achieve similar tracking file name effect; but tail always uses fstat system call, not stat system call; the result is: by default, when tail files are secretly deleted, tail does not know, and tailf is aware.

Common parameters

Format: tailf logfile

Dynamically trace the log file logfile, initially printing the last 10 lines of the file.

The above are all the contents of the command on how to view the contents of the file under linux. Thank you for your reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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