The commands to view the contents of a file under the Linux system include: cat means displaying all contents starting from the first line, tac displaying all contents in reverse order from the last line, more displaying contents page by page according to the window size, etc.
【Recommended course: Linux Tutorial】
Command to view file content under Linux
Command to view file content:
cat: by the first Display content starting from the last line and output all contents
tac: Display content in reverse order from the last line and output all contents
more: According to the window size, the file content is displayed page by page
less: similar to more, but its advantage is that you can page forward and search for characters
head: Only the first few lines are displayed
tail: Only the last few lines are displayed
nl: Similar to cat - n, output line number when displaying
tailf: similar to tail -f
cat and tac
cat
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:
-n: When displayed, output the line number together
cat [-n] 文件名
tac
# 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 文件名
more and less (commonly used)
more
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) using the syntax ofmore:
more 文件名
less
The function of less is similar to 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 文件名
[root@redhat etc]# less passwd
/weblogic
head and tail
head
head and tail are usually used when only the first few lines of the file need to be read or Used for the next few lines. The function of head is to display the first few lines of the fileThe syntax of head:
number Display the number of lineshead [n number] 文件名
tail
The function of tail is exactly the opposite of head, only the last few lines of content are displayed.The syntax of tail:
tail [-n number] 文件名
nl## The function of #nl is the same as
cat -n. It also outputs the entire content from the first line and displays the line number.
tailf command is almost equivalent to , strictly speaking it should be the same as 1. Different from , if the file does not grow, it will not access the disk file. tailf always reads bit by bit from the beginning of the file, while tail -f starts reading from the end of the file Note: tail can also achieve a similar effect 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. The above is the detailed content of What are the commands to view files in Linux?. For more information, please follow other related articles on the PHP Chinese website!nl 文件名
tail --follow =name
is more similar. It can continue to track when the file is renamed, which is especially suitable for tracking of log files. Because it can save power and reduce disk access, the tailf command is not a script, but a binary executable file compiled with C code. However, some Linux installations do not have this command.
2. When tailf checks the file growth, it uses the file name and uses the stat system call; and tail -f uses the open file descriptor