The Linux commands used for file reading are: 1. "cat and tac", which is used to continuously output the contents of the file on the screen starting from the first line; 2. "more and less", Used to start the file from the first line, output the file content appropriately according to the size of the output window, etc.
Recommended: "linux system tutorial"
Linux file reading command
1. cat and tac
The function of cat is to continuously output the contents of the file on the screen starting from the first line. 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 filename (-n: when displayed, output the line number together)
Use cat query: cat filename | grep keyword
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 filename.
2. more and less
The function of more is to start the file from the first line and output the file content 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 a line and the "Space bar" to scroll down a 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 n lines down, 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;
= Output the line number of the current line;
:f Output the file name and the line number of the current line;
v Call the vi editor;
! The command calls the Shell , and execute the command;
q exit more
The function of less is similar to more, but using more cannot turn pages forward, only 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 that allows you to search for the content you want to find in the file. Suppose you want to find whether there is a weblogic string in the passwd file. , then you can do it like this:
[root@redhat etc]# less passwd
Then enter:
/weblogic
Enter
If there is a weblogic string at this time, Linux will highlight the character.
To exit the viewing page, please press the "q" key.
3. head and tail
Head and tail are usually used when only the first few lines or the last few lines of the file need to be read. The function of head is to display the first few lines of the file
The syntax of head: head [n number] filename (number displays the number of lines)
The function of tail is exactly the same as On the contrary, head only displays the last few lines of content.
tail syntax: tail [-n number] filename
4, nl
nl function Like cat -n, it also outputs the entire content from the first line and displays the line number.
nl syntax: nl file name
5, tail
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.
The above is the detailed content of which linux commands can be used for file reading. For more information, please follow other related articles on the PHP Chinese website!