使用命令在Linux系統中查看文件的內容是Linux管理員的基本技能之一,在Linux中,有許多應用程序以不同的方式顯示文件內容。您可以使用cat、less、more、head、tail、nl、od、xxd、gv、xdvi命令來查看文本文件或任何其他文件。為了對此進行測試,我創建了file-demo.txt和file-demo-1.txt文件,并添加了定義和一些內容來試驗這些命令。
1)cat命令
cat最常見用法是讀取文件的內容。
cat命令的通用語法:
cat [Option] [File_Name]
如何在Linux上使用cat命令,以下為您講解,參考在Linux/Unix系統上使用cat和cp命令的示例。
新用戶經常使用此命令讀取文件,雖然不錯,但效率不高:
# cat file-demo.txt
This is a test file to experiment the "File View" command.
GNU Linux
Debian
Ubuntu
Arch Linux
Linux Mint
CentOS
Red Hat
Fedora
openSUSE
Manjaro
elementary
MX Linux
Solus
Deepin
在cat命令中添加-n選項,以在cat命令輸出中打印行號:
# cat -n file-demo.txt
1 This is a test file to experiment the "File View" command.
2 GNU Linux
3 Debian
4 Ubuntu
5 Arch Linux
6 Linux Mint
7 CentOS
8 Red Hat
9 Fedora
10 openSUSE
11 Manjaro
12 elementary
13 MX Linux
14 Solus
15 Deepin
使用下面的cat命令格式一次讀取多個文件:
# cat file-demo.txt file-demo-1.txt
2)less命令
less命令使您可以使用UP & DOWN箭頭鍵或Page UP & Page Down按鈕在頂部到底部快速查看文件內容。
less命令的通用語法:
less [Option] [File_Name]
如何在Linux上使用less命令,以下為您講解。
如果要搜索文件中與格式匹配的任何行,那么less命令是一個不錯的選擇。如果找到,它將突出顯示給定的模式。按CTRL+n轉到下一個匹配模式,然后按CTRL+p返回上一個匹配模式。
為此,請運行以下命令:
# less file-demo.txt
This is a test file to experiment the "File View" command.
GNU Linux
Debian
Ubuntu
Arch Linux
Linux Mint
CentOS
Red Hat
Fedora
openSUSE
Manjaro
elementary
MX Linux
Solus
Deepin
(END)
3)more命令
more命令使您可以從上至下快速在屏幕上快速查看文件內容。
more命令的通用語法:
more [Option] [File_Name]
如何在Linux上使用more命令,以下為您講解。
它顯示帶有百分比的頁面,另外,允許用戶查找模型匹配,但是,它不會突出顯示模式,并且在當前頁面上也沒有找到相關性:
# more file-demo.txt
This is a test file to experiment the "File View" command.
GNU Linux
Debian
Ubuntu
Arch Linux
Linux Mint
CentOS
Red Hat
Fedora
openSUSE
Manjaro
elementary
MX Linux
Solus
Deepin
--More--(18%)
4)head命令
head命令顯示文件的開頭(文件的第一部分)。
head命令的通用語法:
head [Option] [File_Name]
如何在Linux上使用head命令,以下為您講解。
默認情況下,它將打印前10行,如果要打印其他行,可以使用(N)選項:
# head file-demo.txt
This is a test file to experiment the "File View" command.
GNU Linux
Debian
Ubuntu
Arch Linux
Linux Mint
CentOS
Red Hat
Fedora
openSUSE
5)tail命令
tail命令顯示文件的最后一部分。
tail命令的通用語法:
tail [Option] [File_Name]
如何在Linux上使用tail命令,請參考演示使用Linux tail命令的示例。
默認情況下它將打印最后10行,如果要打印其他行,可以使用(N)選項:
# tail file-demo.txt
Linux Mint
CentOS
Red Hat
Fedora
openSUSE
Manjaro
elementary
MX Linux
Solus
Deepin
6)nl命令
nl命令用于在屏幕上以行號打印文件內容,行號從“1”到“N”開始,這將一次在屏幕上打印文件,類似于cat命令。
nl命令的通用語法:
nl [Option] [File_Name]
如何在Linux上使用nl命令,以下為您講解。
輸入文件名,然后輸入nl命令以打印帶有行號的文件內容:
# nl file-demo.txt
1 This is a test file to experiment the "File View" command.
2 GNU Linux
3 Debian
4 Ubuntu
5 Arch Linux
6 Linux Mint
7 CentOS
8 Red Hat
9 Fedora
10 openSUSE
11 Manjaro
12 elementary
13 MX Linux
14 Solus
15 Deepin
7)od命令
od代表八進制轉儲,它以八進制格式(二進制格式)為默認格式以不同格式顯示文件的內容。
od命令的通用語法:
od [Option] [File_Name]
如何在Linux上使用od命令,運行以下命令,會出來以下結果:
# od file-demo.txt
0000000 064124 071551 064440 020163 020141 062564 072163 063040
0000020 066151 020145 067564 062440 070170 071145 066551 067145
0000040 020164 064164 020145 043042 066151 020145 064526 073545
0000060 020042 067543 066555 067141 027144 043412 052516 046040
0000100 067151 074165 042012 061145 060551 005156 061125 067165
0000120 072564 040412 061562 020150 064514 072556 005170 064514
0000140 072556 020170 064515 072156 041412 067145 047564 005123
0000160 062522 020144 060510 005164 062506 067544 060562 067412
0000200 062560 051556 051525 005105 060515 065156 071141 005157
0000220 066145 066545 067145 060564 074562 046412 020130 064514
0000240 072556 005170 067523 072554 005163 062504 070145 067151
0000260 000012
0000261
8)xxd命令
xxd命令創建給定文件的十六進制轉儲,它還可以將十六進制轉儲轉換回其原始二進制形式。
xxd命令的通用語法:
xxd [Option] [File_Name]
如何在Linux上使用xxd命令,運行以下命令,會出來以下結果:
# xxd file-demo.txt
00000000: 5468 6973 2069 7320 6120 7465 7374 2066 ?This is a test f
00000010: 696c 6520 746f 2065 7870 6572 696d 656e ?ile to experimen
00000020: 7420 7468 6520 2246 696c 6520 5669 6577 ?t the "File View
00000030: 2220 636f 6d6d 616e 642e 0a47 4e55 204c ?" command..GNU L
00000040: 696e 7578 0a44 6562 6961 6e0a 5562 756e ?inux.Debian.Ubun
00000050: 7475 0a41 7263 6820 4c69 6e75 780a 4c69 ?tu.Arch Linux.Li
00000060: 6e75 7820 4d69 6e74 0a43 656e 744f 530a ?nux Mint.CentOS.
00000070: 5265 6420 4861 740a 4665 646f 7261 0a6f ?Red Hat.Fedora.o
00000080: 7065 6e53 5553 450a 4d61 6e6a 6172 6f0a ?penSUSE.Manjaro.
00000090: 656c 656d 656e 7461 7279 0a4d 5820 4c69 ?elementary.MX Li
000000a0: 6e75 780a 536f 6c75 730a 4465 6570 696e ?nux.Solus.Deepin
000000b0: 0a
9)gv命令
gv命令用于查看Postscript和PDF文件。
gv命令的通用語法:
gv [Option] [File_Name]
10)xdvi命令
xdvi命令是用于預覽dvi文件的程序,Xdvi可以顯示由于各種整數因素而縮小的文件,并且具有放大鏡,用于查看放大的頁面部分。
xdvi命令的通用語法:
xdvi [Option] [File_Name]
相關主題