Home > Article > Operation and Maintenance > What are the commands to view files in Linux?
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 文件名Enter n lines down, needs to be defined, the default is 1 OK; 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 executes the command ; q Exit 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 文件名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 something in the passwd file. If there is no weblogic string, you can do this:
[root@redhat etc]# less passwdThen enter:
/weblogicEnterIf there is a weblogic string at this time, Linux will replace the character Highlighted. To exit the viewing page, please press the "q" key.
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.
nl 文件名
tailf command is almost equivalent to
tail -f, strictly speaking it should be the same as 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.
1. Different from
tail -f, 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 file2. 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
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!