Linux中的Cat命令–如何使用Cat或Touch创建文本文件

The cat command is a very popular and versatile command in the 'nix ecosystem.  There are 4 common usages of the cat command. It can display a file, concatenate (combine) multiple files, echo text, and it can be used to create a new file.

cat命令是'nix生态系统中非常流行且用途广泛的命令。 cat命令有4种常见用法。 它可以显示一个文件,连接(合并)多个文件,回显文本,并且可以用来创建一个新文件。

显示文件 (Displaying a file)

The most common use of the cat command is to output the contents of a file. The following is an example that you can try.

cat命令最常见的用法是输出文件的内容。 以下是您可以尝试的示例。

echo "Dance, Dance" > cat_create #create a file
cat cat_create

In this simple example, we're using a combination of echo and a redirect to create a file containing "Dance, Dance". We then use the cat command to display the contents.

在这个简单的示例中,我们结合使用echo和重定向来创建一个包含“ Dance,Dance”的文件。 然后,我们使用cat命令显示内容。

The output is as follows:

输出如下:

(骗子)猫 ((Con)cat)

The previous example is actually a specific case of the cat command's main function, which is to concatenate files for display.  If we use the command the same way, but give it two or more files, then it outputs the concatenation for the files.

前面的示例实际上是cat命令主要功能的特定情况,该功能是连接文件以供显示。 如果我们以相同的方式使用命令,但是给它两个或更多文件,则它将输出文件的串联。

If we run the following commands:

如果我们运行以下命令:

echo "This is how we do it" > test1 #create 1st file
echo "*This is how we do it*" > test2 #create 2nd file
cat test1 test2

The output is the contents of the 1st file, followed by the contents of the 2nd file. You can give cat many files and it will concatenate (combine) all of them. Notice however, that the cat command automatically inserts a line break between outputs.

输出是第一个文件的内容,然后是第二个文件的内容。 您可以给cat提供许多文件,它将串联(合并)所有文件。 但是请注意,cat命令自动在输出之间插入换行符。

cat also provides some switches to to do things such as show non-print characters (-v), or number your lines (-n). A complete breakdown can be found in the man pages.

cat还提供了一些开关来执行操作,例如显示非打印字符(-v)或对行进行编号(-n)。 完整的故障分类可以在手册页中找到。

回声 (Echoing)

This is a less common usage of cat , but is the basis for the next section. If you run the cat command with no commands, cat will run in interactive mode and echo anything you type until you exit the command.

这是cat的较不常用用法,但是下一部分的基础。 如果不使用任何命令运行cat命令, cat将以交互模式运行并回显您键入的任何内容,直到退出该命令。

In the example here, I've typed a single word per line. Each time I hit enter, the line was echoed.

在这里的示例中,我每行输入一个单词。 每次我按回车键时,都会回荡一行。

You can also pipe text to cat, in which case that text is echoed. For example:

您还可以将文本通过管道传递给cat ,在这种情况下,将回显文本。 例如:

echo "Piping fun" | cat

This will result in the following output:

这将导致以下输出:

创建一个文件 (Creating a File)

In the previous examples, we've been using the echo command redirected to a file to create new files. Cat can be used in a similar way. In fact, we can use cat's concat and echo functionality to create files.

在前面的示例中,我们一直使用echo命令重定向到文件来创建新文件。 猫可以类似的方式使用。 实际上,我们可以使用cat的concat和echo功能创建文件。

We can create a file containing the concatenation of multiple files like this:

我们可以创建一个包含多个文件的串联文件,如下所示:

echo "File 1 Contents" > file1
echo "File 2 Contents" > file2
echo "File 3 Contents" > file3
cat file1 file2 file3 > combined_file
cat combined_file

In the above example, we're creating 3 files using echo, combining the 3 files into one using cat, and then displaying the new combined file using cat.

在上面的例子中,我们创建了使用3档echo ,使用3个文件合并成一个cat ,然后使用显示合并后的新文件cat

We can also use cat's interactive mode to create a file with the text that we type into the terminal.

我们还可以使用cat的交互模式来创建一个包含我们输入到终端中的文本的文件。

Each time you hit enter, it commits the text to the file. If you have uncommitted text and exit, it won't be captured in the file.

每次您按下Enter键,它都会将文本提交到文件中。 如果您有未提交的文本并退出,则不会在文件中捕获它。

This is a fantastic way to create a file quickly with the ability to enter the content of the file.

这是一种使用输入文件内容的能力快速创建文件的绝佳方法。

改用Touch创建文件 (Using Touch to create a file instead)

Sometimes you just need a file to exist. As an alternative to using cat to create a file, you can use the touch command.

有时您只需要一个文件即可。 作为使用cat创建文件的替代方法,可以使用touch命令。

The touch command was designed to update the modified timestamp of a file, but is commonly used as a quick way to create an empty file. Here is an example of that usage:

touch命令旨在更新文件的修改时间戳,但通常用作创建空文件的快速方法。 这是该用法的一个示例:

touch new_file_name

The touch command can create multiple files, update the modification and/or creation timestamps, and a bunch of other useful things. The full man pages can be found here.

touch命令可以创建多个文件,更新修改和/或创建时间戳,以及许多其他有用的东西。 完整的手册页可以在这里找到。

Touch is commonly used to ensure that a file exists, and is a great command if you need an empty file quickly.

触摸常用于确保文件存在,如果您需要快速清空文件,则触摸是一个很好的命令。

摘要 (Summary)

Cat is a very useful command.  You can use it to create, display, and combine text files very quickly and easily.  

猫是一个非常有用的命令。 您可以使用它来快速,轻松地创建,显示和合并文本文件。

If you only need a file to exist, but don't mind (or require) it being empty, using touch is a great alternative.

如果您只需要一个文件存在,但不介意(或要求它)为空,则使用touch是一个很好的选择。

Hughie Coles is a lead developer at Index Exchange. He writes about software architecture, scaling, leadership, and culture.  For more of his writing, check out his blog on medium.

Hughie Coles是Index Exchange的首席开发人员。 他撰写了有关软件体系结构,可伸缩性,领导能力和文化的文章。 有关他的更多作品,请查看他的博客。

翻译自: https://www.freecodecamp.org/news/the-cat-command-in-linux-how-to-create-a-text-file-with-cat-or-touch/

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值