linux cat 命令_Linux cat命令示例

linux cat 命令

One of the most commonly used commands in Linux is the cat command. Short for concatenate, Linux cat command allows users to perform the following operations.

cat命令是Linux中最常用的命令之一。 Linux cat命令是连接的缩写,它允许用户执行以下操作。

  • View contents of a file

    查看文件内容
  • Redirect output of files

    重定向文件输出
  • Create files and input content

    创建文件和输入内容

In this tutorial, we take a look at cat command with a few examples of how it can be used.

在本教程中,我们看一下cat命令以及一些如何使用它的示例。

Syntax:

语法

$ cat [option] [file]

1.显示文件内容 (1. Display content of a file)

To view a file’s content, use the syntax.

要查看文件的内容,请使用语法。

$ cat file-name

For example to view the file /etc/passwd run:

例如,查看文件/etc/passwd运行:

$ cat /etc/passwd

Sample output

样品输出

2.查看多个文件 (2. Viewing Multiple Files)

In addition to viewing a single file, you can view multiple files in one command using the syntax as shown below.

除了查看单个文件之外,您还可以使用如下所示的语法在一个命令中查看多个文件。

$ cat file1 file2

For example, we have 2 files with the following content.

例如,我们有2个文件,其内容如下。

To view the files simultaneously, run:

要同时查看文件,请运行:

$ cat file1.txt file2.txt

Sample output

样品输出

3.编号文件中的内容行 (3. Number the lines of content in a file)

If you want to number the lines of content in a file, use the -n as shown below.

如果要对文件中的内容行进行编号,请使用-n ,如下所示。

$ cat -n filename

For example,

例如,

$ cat -n /etc/passwd

Sample Output

样本输出

4.使用cat命令或多或少的选项 (4. Use cat command with less or more options)

The more or less parameters enable a user to easily scroll down long content that scrolls down very fast, making it difficult to follow . The synatx is quite simple.

moreless参数,使用户能够很容易地向下滚动长的内容,向下滚动速度非常快,因此很难跟随。 synatx非常简单。

$ cat filename | less

OR

要么

$ cat filename | more

5.使用cat命令创建文件 (5. Create files using cat command)

To create a file, use the > sign as shown in the syntax below.

要创建文件,请使用>符号,如以下语法所示。

$ cat > filename

Next, type your desired text input and at the very end, press CTRL + D to exit.

接下来,键入所需的文本输入,最后,按CTRL + D退出。

The text will be written onto the text file. You can verify this using the cat command as seen in the first example.

文本将被写入文本文件。 您可以使用cat命令来验证这一点,如第一个示例所示。

6.一次查看多个文件 (6. View multiple files at once)

Assume you have several files that you’d want to view at a go, use the colon mark ; to separate the files being displayed as shown.

假设您要一次查看几个文件,请使用冒号; 分开显示的文件,如图所示。

cat file1.txt; cat file2.txt; cat file3.txt; cat file4.txt ...

7.将标准输出附加到另一个文件 (7. Appending standard output to another file)

You can append content onto a file using the double greater sign redirection operator >> as shown.

您可以使用双倍大符号重定向运算符>>将内容附加到文件上,如图所示。

cat file1 >> file2

This appends the contents of file 1 onto file 2.

这会将文件1的内容附加到文件2上。

Sample output

样品输出

8.使用重定向运算符将文件的输出重定向到另一个文件 (8. Redirect output of a file onto another file using the redirection operator)

You can redirect standard output of a file into a new file with ‘>‘ (greater than) symbol.

您可以将文件的标准输出重定向到带有'>'(大于)符号的新文件中。

cat file1 > file2

As seen in the output above, extra caution should be taken when doing this because this operation overwrites the contents of the destination file.

如上面的输出所示,执行此操作时应格外小心,因为此操作将覆盖目标文件的内容。

To redirect multiple outputs into a single file use the syntax

要将多个输出重定向到单个文件中,请使用以下语法

$ cat file1 file2 file3 file4 > file5

结论 (Conclusion)

Linux cat command is a simple utility to view file contents, create a text file with some content, and append data to a text file.

Linux cat命令是一个简单的实用程序,用于查看文件内容,创建包含某些内容的文本文件以及将数据追加到文本文件。

翻译自: https://www.journaldev.com/30345/linux-cat-command-examples

linux cat 命令

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值