![linux基本命令示例](https://i-blog.csdnimg.cn/blog_migrate/e162eb9c99d17752fedd16f922f2c948.png)
linux基本命令示例
Concatenate or simply cat is very useful command in Linux and Unix systems. Cat has the ability to create files, concatenate files or redirect output. In this tutorial, we will look at different useful examples for daily usage for system administrators.
在Linux和Unix系统中,串联或简单地说cat是非常有用的命令。 Cat可以创建文件,连接文件或重定向输出。 在本教程中,我们将查看系统管理员日常使用的各种有用示例。
句法 (Syntax)
Syntax of Linux cat command is standard as most of the Linux tools.
Linux cat命令的语法是大多数Linux工具的标准配置。
cat [OPTION]... [FILE]...
人与帮助 (Man and Help)
Man page can get like below. As we can see cat
command provides a lot of features.
手册页如下所示。 如我们所见, cat
命令提供了很多功能。
$ man cat
![Man and Help](https://i-blog.csdnimg.cn/blog_migrate/7c8590f345e4d099da1336512f94991d.png)
打印内容(Print Content)
The most popular way to use cat command is printing contents of a file. cat
command will read given file line by line and print to the terminal or shell accordingly. In this example, we will print file named myshell.sh
which is a bash script.
使用cat命令的最流行方法是打印文件内容。 cat
命令将逐行读取给定的文件,并相应地打印到终端或外壳程序。 在此示例中,我们将打印名为myshell.sh
文件,它是一个bash脚本。
$ cat myshell.sh
![Print Content](https://i-blog.csdnimg.cn/blog_migrate/f78adfe0edb07354fd57f28327babc7a.png)
打印多个文件的内容(Print Content of Multiple Files)
cat
command accepts multiple parameters like the file name to print all contents of provided files. In this example, we will print file contents named myshell.sh
and myscan
in a row.
cat
命令接受多个参数(例如文件名)以打印所提供文件的所有内容。 在这个例子中,我们将打印文件内容命名为myshell.sh
和myscan
成一排。
$ cat myshell.sh myscan
![](https://i-blog.csdnimg.cn/blog_migrate/14a94214ee367650b479e221eaf97540.png)
建立档案 (Create File)
An empty file will be created when cat output is directed to the file name like below. After redirect control character should be sent to close the file.
当cat输出指向以下文件名时,将创建一个空文件。 重定向后,应发送控制字符以关闭文件。
$ cat > new_file
And then
接着
Ctrl+d
![Create File](https://i-blog.csdnimg.cn/blog_migrate/f00301f5fe2a72e623e2e86d12e88fc6.png)
与越来越多的命令一起使用(Use With More and Less Commands)
While working more than one text file with more or less commands cat provides a practical solution to pipe all files contents.
当使用或多或少的命令处理多个文本文件时,cat提供了一种实用的解决方案来管道所有文件的内容。
$ cat myshell.sh myscan | less
显示行号 (Display Line Numbers)
While printing file contents with cat the line numbers can be shown to direct line specification. If we need specific line numbers it will be very helpful especially using with grep
command. We will use -n
as an option to enable line number printing.
用cat打印文件内容时,可以显示行号以直接指定行。 如果我们需要特定的行号,这将非常有用,尤其是与grep
命令一起使用时。 我们将使用-n
作为启用行号打印的选项。
$ cat -n myshell.sh
![Display Line Numbers](https://i-blog.csdnimg.cn/blog_migrate/f36dd9219a422ccd9e65cf5b9010ec47.png)
将$显示为行尾(Display $ as End Of Line)
If we want to put delimiter at the end of each line we can use -e
option. This will put $
to the end of each line.
如果要将定界符放在每行的末尾,可以使用-e
选项。 这会将$
放在每一行的末尾。
$ cat -e myshell.sh
![Display $ End Of Line](https://i-blog.csdnimg.cn/blog_migrate/035589d391c5c9b5310dbfe0f75ce637.png)
显示选项卡分隔线(Display Tab Separated Lines)
There is also an option to separate tabbed lines with -T
parameter like below. ^|
will be used as a tab sign to show as characters.
还有一个选项可使用-T
参数分隔选项卡式行,如下所示。 ^|
将用作标签显示为字符。
$ cat -T myshell.sh
![Display Tab Separated Lines](https://i-blog.csdnimg.cn/blog_migrate/850aedbc9f824af9ce4f04d78b13928f.png)
与猫重定向(Redirect With Cat)
The redirect will be used to provide standard output content to the next command standard input. Cat prints file content and following file will get content of the previous file.
重定向将用于向下一个命令标准输入提供标准输出内容。 Cat打印文件内容,后续文件将获取前一个文件的内容。
$ cat myshell.sh > yourshell.sh
Keep in mind this operation will overwrite all yourshell.sh content with new content
请记住,此操作将用新内容覆盖所有yourshell.sh内容
追加到猫的文件 (Append To File With Cat)
While redirecting outputs previous content will be overwritten. to prevent overwrite and add new content to the existing content >> operator can be used.
重定向输出时,先前的内容将被覆盖。 为防止覆盖并将新内容添加到现有内容>>运算符,可以使用。
$ cat myshell.sh >> yourshell.sh
![Append To File With Cat](https://i-blog.csdnimg.cn/blog_migrate/7fd746de30d0a792fb6b1ece5111a16b.png)
重定向标准输入(Redirect Standard Input)
Another useful usage of the cat command is redirecting standard input. Generally, the content of a file is the standard input.
cat命令的另一个有用用法是重定向标准输入。 通常,文件的内容是标准输入。
$ cat < myshell.sh
![Redirect Standard Input](https://i-blog.csdnimg.cn/blog_migrate/93e0191e1318fdf3f84af8645e6dc220.png)
将多个文件合并为一个文件(Concatenate Multiple File Into One File)
One of the funniest usage of the cat command is adding multiple files into a single file with a single shot.
cat命令最有趣的用法之一是一次将多个文件添加到单个文件中。
$ cat myshell.sh yourshell.sh > ourshell.sh
![Concatenate Multiple File Into One File](https://i-blog.csdnimg.cn/blog_migrate/69fee93f198de822cc607f0e9eba00b3.png)
与排序一起使用(Use With Sort)
Using cat with sort command is another useful scenario. All content of files are piped to the sort and sort will sort all content accordingly.
将cat与sort命令一起使用是另一种有用的方案。 文件的所有内容都通过管道传递到排序,排序将对所有内容进行相应的排序。
$ cat myshell.sh yourshell.sh | sort
![Use With Sort](https://i-blog.csdnimg.cn/blog_migrate/9dd092574628b41c5ed2720f8affe690.png)
linux基本命令示例