


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.
Full text display --cat
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.
Display the full text in reverse order--tac
tac is cat written upside down. tac displays the full text content in reverse order in line units.
tac file
Display text in pages --more
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.
Browse and search text at will--less
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 #从当前列表移除文件
Display the text header content--head
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行以外的内容。
Display the tail content of the text--tail
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.
Specify the order to display text--sort
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.
Filter and display text--sed
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行
Deduplicate and display text--uniq
Common usage is as follows:
uniq file #去除重复的行 uniq -c file #去除重复的行,并显示重复次数 uniq -d file #只显示重复的行 uniq -u file #只显示出现一次的行 uniq -i file #忽略大小写,去除重复的行 uniqe -w 10 file #认为前10个字符相同,即为重复
Text editing and viewing--vim
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.
Summary
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Docker process viewing method: 1. Docker CLI command: docker ps; 2. Systemd CLI command: systemctl status docker; 3. Docker Compose CLI command: docker-compose ps; 4. Process Explorer (Windows); 5. /proc directory (Linux).

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

VS Code One-step/Next step shortcut key usage: One-step (backward): Windows/Linux: Ctrl ←; macOS: Cmd ←Next step (forward): Windows/Linux: Ctrl →; macOS: Cmd →
