Home  >  Article  >  Operation and Maintenance  >  Command to view file contents under linux

Command to view file contents under linux

步履不停
步履不停Original
2019-06-13 17:55:194351browse

Command to view file contents under linux

Command to view the contents of the file:

cat starts displaying the content from the first line and outputs all the contents

tac Displays the content in reverse order from the last line and outputs all the content

more Based on the window size, the actual file content is displayed page by page

less is similar to more, but has the advantage of being able to page forward and searching for characters

head. Only the first few lines of # are displayed.

##tail Only display the last few lines

nl Similar to cat -n, output the line number when displaying

tailf is similar to tail -f

##1.cat and tac

cat's function is to continuously add the contents of the file starting from the first line. The output is on the screen. However, cat is not commonly used. The reason is that when the file is large and the number of lines is relatively large, only part of the content can be seen when the screen cannot accommodate it all.

cat syntax: cat [-n] file name (-n: when displayed, output together with the line number)

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

tac syntax: tac file name.

2.more and less (commonly used)

The function of more is to convert files Starting from the first line, the file content is output appropriately according to the size of the output window. When the entire page cannot be output, you can use the "Enter key" to scroll down the line and the "Space bar" to scroll down the page. To exit the viewing page, please press the "q" key. In addition, more can also be used with the pipe character "|" (pipe), for example: ls -al | more

more's syntax: more file name

Enter goes down n lines, needs to be defined, the default is 1 line;

Ctrl f scrolls down one screen;

Space bar scrolls down one screen;

Ctrl b returns to the previous screen;

= Outputs the line number of the current line;

:f Output the file name and line number of the current line;

v Call the vi editor;

! command calls Shell and executes the command;

q exit more

The function of less is similar to that of more, but using more cannot turn pages forward, but can only turn pages backward.

less can use the [pageup] and [pagedown] keys to turn pages forward and backward, which seems more convenient.

less syntax: less file name

less also has a function to search for the content you want to find in the file, assuming If you want to check whether there is a weblogic string in the passwd file, you can do it like this:

[root@redhat etc]# less passwd

Then enter:

/weblogic

Enter

If there is For weblogic strings, Linux will display the characters in highlighted form.

To exit the viewing page, please press the "q" key.

3.head and tail

head and tail are usually used when only reading Use it to get the first few lines or the last few lines of the file. The function of head is to display the first few lines of the file

The syntax of head: head [n number] file name (number displays the number of lines)

The function of tail is just the opposite of head, only the last few lines of content are displayed

The syntax of tail: tail [ -n number] File name

##4.nl

nl functions and cat -n is the same, it also outputs the entire content from the first line and displays the line number

nl syntax: nl file name

5.tailf

The tailf command is almost equivalent to tail -f. Strictly speaking, it should be more similar to tail --follow=name. It can also continue to track when the file is renamed, which is especially suitable for following the growth of a log 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 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 executable file compiled with C code. Some Linux installations do not have this command. This article provides how to compile and install the tailf command.

Let’s talk about the difference between the two:

1. tailf always reads bit by bit from the beginning of the file, while tail -f starts reading from the end of the file

2. When tailf checks the file growth, it uses the file name and uses the stat system call; while tail -f uses the opened file descriptor; Note: tail can also achieve similar effects of tracking file names. ; But tail always uses the fstat system call instead of the stat system call; the result is: by default, when tail's files are secretly deleted, tail does not know, but tailf does.

Commonly used parameters

Format: tailf logfile

Dynamic tracking log file logfile, initially print the last 10 lines of the file.

For more Linux-related technical articles, please visit the Linux Tutorial column to learn!

The above is the detailed content of Command to view file contents under linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn