Cat (串联) 命令是Linux/Unix开源系统中比较常用的一个命令。我们可以通过Cat命令创建一个或多个文件,查看文件内容,串联文件并将内容输出到终端设备或新的文件当中,这篇文章我们将会以实例的方式讲解Linux中cat命令一些简便的用法。

The cat (short for “concatenate“) command is one of the most frequently used command in Linux/Unix like operating systems. cat command allows us to create single or multiple files, view contain of file, concatenate files and redirect output in terminal or files. In this article, we are going to find out handy use of cat commands with their examples in Linux.

Read Also: Learn How to use ‘cat’ and ‘tac’ (Reverse of cat Command) in Linux

13中基本的Linux Cat 命令

13 Basic Linux Cat Commands

一般语法格式

General Syntax

cat [OPTION] [FILE]...

1. Display Contents of File(显示文件内容)

In the below example, it will show contents of /etc/passwd file.

下面这个例子,显示出/etc/passwd文件的内容。

# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
narad:x:500:500::/home/narad:/bin/bash

2. View Contents of Multiple Files in terminal(在设备中显示多个文件内容)

In below example, it will display contents of test and test1 file in terminal.

下面这个例子,在设备中显示出test文件和test1文件内容

# cat test test1
Hello everybody
Hi world,

3. Create a File with Cat Command(使用Cat命令创建文件)

We will create a file called test2 file with below command.

我们将通过如下命令格式创建test2文件

# cat >test2

Awaits input from user, type desired text and press CTRL+D (hold down Ctrl Key and type ‘d‘) to exit. The text will be written in test2 file. You can see content of file with following cat command.

输入上面的命令后点击回车按钮,等待用户输入文件内容,同时按住CTRL+D退出编辑。通过下面的命令查看文件内容。

# cat test2
hello everyone, how do you do?

4. Use Cat Command with More & Less Options(使用带有More和Less参数的Cat命令)

If file having large number of content that won’t fit in output terminal and screen scrolls up very fast, we can use parameters more and less with cat command as show above.

如果文件有大量内容,设备输出不够清晰或则屏幕滚动条滚动过快,我们可以通过使用More和Less参数使内容展示更加可控。

# cat song.txt | more
# cat song.txt | less

5. Display Line Numbers in File(文件内显示内容行号)

With -n option you could see the line numbers of a file song.txt in the output terminal.

通过 -n 参数可以使song.txt文件的内容在设备中带行号显示

# cat -n song.txt
1 "Heal The World"
2 There's A Place In
3 Your Heart
4 And I Know That It Is Love
5 And This Place Could
6 Be Much
7 Brighter Than Tomorrow
8 And If You Really Try
9 You'll Find There's No Need
10 To Cry
11 In This Place You'll Feel
12 There's No Hurt Or Sorrow

6. Display $ at the End of File(在文件末尾显示$符号)

In the below, you can see with -e option that ‘$‘ is shows at the end of line and also in space showing ‘$‘ if there is any gap between paragraphs. This options is useful to squeeze multiple lines in a single line.

如下,使用-e参数,在文件内容的末尾和空格处会出现$符号。这个选项对于一行中挤压多行很有帮助。

# cat -e test
hello everyone, how do you do?$
$
Hey, am fine.$
How's your training going on?$
$

7. Display Tab separated Lines in File(文件内容中显示Tab分隔符)

In the below output, we could see TAB space is filled up with ‘^I‘ character.

如下输出内容中,我们可以看到Tab分隔的内容使用 ‘^I‘ 填充。

# cat -T test
hello ^Ieveryone, how do you do?
Hey, ^Iam fine.
^I^IHow's your training ^Igoing on?
Let's do ^Isome practice in Linux.

8. Display Multiple Files at Once(一次性显示多个文件内容)

In the below example we have three files test, test1 and test2 and able to view the contents of those file as shown above. We need to separate each file with ; (semi colon).

这个例子中,我们可以同时查看test, test1, test2三个文件的内容,三个文件之间使用 ;(分号)隔开。

# cat test; cat test1; cat test2
This is test file
This is test1 file.
This is test2 file.

9. Use Standard Output with Redirection Operator(使用重定向操作符作为标准输出)

We can redirect standard output of a file into a new file else existing file with ‘>‘ (greater than) symbol. Careful, existing contents of test1 will be overwritten by contents of test file.

我们可以使用大于号>将文件内容复制到新的文件或已存在的文件中,注意:复制到已存在的文件时会覆盖已有内容。

# cat test > test1

10. Appending Standard Output with Redirection Operator(使用重定向操作符将追加内容)

Appends in existing file with ‘>>‘ (double greater than) symbol. Here, contents of test file will be appended at the end of test1 file.

使用两个大于号>>将新的内容追加到已有内容的末尾。

# cat test >> test1

11. Redirecting Standard Input with Redirection Operator(使用重定向操作符将内容输出)

When you use the redirect with standard input ‘<‘ (less than symbol), it use file name test2 as a input for a command and output will be shown in a terminal.

我们可以使用小于号<,将test2文件的内容显示到设备中。

# cat < test2
This is test2 file.

12. Redirecting Multiple Files Contain in a Single File(将多个文件内容合并到一个文件中)

This will create a file called test3 and all output will be redirected in a newly created file.

将test, test1, test2的内容合并到test3文件中

# cat test test1 test2 > test3

13. Sorting Contents of Multiple Files in a Single File(将多个文件的内容合并到一个文件中并排序)

This will create a file test4 and output of cat command is piped to sort and result will be redirected in a newly created file.

