file3"; 2. The tac command can display the file contents in reverse order. The syntax "tac file"; 3. The more command can display the text content in pages, the syntax is "more [+ starting line number] file"; 4. The less command can view the file content forward or backward; 5. The head command can Look at the beginning of the file."/> file3"; 2. The tac command can display the file contents in reverse order. The syntax "tac file"; 3. The more command can display the text content in pages, the syntax is "more [+ starting line number] file"; 4. The less command can view the file content forward or backward; 5. The head command can Look at the beginning of the file.">
Home > Article > Operation and Maintenance > What other Linux file viewing commands are there besides vi?
In addition to vi, other commands to view files: 1. cat command, which can display the contents of text files, with the syntax "cat [-n] file" or "cat file1 file2 >file3"; 2. tac command , the file content can be displayed in reverse order, the syntax is "tac file"; 3. the more command, the text content can be displayed in pages, the syntax is "more [starting line number] file"; 4. the less command, the file content can be viewed forward or backward; 5. Use the head command to view the content at the beginning of the file.
#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.
In addition to vi, linux file viewing commands also include cat, more, less, head, tail, etc.
The cat command can be used to display the contents of a text file (similar to the type command under DOS), or it can also combine several files The content is appended to another file, i.e. the concatenated merge file.
cat may be a commonly used text viewing command, and the usage method is also very simple:
cat file #全文本显示在终端 cat -n file #显示全文本,并显示行号
In addition, cat can also be used to merge files:
cat file1 file2 >file3
This command will The contents of file1 file2 are merged and written to file3.
tac is cat written upside down. tac displays the full text content in reverse order in line units.
tac file
cat outputs the entire text content to the terminal. Then it will bring a problem. If there is a lot of text content, it will be very inconvenient to view the previous content. The more command can be displayed in pages.
1. After displaying the content
more file
, you can use the keys to view the text. Commonly used keys are as follows:
回车 #向下n行,默认为1行 空格 #向下滚动一屏 b #向上滚动一屏 = #输出当前行号 :f #输出当前文件名和当前行号 q #退出
2. Display the contents of the file starting from the specified line
more +10 file
This command starts from the 10th line to display the contents of the file.
3. Display the contents of file starting from the matching string line
more +/string file
This command starts from the first two lines of the line with string.
The less command can view the file content forward or backward
The basic functions of the less command and more are not available There is a big difference, but the less command can browse files forward, while more can only browse files backward, and less also has more search functions.
Common usage:
less file #浏览file less -N file #浏览file,并且显示每行的行号 less -m file #浏览file,并显示百分比
Commonly used keys are as follows:
f #向前滚动一屏 b #向后滚动一屏 回车或j #向前移动一行 k #向后移动一行 G #移动到最后一行 g #移动到第一行 /string #向下搜索string,n查看下一个,N查看上一个结果 ?string #向上搜索string,n查看下一个,N查看上一个结果 q #退出
Compared with the more command, the less command can search for matching required strings.
In addition, less can also switch between browsing multiple files:
less file1 file2 file3 :n #切换到下一个文件 :p #切换到上一个文件 :x #切换到第一个文件 :d #从当前列表移除文件
The role of the head command Just like its name, it is used to display the text at the beginning of the file.
Common usage is as follows:
head -n 100 file #显示file的前100行 head -n -100 file #显示file的除最后100行以外的内容。
is similar to the head command, except that the tail command is used to read text Tail part content:
tail -100 file #显示file最后100行内容 tail -n +100 file #从第100行开始显示file内容
tail also has a more practical usage, which is used to update content in real-time text. For example, if a log file is being written and updated in real time, you can use the command:
tail -f logFile
The updated log content will be printed to the terminal in real time, so you can view the real-time log.
sort can be used to sort and display text. The default is dictionary ascending order.
For example, there is a text test.txt with the following content:
vim count fail help help dead apple
1. Display the text in ascending order
Use the command:
sort test.txt apple count dead fail help help vim
The text content will be displayed in ascending order.
2. Display
related parameters in descending order -r:
sort -r test.txt vim help help fail dead count apple
3. Remove duplicate lines
We can observe that the previous help has two lines. What if we don’t want to see duplicate lines? You can use the parameter -u, for example:
sort -u test.txt apple count dead fail help vim
You can see that the help line is no longer displayed repeatedly.
4. Sort by numbers
If you sort by dictionary, 10 will be in front of 2, so we need to sort by number:
sort -n file
Due to the limited space of this article, we will not introduce it in this article. The wonderful use of the sort command will be introduced separately later.
sed is a stream editor with very powerful functions, but this article only introduces text viewing related functions.
1. Display matching keyword lines
Sometimes when viewing logs, you may only need to view log lines containing certain keywords:
sed -n "/string/p" logFile
The above command means printing lines containing string.
2. Print the specified line
sed -n "1,5p" logFile #打印第1到5行 sed -n '3,5{=;p}' logFile #打印3到5行,并且打印行号 sed -n "10p" logFIle #打印第10行
Common usage is as follows:
uniq file #去除重复的行 uniq -c file #去除重复的行,并显示重复次数 uniq -d file #只显示重复的行 uniq -u file #只显示出现一次的行 uniq -i file #忽略大小写,去除重复的行 uniqe -w 10 file #认为前10个字符相同,即为重复
Viewing files is also very simple:
vim file
When you first open the file, Vim is in command mode, and the bottom of the file will Display some information of the file, including the total number of lines and characters of the file, as well as the current cursor position, etc. At this time, you can use the insert command to enter the input mode to edit the file, as shown in Figure 1.
There are many text viewing commands, and you can choose to use different commands according to different usage scenarios. Some commands have many usages. This article only introduces the classic usages. You can use the man command to view more usages. Many commands can be used in conjunction with other commands, such as ps -elf|more, paging to display process information, etc. You can explore more usages by yourself.
Related recommendations: "Linux Video Tutorial"
The above is the detailed content of What other Linux file viewing commands are there besides vi?. For more information, please follow other related articles on the PHP Chinese website!