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 are the Linux read commands for files?

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

Share

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

Editor to share with you what the Linux file reading commands are, I believe 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!

The linux commands for file reading are: 1, "cat and tac", which is used to continuously output the contents of the file on the screen from the first line; 2, "more and less", which is used to start the file from the first line, according to the size of the output window, the appropriate output file contents, and so on.

Linux's command to read the file

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 filename (- n: when displayed, line numbers are output together)

Query using cat: cat filename | grep keyword

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

2 、 more and less

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] filename (number shows the number of rows)

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

Syntax of tail: tail [- n number] filename

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 、 tail

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.

The above is all the contents of the article "what are the Linux read commands for files?" 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