# cat test test3 test2 test1 | sort > test4
例如: test文件内容是:this is test file content.
test3文件内容: this is test3 file content.
test2文件内容: this is test2 file content.
test1文件内容:this is test1 file content.
最后输出的test4文件内容:

this is test1 file content.
this is test2 file content.
this is test3 file content.
this is test file content.

13 Basic Cat Command Examples in Linux(转) Linux中cat命令的13中基本用法的更多相关文章

  1. 13 Basic Cat Command Examples in Linux

    FROM: http://www.tecmint.com/13-basic-cat-command-examples-in-linux/ The cat (short for “concatenate ...

  2. 15 Basic ‘ls’ Command Examples in Linux

    FROM: http://www.tecmint.com/15-basic-ls-command-examples-in-linux/ ls command is one of the most fr ...

  3. 00014 - linux中用top、ps命令查看进程中的线程

    在Linux上显示某个进程的线程的几种方式. 方法一:PS 在ps命令中,“-T”选项可以开启线程查看.下面的命令列出了由进程号为<pid>的进程创建的所有线程. 1.$ ps -T -p ...

  4. Linux bash脚本及常用命令--不断更新中

    1.如何在向alias命令传递参数: 这种用法的话就需要使用函数来配合使用. 如要cd到指定目录,并且ls当前目录下的文件可以使用:  alias cdls='cdls(){ cd $1; ls; } ...

  5. Linux 查看磁盘空间 相关命令

    Linux 查看磁盘空间 相关命令 实际工作中,我们经常需要查看磁盘空间的使用情况,以防止磁盘空间不足,导致的系统崩溃或者服务异常等问题. 常用的磁盘空间查看命令如下: 1.查看磁盘空间的整体使用情况 ...

  6. 15 Linux Split and Join Command Examples to Manage Large Files--reference

    by HIMANSHU ARORA on OCTOBER 16, 2012 http://www.thegeekstuff.com/2012/10/15-linux-split-and-join-co ...

  7. 18 Tar Command Examples in Linux

    FROM: http://www.tecmint.com/18-tar-command-examples-in-linux/ 18 Tar Command Examples in Linux By R ...

  8. 12 Linux Which Command, Whatis Command, Whereis Command Examples

    This Linux tutorial will explain the three "W" commands. The three "W"s are what ...

  9. 【Linux高频命令专题(13)】cat

    概述 常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用. cat主要有三大功能: 1.一次显示整个文件:cat filename 2.从键盘创建一 ...

随机推荐

  1. org.springframework.expression.spel.SpelEvaluationException: EL1004E: Method call: Method service() cannot be found on com.my.blog.springboot.thymeleaf.util.MethodTest type

    前言 本文中提到的解决方案,源码地址在:springboot-thymeleaf,希望可以帮你解决问题. 至于为什么已经写了一篇文章thymeleaf模板引擎调用java类中的方法,又多此一举的单独整 ...

  2. mongodb数据库安装

    mongodb的安装 1,下载安装包: http://www.runoob.com/mongodb/mongodb-window-install.html 2,安装至:    D:\MongoDB,将 ...

  3. Activiti 5.22.0 之自由驳回任务实现(亲测)

    ​ 上篇博文,我们完成一个任务SKIP的实现,说好要给各位看官带来驳回实现的现在,就奉上具体实现和讲解.(其实我感觉我的注释写的已经非常清楚了,哈哈) ​ 依旧是,先说我们的需求和思路. PS: ​ ...

  4. 一步一步从原理跟我学邮件收取及发送 2.邮箱的登录和绕不开的base64

    一步一步从原理跟我学邮件收取及发送 2.邮箱的登录和绕不开的base64 好了,经过本系列上一篇文章 "1.网络命令的发送",假设大家已经掌握了 email 电子邮件的命令发送的方 ...

  5. Linux 下非root用户使用docker

    Linux 下非root用户使用docker 通常我们使用linux系统的时候,最好是不要直接使用root账号,但是使用Docker的时候,默认又是不能使用非root用户的,关于原因,官方说法如下: ...

  6. CTF---Web入门第六题 因缺思汀的绕过

    因缺思汀的绕过分值:20 来源: pcat 难度:中 参与人数:6479人 Get Flag:2002人 答题人数:2197人 解题通过率:91% 访问解题链接去访问题目,可以进行答题.根据web题一 ...

  7. bzoj:1692 [Usaco2007 Dec]队列变换&&1640 [Usaco2007 Nov]Best Cow Line 队列变换

    Description FJ打算带他的N(1 <= N <= 30,000)头奶牛去参加一年一度的“全美农场主大奖赛”.在这场比赛中,每个参赛者都必须让他的奶牛排成一列,然后领她们从裁判席 ...

  8. Codeforces 834E The Bakery【枚举+数位dp】

    E. Ever-Hungry Krakozyabra time limit per test:1 second memory limit per test:256 megabytes input:st ...

  9. JS 监听浏览器各个标签间的切换

    以前看到过一些网页,在标签切换到其它地址时,网页上的标题上会发生变化,一直不知道这个是怎么做的,最近查了一些资料才发现有一个 visibilitychange 事件就可以搞定,这里将介绍一下页面可见性 ...

  10. OpenStack Horizon创建虚拟机时增加虚拟机OS用户

    背景 通过OpenStack的Horizon使用镜像创建虚拟机(以Ubuntu为例),如果不知道镜像的用户名和密码,在创建好虚拟机之后,无法登录虚拟机的OS.因此,我们需要一种方法,创建虚拟机时增加用 ...