在LINUX中查看文件

时间:2019-04-29 03:17:26  来源:igfitidea点击:

在命令行中查看文件

如何使用shell来查看文件

在以下示例中,我们将发现有很多方法可以从终端中查看文件。

cat命令

cat命令可用于查看单个文件并将其输出发送到Standard Out(stdout)。只需输入 cat filename,通常就可以查看文件的内容。但是, cat命令还有更多内容。cat这个名字来源于 concatenation 连接一词,意为对象或文件的连接。

如果我们输入 cat file1 file2,这两个文件将依次显示在屏幕上。如果要将此命令与一些重定向结合在一起,则可以创建包含file1和file2的第三个文件。为此,我们键入 cat file1 file2> file3

cat也可以查看带有特殊字符的文件。您甚至可以在行中添加数字,将大量空白行压缩为一行。以下是 cat命令的一些示例。

cat 文件名

john@john-desktop:~/test_examples$ cat textfile.txt
Linux Mint
Mageia
Ubuntu
Fedora
openSUSE
Debian
Arch Linux
PCLinuxOS
Zorin OS
CentOS

cat命令-对所有行进行编号

行号被添加到每行的开头。

john@john-desktop:~/test_examples$ cat -n textfile.txt
     1	Linux Mint
     2	Mageia
     3	Ubuntu
     4	Fedora
     5	openSUSE
     6	Debian
     7	Arch Linux
     8	PCLinuxOS
     9	Zorin OS
    10	CentOS

cat 多个文件

john@john-desktop:~/test_examples$ cat file1 file2
Line 1 of file1
Line 2 of file1
Line 3 of file1
Line 1 of file2
Line 2 of file2
Line 3 of file2

显示行尾标记($)

john@john-desktop:~/test_examples$ cat -E file1
Line 1 of file1$
Line 2 of file1$
Line 3 of file1$

显示不可以打印字符-tab标记(^l)

john@john-desktop:~/test_examples$ cat file3
Line 1 of file 3        Directly before me is a Tab
Line 2 of file 3        Directly before me is a Tab
Line 3 of file 3        Directly before me is a Tab

john@john-desktop:~/test_examples$ cat -T file3
Line 1 of file 3^IDirectly before me is a Tab
Line 2 of file 3^IDirectly before me is a Tab
Line 3 of file 3^IDirectly before me is a Tab

显示不可打印的字符-选项卡显示为^ I,行尾显示为$

john@john-desktop:~/test_examples$ cat -vet file3
Line 1 of file 3^IDirectly before me is a Tab$
Line 2 of file 3^IDirectly before me is a Tab$
Line 3 of file 3^IDirectly before me is a Tab$

以倒序方式显示文件

john@john-desktop:~/test_examples$ tac file1
Line 3 of file1
Line 2 of file1
Line 1 of file1

more 命令

有时,您可能需要将cat与大文件一起使用。如果您键入cat,然后输入这个大文件,则输出可能会在屏幕上刷屏。为了解决这个问题,我们可以将cat与 more命令结合使用。为此,我们只需将命令修改为: cat file1 | more。现在,输出一次显示一页。如果按下键盘上的 空格键,则将显示下一个信息屏幕。您可以在more命令上指定 -d选项:cat largefile.txt | more -d。通过添加 -d选项,然后提示我们 -more-[按空格继续,'q'退出。]

john@john-desktop:~/test_examples$ more file1
Line 1 of file1
Line 2 of file1
Line 3 of file1

john@john-desktop:~/test_examples$ cat file1 |more
Line 1 of file1
Line 2 of file1
Line 3 of file1

less 命令

less是一个类似于 more程序的程序。主要区别在于 less程序将允许您在文件中来回移动。像大多数分页程序一样, less可以接收管道命令的输出,也可以直接使用该命令打开文件。显示文件的最后一行后,可以按 q退出:

john@john-desktop:~/test_examples$ less file1
Line 1 of file1
Line 2 of file1
Line 3 of file1
file1 (END)

pg 命令

pg是另一个程序,它允许您查看文件的内容。信息一次显示在一个屏幕上。但是,可以直接在给定页面上打开文件。为此,只需在 **+**号后面传递页码。

john@john-desktop:~/test_examples$ pg +2 file1

Line 3 of file1

(EOF):

head命令和tail命令

可用于显示文件信息行的两个极其有用的程序是 headtail。基本上, head将显示文件开头的信息,而 tail将显示文件结尾的信息。

使这两个程序真正有用的原因是能够指定输出中包含多少行。默认情况下, headtail程序分别显示前10行或后10行:

john@john-desktop:~/test_examples$ head concatfile2.txt
     1	Linux Mint
     2	Mageia
     3	Ubuntu
     4	Fedora
     5	openSUSE
     6	Debian
     7	Arch Linux
     8	PCLinuxOS
     9	Zorin OS
    10	CentOS
john@john-desktop:~/test_examples$ tail concatfile2.txt
    21	Linux Mint
    22	Mageia
    23	Ubuntu
    24	Fedora
    25	openSUSE
    26	Debian
    27	Arch Linux
    28	PCLinuxOS
    29	Zorin OS
    30	CentOS

我们可以对显示的行数使用 -n标志。在下面的示例中,先查看文件的前5行,然后查看文件的最后几行:

john@john-desktop:~/test_examples$ head -n 5 concatfile2.txt
     1	Linux Mint
     2	Mageia
     3	Ubuntu
     4	Fedora
     5	openSUSE
john@john-desktop:~/test_examples$ tail -n 5 concatfile2.txt
    26	Debian
    27	Arch Linux
    28	PCLinuxOS
    29	Zorin OS
    30	CentOS

如果您只想查看文件中间的内容,这些命令真正有用的地方。在我们的示例中,我们只希望看到第11行到第20行的输出。要实现这一点,我们需要结合使用 head命令和 tail命令:

john@john-desktop:~/test_examples$ tail -n 20 concatfile2.txt | head -n 10
    11	Linux Mint
    12	Mageia
    13	Ubuntu
    14	Fedora
    15	openSUSE
    16	Debian
    17	Arch Linux
    18	PCLinuxOS
    19	Zorin OS
    20	CentOS