If you’re a keyboard person, you can accomplish a lot of things just using the Linux command line. For example, there are a few easy-to-use methods for creating text files, should you need to do so.

如果您是键盘手,则只需使用Linux命令行即可完成很多事情。 例如,如果需要,有几种易于使用的方法来创建文本文件。

使用Cat命令创建文本文件 (Create a Text File Using the Cat Command)

Our first method for creating text files uses the cat command. It’s useful if you want to immediately add some text to your new file.

创建文本文件的第一种方法是使用cat命令。 如果要立即将一些文本添加到新文件中,这很有用。

Just type the following command at the terminal prompt (replacing “sample.txt” with whatever you want to name your file), and then press Enter:

只需在终端提示符下键入以下命令(用您要命名的文件替换“ sample.txt”),然后按Enter:

cat > sample.txt

After pressing Enter, you are not returned to the terminal prompt. Instead, the cursor is placed on the next line, and you can start entering text directly into your file. Type your lines of text, pressing Enter after each line. When you are done, press Ctrl+D to exit the file and return to the prompt.

按Enter后,您将不会返回到终端提示符。 而是将光标放在下一行,您可以开始直接在文件中输入文本。 输入您的文本行,在每行之后按Enter。 完成后,按Ctrl + D退出文件并返回提示。

To verify your file was created, you can use the ls command to show a directory listing for the file:

要验证文件已创建,可以使用ls命令显示该文件的目录列表:

ls -l sample.txt

You can also use the cat command to view the contents of your file. Just type the following command at the prompt, and then press Enter:

您还可以使用cat命令查看文件的内容。 只需在提示符下键入以下命令,然后按Enter:

cat sample.txt

使用触摸命令创建文本文件 (Create a Text File Using the Touch Command)

You can also create a text file using the touch command. One difference between using this command and the cat command we covered in the last section is that, while the cat command lets you enter text into your file immediately, using the touch command does not. Another big difference is that the touch command lets you create multiple new files with a single command.

您也可以使用touch命令创建文本文件。 使用此命令和我们在上一节中介绍的cat命令之间的区别是,尽管cat命令使您可以立即在文件中输入文本,但使用touch命令则不能。 另一个很大的不同是, touch命令使您可以使用单个命令创建多个新文件。

The touch command is handy for quickly creating files you intend to use later.

touch命令对于快速创建以后要使用的文件非常有用。

To create a new file, type the following command at the terminal prompt (replacing “sample.txt” with whatever file name you want to use), and then press Enter:

要创建新文件,请在终端提示符下键入以下命令(将“ sample.txt”替换为要使用的任何文件名),然后按Enter:

touch sample.txt

Notice that you are given no indication that the file was created; you’re just returned to the prompt. You can use the ls command to verify the existence of your new file:

请注意,没有任何迹象表明您已创建该文件。 您只是返回到提示。 您可以使用ls命令来验证新文件的存在:

ls -l sample.txt

You can also create multiple new files at once with the touch command. Just add as many extra file names (separated by spaces) as you want to the end of the command:

您还可以使用touch命令一次创建多个新文件。 只要在命令末尾添加任意数量的额外文件名(用空格分隔)即可:

touch sample1.txt sample2.txt sample3.txt

触摸sample1.txt sample2.txt sample3.txt

Again, you’re shown no indication that the file was created, but issuing a simple ls command shows that the files are indeed there:

同样,您没有看到文件已创建的指示,但是发出一个简单的ls命令表明文件确实存在:

And when you’re ready to add text to your new files, you can just use a text editor like Vi.

当您准备向新文件中添加文本时,可以仅使用诸如Vi之类文本编辑器

使用标准重定向符号(>)创建文本文件 (Create a Text File Using the Standard Redirect Symbol (>))

You can also create a text file using the standard redirect symbol, which is usually used to redirect the output of a command to a new file. If you use it without a preceding command, the redirect symbol just creates a new file. Like the touch command, creating a file this way does not let you enter text into the file right away. Unlike the touch command, though, creating a file using the redirect symbol only lets you create one file at a time. We’re including it for completeness, and also because if you’re just creating a single file, it does offer the least typing.

您也可以使用标准重定向符号创建文本文件,该符号通常用于将命令输出重定向到新文件。 如果不使用前面的命令就使用它,则重定向符号只会创建一个新文件。 就像touch命令一样,以这种方式创建文件并不能使您立即在文件中输入文本。 但是,与touch命令不同,使用重定向符号创建文件只能一次创建一个文件。 我们之所以将其包括在内是出于完整性的考虑,并且还因为如果您仅创建一个文件,它的输入最少。

To create a new file, type the following command at the terminal prompt (replacing “sample.txt” with whatever file name you want to use), and then press Enter:

要创建新文件,请在终端提示符下键入以下命令(将“ sample.txt”替换为要使用的任何文件名),然后按Enter:

> sample.txt

You are given no indication that the file was created, but you can use the ls command to verify the existence of your new file:

您不会得到有关文件已创建的指示,但是可以使用ls命令来验证新文件的存在:

ls -l sample.txt


These three methods should allow you to quickly create text files at the Linux terminal, whether you need to enter text into them immediately or not.

无论您是否需要立即在其中输入文本,这三种方法都可以使您在Linux终端上快速创建文本文件。

翻译自: https://www.howtogeek.com/199687/how-to-quickly-create-a-text-file-using-the-command-line-in-linux/

GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e added ecosystem file for PM2 4 年前
5def40a3 Add host customization support for the NodeJS version 4 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