linux基本命令示例_带有示例Linux Cat命令

linux基本命令示例

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
Man and Help
人与帮助

打印内容(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
Print Content
打印内容

打印多个文件的内容(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.shmyscan成一排。

$ cat myshell.sh myscan

建立档案 (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
Create File
建立档案

与越来越多的命令一起使用(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
Display Line Numbers
显示行号

将$显示为行尾(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
Display $ End Of Line
显示$行尾

显示选项卡分隔线(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
Display Tab Separated Lines
显示选项卡分隔线

与猫重定向(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内容

LEARN MORE  How to Deal With Dashed File Names Like Open, Remove?
了解更多信息如何处理像打开,删除这样的虚线文件名?

追加到猫的文件 (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
Append To File With Cat
追加到猫的文件

重定向标准输入(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
Redirect Standard Input
重定向标准输入

将多个文件合并为一个文件(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
Concatenate Multiple File Into One File
将多个文件串联成一个文件

与排序一起使用(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
Use With Sort
与排序一起使用

翻译自: https://www.poftut.com/linux-cat-command-examples/

linux基本命令示例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值