linux中bash命令
Cat in Linux stands for concatenation (to merge things together) and is one of the most useful and versatile Linux commands. While not exactly as cute and cuddly as a real cat, the Linux cat
command can be used to support a number of operations utilizing strings, files, and output.
Linux中的Cat代表串联(将事物合并在一起),是最有用,用途最广泛的Linux命令之一。 虽然不像真正的猫咪那样可爱和可爱,但是Linux cat
命令可用于支持许多利用字符串,文件和输出的操作。
The cat command has three primary purposes involving text files:
cat命令具有涉及文本文件的三个主要目的:
We'll go over each of these in turn to show the commands and options associated with each operation.
我们将依次检查每一个,以显示与每个操作关联的命令和选项。
To start out, let's create a couple of files called foo.txt and spam.txt.
首先,让我们创建几个名为foo.txt和spam.txt的文件。
Let's start by creating foo.txt with the command cat > foo.txt
from the Linux command line.
首先从Linux命令行使用cat > foo.txt
命令创建foo.txt。
Warning: If there is already a file named foo.txt, the cat
command using the > operator WILL overwrite it.
警告:如果已经有一个名为foo.txt的文件,则使用>运算符的cat
命令将覆盖它。
From here the prompt will display a newline that will allow us to input the text we want. For this example we'll use:
从这里,提示符将显示换行符,使我们可以输入所需的文本。 在此示例中,我们将使用:
FILE 1
foo
bar
baz
To get back to the command line and create the text file we use CTRL + D.
要返回命令行并创建文本文件,我们使用CTRL +D。
Now let's create spam.txt with cat > spam.txt
and put in the following:
现在,让我们使用cat > spam.txt
创建spam.txt并输入以下内容:
FILE 2
spam
ham
eggs
If we wanted to append or add more text to these files we would use cat >> FILENAME
and input the text we want to use.
如果我们想向这些文件添加或添加更多文本,我们将使用cat >> FILENAME
并输入我们要使用的文本。
Note that the >> operator is used for appending as opposed to the > operator.
请注意,>>运算符用于附加,而不是>运算符。
Instead of having to open a text editor, we were able to create a quick and simple text file from the command line, saving us time and effort.
不必打开文本编辑器,我们可以从命令行创建一个快速简单的文本文件,从而节省了我们的时间和精力。
The key takeaway from this section is that we use cat > FILENAME
to create or overwrite a file. Additionally, we can use cat >> FILENAME
to append to a file that's already there. Then after typing in the text we want we use CTRL + D to exit the editor, return to the command line, and create the file.
本节的主要内容是,我们使用cat > FILENAME
创建或覆盖文件。 此外,我们可以使用cat >> FILENAME
附加到已经存在的文件中。 然后,在输入文本后,我们希望使用CTRL + D退出编辑器,返回命令行,并创建文件。
Now that we've created something let's take a look at what we've made.
现在我们已经创建了一些东西,让我们来看看我们所做的。
Notice how we don't have a > or a >> operator in the following command, only cat and the filename.
请注意,在以下命令中我们如何没有>或>>运算符,只有cat和文件名。
The command cat foo.txt
will display the following:
命令cat foo.txt
将显示以下内容:
FILE 1
foo
bar
baz
So cat foo.txt
will let us read the file, but let's see what else we can do.
所以cat foo.txt
将让我们读取文件,但让我们看看我们还能做什么。
Say we wanted to figure out how many lines a file we were working on was. For this the -n option comes in handy.
假设我们想弄清楚我们正在处理的文件有多少行。 为此,-n选项非常有用。
With the command cat -n foo.txt
we can see how long our file is:
使用命令cat -n foo.txt
我们可以看到文件有多长时间:
1 FILE 1
2 foo
3 bar
4 baz
With -n we can get an idea of how many lines the file we're working with has. This can come in handy when we're iterating over a file of a fixed length.
使用-n,我们可以了解正在处理的文件有多少行。 当我们遍历固定长度的文件时,这可能会派上用场。
Ok, so we've seen that cat can create and display files, but what about concatenating (combining) them?
好的,所以我们已经看到cat可以创建和显示文件,但是如何将它们串联(合并)呢?
For this example we'll use files foo.txt and spam.txt. If we want to get fancy we can take a look at the contents of both files at the same time. We'll use the cat command again, this time using cat foo.txt spam.txt
.
在此示例中,我们将使用文件foo.txt和spam.txt。 如果我们想花哨的话,我们可以同时查看两个文件的内容。 我们将再次使用cat命令,这次使用cat foo.txt spam.txt
。
cat foo.txt spam.txt
results in the following:
cat foo.txt spam.txt
结果如下:
FILE 1
foo
bar
baz
FILE 2
spam
ham
eggs
Note that the above only DISPLAYS the two files. At this point we haven't concatenated them into a new file yet.
请注意,以上仅显示两个文件。 此时,我们还没有将它们串联到一个新文件中。
To concatenate the files into a new file we want to use cat foo.txt spam.txt > fooSpam.txt
which gives us the result into a new file fooSpam.txt.
要将文件串联成一个新文件,我们要使用cat foo.txt spam.txt > fooSpam.txt
,这cat foo.txt spam.txt > fooSpam.txt
结果提供给一个新文件fooSpam.txt。
Using cat fooSpam.txt
outputs the following to the terminal:
使用cat fooSpam.txt
将以下内容输出到终端:
FILE 1
foo
bar
baz
FILE 2
spam
ham
eggs
This command is also useful for when we want to concatenate more than two files into a new file.
当我们要将两个以上的文件连接成一个新文件时,此命令也很有用。
The takeaways here are we can view multiple files with cat FILENAME1 FILENAME 2
.
这里的要点是,我们可以使用cat FILENAME1 FILENAME 2
查看多个文件。
Furthermore we can concatenate multiple files into one file with the command cat FILENAME1 FILENAME 2 > FILENAME3
.
此外,我们可以使用cat FILENAME1 FILENAME 2 > FILENAME3
命令将多个文件合并为一个文件。
Let's say we're working with a file and we keep getting errors for some reason before the end of the file – and it looks like it might have more lines than we expected it to.
假设我们正在处理文件,并且由于某种原因而在文件结束之前不断出现错误-看起来它的行数可能比我们预期的要多。
To investigate the file a bit further and possibly solve our problem we can use the -A switch. The -A option will show us where lines end with a $, it will show us tab characters with a ^I, and it also displays other non-printing characters.
为了进一步研究该文件并可能解决我们的问题,我们可以使用-A开关。 -A选项将向我们显示行以$结尾的位置,它将向我们显示带有^ I的制表符,并且还将显示其他非打印字符。
If we were looking at an example of a non-printable text file with cat nonPrintExample.txt
we might get out something like this:
如果我们使用cat nonPrintExample.txt
查看一个不可打印的文本文件的示例,我们可能会得到如下信息:
Which is OK but may not tell us a full story of a character or string that might be causing us issues.
可以,但是可能无法告诉我们有关可能导致问题的字符或字符串的完整故事。
Whereas cat -A nonPrintExample.txt
might give us more useful output:
cat -A nonPrintExample.txt
可能会为我们提供更多有用的输出:
^I^I$
$
^L$
$
^G^H^H^H^Y^I^N^O^P^@$
^@^@^[g^[f^[d^[g^[6^[5^[4^[6^[=$
$
$
^X$
Here, we get a clearer representation of what might be going on between tabs, line feeds, returns, and other characters.
在这里,我们可以更清楚地表示制表符,换行符,返回符和其他字符之间可能发生的情况。
The takeaway here is that cat -A FILENAME can tell us more in-depth details about the file that we're working with.
这里的要点是cat -A FILENAME可以告诉我们有关正在使用的文件的更多详细信息。
This article should give you a good overview of the cat command, what it can do, and its functionality.
本文应该为您很好地概述cat命令,它可以做什么及其功能。
linux中bash命令