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/

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

  1. linux中mysql客户端命令行连接不了 docker 创建的mysql

    linux中mysql客户端命令行连接不了 docker 创建的mysql 问题 :如题 a@z:~$ mysql -h localhost -p ERROR 2002 (HY000): Can't ...

  2. linux dmesg信息哪来的,linux中的dmesg命令简介

    今天, 我们来介绍一个linux中的dmesg命令,事实上, 我们之前用过, 但是没有单独介绍过. 看一下dmesg命令的用途吧: dmesg命令用会把开机信息存到ring bufer中, 形成一个缓 ...

  3. linux中ftp的用法,linux中的ftp命令用法(7页)-原创力文档

    第 第 PAGE 1 页 共 NUMPAGES 1 页 linux中的ftp命令用法 Linux系统中的ftp命令功能强大,那么它的具体用法是怎样呢?下面由小编为大家整理了linux中的ftp命令用法 ...

  4. 2021-05-10 linux中的find命令——查找文件名

    linux中的find命令--查找文件名 1.在某目录下查找名为"elm.cc"的文件 find /home/lijiajia/ -name elm.cc 2.查找文件名中包含某字 ...

  5. Linux中最常见命令总结

    Linux中最常见命令总结 基础命令 命令使用格式 命令名[选项参数] [操作对象]Ls -a workspace 目录命令 Ls 默认显示浏览当前文件目录 -a 显示所有文件,不忽略以点开头的文件 ...

  6. php umount强制,linux中mount/umount命令的基本用法及开机自动挂载方法

    本文介绍了linux中mount/umount命令的基本用法及开机自动挂载,具体方法如下: mount命令格式如下: 格式:mount [-参数] [设备名称] [挂载点] 其中常用的参数有: -a ...

  7. Linux中许多常用命令是必须掌握的,这里将我学linux入门时学的一些常用的基本命令分享给大家一下,希望可以帮助你们。...

    Linux中许多常用命令是必须掌握的,这里将我学linux入门时学的一些常用的基本命令分享给大家一下,希望可以帮助你们. 这个是我将鸟哥书上的进行了一下整理的,希望不要涉及到版权问题. 1.显示日期的 ...

  8. 如何在Linux中使用ulimit命令

    The ulimit command in Linux is an extremely useful command for system admins who manage multi-user s ...

  9. linux xargs命令_如何在Linux中使用xargs命令?

    linux xargs命令 The xargs command allows us to pass the output of one command as the input for another ...

  10. linux cut命令学习,Linux中的cut 命令详解

    今天小编要跟大家分享的文章是关于Linux中的cut 命令详解.cut 命令在Linux和Unix中的作用是从文件中的每一行中截取出一些部分,并输出到标准输出中.我们可以使用 cut 命令从一行字符串 ...

最新文章

  1. 插件化知识储备-Binder和AIDL原理
  2. Matlab 非线性规划问题模型代码
  3. python有趣代码-wtfPython―Python中一组有趣微妙的代码【收藏】
  4. hdu 1227(二维dp)
  5. mysql在线开启并行复制_mysql 5.7开启并行复制
  6. android 调用默认铃声后怎么改起始的默认选择位置?
  7. 阿联酋esma认证_阿联酋无人驾驶汽车预计2021年上路
  8. 贷款购房 房贷 每月还款额是如何计算出来的? 每月还款额计算步骤/方法
  9. 计算机图学测试题及答案,《计算机图形学》练习测试题及参考答案
  10. SpringCloud_004_SpringCloud服务发现组件原理介绍
  11. Java转C#的最佳工具
  12. 学python编程_程序员学Python编程或许不知的十大提升工具
  13. VS中添加新项 数据选项卡下没有ADO.NET实体数据模型解决方案
  14. 微信小程序中自定义模板
  15. codeblock添加tools实现git log查看
  16. Python-Django毕业设计小斌美食网站(程序+LW)
  17. 这就是iPhone 6的屏幕?
  18. jmeter——提取内容到文件
  19. 计算机网络的速率怎么计算,宽带速率对照表和计算方法
  20. 统计学中sp_统计学名词解释

热门文章

  1. Excel-一元线性回归和多元线性回归(借助数据分析功能和直接计算)
  2. 招聘中使用的奇葩心理分析
  3. C++, RAII, and the GSL Refresher
  4. PAT乙级题解——1093 字符串A+B (20分)
  5. Volley读取文档和图片
  6. tcp服务器响应超时,tcp客户端与服务器的连接超时
  7. [#32;] 在wordpress [the_excerpt()] 函数执行的妙用
  8. Android通过修改配置文件设置wifi密码
  9. dubbo SPI机制与@Adaptive自适应扩展机制
  10. android 获取快捷开关_6款快捷开关式实用安卓小插件推荐 简化Android设备操作