Table of Contents
Full text display --cat
Display the full text in reverse order--tac
Display text in pages --more
Browse and search text at will--less
Display the text header content--head
Display the tail content of the text--tail
Specify the order to display text--sort
Filter and display text--sed
Deduplicate and display text--uniq
Text editing and viewing--vim
Summary
Home Operation and Maintenance Linux Operation and Maintenance What other Linux file viewing commands are there besides vi?

What other Linux file viewing commands are there besides vi?

Jun 16, 2022 pm 05:58 PM
linux

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.

What other Linux file viewing commands are there besides vi?

#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 #显示全文本,并显示行号
Copy after login

In addition, cat can also be used to merge files:

cat file1 file2 >file3
Copy after login

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
Copy after login

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
Copy after login

, you can use the keys to view the text. Commonly used keys are as follows:

回车    #向下n行,默认为1行
空格    #向下滚动一屏
b      #向上滚动一屏
=      #输出当前行号
:f     #输出当前文件名和当前行号
q      #退出
Copy after login

2. Display the contents of the file starting from the specified line

more +10 file
Copy after login

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
Copy after login

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,并显示百分比
Copy after login

Commonly used keys are as follows:

f        #向前滚动一屏
b        #向后滚动一屏
回车或j   #向前移动一行
k        #向后移动一行
G        #移动到最后一行
g        #移动到第一行
/string  #向下搜索string,n查看下一个,N查看上一个结果
?string #向上搜索string,n查看下一个,N查看上一个结果
q    #退出
Copy after login

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     #从当前列表移除文件
Copy after login

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行以外的内容。
Copy after login

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内容
Copy after login

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
Copy after login

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
Copy after login

1. Display the text in ascending order

Use the command:

sort test.txt
apple
count
dead
fail
help
help
vim
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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行
Copy after login

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个字符相同,即为重复
Copy after login

Text editing and viewing--vim

Viewing files is also very simple:

vim file
Copy after login

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.

What other Linux file viewing commands are there besides vi?

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to view the docker process How to view the docker process Apr 15, 2025 am 11:48 AM

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).

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

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)

vscode cannot install extension vscode cannot install extension Apr 15, 2025 pm 07:18 PM

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.

Can vscode be used for mac Can vscode be used for mac Apr 15, 2025 pm 07:36 PM

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.

What is vscode What is vscode for? What is vscode What is vscode for? Apr 15, 2025 pm 06:45 PM

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.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

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.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

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.

vscode Previous Next Shortcut Key vscode Previous Next Shortcut Key Apr 15, 2025 pm 10:51 PM

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 →

See all articles