在Linux系统中,大多数配置文件、日志文件,甚至shell脚本都使用文本文件格式,因此,Linux系统存在着多种文本编辑器,但当你仅仅想要查看一下这些文件的内容时,可使用一个简单的命令-cat。

cat手册里这样描述:

cat命令读取文件内容,并输出到标准设备上面。

cat是一条linux内置命令. 几乎所有linux发行版都内置(译注:或者说我从未听说过不内置cat命令的发行版)。接下来,让我们开始学习如何使用。

1. 显示文件内容

最简单的方法是直接输入‘cat file_name’.

# cat /etc/issue

CentOS release 5.10 (Final)
Kernel \r on an \m

2. 同时显示行号

当在读取内容很多的配置文件时,如果同时显示行号将会使操作变简单,加上-n参数可以实现.

# cat -n /etc/ntp.conf

1 # Permit time synchronization our time resource but do not
2 # permit the source to query or modify the service on this system
3 restrict default kod nomodify notrap nopeer noquery
4 restrict -6 default kod nomodify notrap nopeer noquery
5
6 # Permit all access over the loopback interface. This could be
7 # tightened as well, but to do so would effect some of the
8 # administration functions
9 restrict 127.0.0.1
10 restrict -6 ::1

3. 在非空格行首显示行号

类似于-n参数,-b也可以显示行号。区别在于-b只在非空行前显示行号。

#cat -b /etc/ntp.conf

1 # Permit time synchronization our time resource but do not
2 # permit the source to query or modify the service on this system
3 restrict default kod nomodify notrap nopeer noquery
4 restrict -6 default kod nomodify notrap nopeer noquery 5 # Permit all access over the loopback interface. This could be
6 # tightened as well, but to do so would effect some of the
7 # administration functions
8 restrict 127.0.0.1
9 restrict -6 ::1

4. 显示tab制表符

当你想要显示文本中的tab制表位时. 可使用-T参数. 它会在输入结果中标识为 ^I .

# cat -T /etc/hosts

# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1^I^Ilocalhost.localdomain localhost
::1^I^Ilocalhost6.localdomain6 localhost6

5. 显示换行符

-E参数在每行结尾使用 $ 表示换行符。如下所示 :

# cat -E /etc/hosts

# Do not remove the following line, or various programs$
# that require network functionality will fail.$
127.0.0.1 localhost.localdomain localhost$
::1 localhost6.localdomain6 localhost6$

6. 同时显示制表符及换行符

当你想要同时达到-T及-E的效果, 可使用-A参数.

# cat -A /etc/hosts

# Do not remove the following line, or various programs$
# that require network functionality will fail.$
127.0.0.1^I^Ilocalhost.localdomain localhost$
::1^I^Ilocalhost6.localdomain6 localhost6$

7. 分屏显示

当文件内容显示超过了你的屏幕大小, 可结合cat命令与其它命令分屏显示。使用管道符 ( | )来连接。

# cat /proc/meminfo | less

# cat /proc/meminfo | more

在less与more显示结果的区别在于less参数可pageup及pagedown上下翻滚。而more仅能使用空格向下翻屏。

8. 同时查看2个文件中的内容

位于/root文件夹里有两个文件取名linux及desktop,每个文件含有以下内容 :

Linux : ubuntu, centos, redhat, mint and slackware

Desktop : gnome kde, xfce, enlightment, and cinnamon

当你想同时查看两文件中的内容时,可按如下方法 :

# cat /root/linux /root/desktop

ubuntu
centos
redhat
mint
slackware
gnome
kde
xfce
enlightment
cinnamon

9. 排序显示

类似. 你也可以结合cat命令与其它命令来进行自定义输出. 如结合 sort ,通过管道符对内容进行排序显示。举例 :

# cat /root/linux | sort

centos
mint
redhat
slackware
Ubuntu

10. 输入重定向

你也可将显示结果输出重定向到屏幕或另一个文件。 只需要使用 > 符号(大于号)即可输出生成到另一个文件。

# cat /root/linux > /root/linuxdistro

以上命令会生成一个与/root/linux内容一模一样的叫linuxdistro的文件.

11. 新建文件

Linux下有多种方法新建文件。其中使用cat就是方法之一.

# cat > operating_system

Unix
Linux
Windows
MacOS

当你输入cat > operatingsystem,它会生成一个operatingsystem的文件。然后下面会显示空行。此时你可输入内容。比如我们输入Unix, Linux, Windows 和 MacOS, 输入完成后,按Ctrl-D存盘退出cat。此时你会发现当前文件夹下会生成一个包含你刚才输入内容的叫operating_system的文件。

12.向文件中追加内容

当你使用两个 > 符时, 会将第一个文件中的内容追加到第二个文件的末尾。 举例 :

# cat /root/linux >> /root/desktop

# cat /root/desktop

它会将 /root/linux的内容追加到/root/desktop文件的末尾。

第二个文件的内容将会变成这样:

gnome
kde
xfce
enlightment
cinnamon
ubuntu
centos
redhat
mint
slackware

13. 重定向输入

你可使用 <命令(小于号)将文件输入到cat中.

# cat < /root/linux

上面命令表示 /root/linux中的内容作为cat的输入。屏幕上显示如下 :

ubuntu
centos
redhat
mint
slackware

为了更清楚表示它的意义,我们使用以下命令 :

# cat < /root/linux | sort > linux-sort

此命令这样理解: 从/root/linux中读取内容,然后排序,将结果输出并生成linux-sort新文件

然后我们看看linux-sort中的内容 :

centos
mint
redhat
slackware
ubuntu

以上是一些cat命令的日常基本应用. 更多相关你可从cat命令手册中学到并记得经常练习它们.

13个Cat命令管理文件实例汇总的更多相关文章

  1. 13个Cat命令管理(显示,排序,建立)文件实例

    在Linux系统中,大多数配置文件.日志文件,甚至shell脚本都使用文本文件格式,因此,Linux系统存在着多种文本编辑器,但当你仅仅想要查看一下这些文件的内容时,可使用一个简单的命令-cat. c ...

  2. 菜鸟学Linux命令:cat命令 查看文件内容

    cat命令的用途是连接文件或标准输入并打印. 这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用. Linux下查看文件内容的方式很多:vi ...

  3. 探索Windows命令行系列(4):通过命令管理文件和文件夹

    1.文件夹操作 1.1.DIR(directory)命令 1.2.TREE 命令 1.3.CD(change directory)命令 1.4.MD(make directory)命令 1.5.RD( ...

  4. Centos文件切割利器_split命令及cat命令合并文件

    有个文件要处理,因为很大,所以想把它切成若干份,每份N行,以便并行处理.split命令可以将一个大文件分割成很多个小文件,有时需要将文件分割成更小的片段,为提高可读性,生成日志等 命令格式 -b:值为 ...

  5. C# 执行Cmd窗口中的命令 [复制文件实例]

    /// <summary> /// 复制文件夹 /// </summary> /// <param name="sCmd"></param ...

  6. cat命令创建文件

    看例子是最快的熟悉方法: # cat << EOF > test.sh > #!/bin/bash #“shell脚本” > #you Shell script writ ...

  7. cat命令在文件中插入内容

    eg: cat>> xxx <<EOFinsert 1insert 2 EOF

  8. 使用cat命令清空文件

    比如要清空 /www/aaa.txt cat /dev/null > /www/aaa.txt; 即可.

  9. 13 Basic Cat Command Examples in Linux(转) Linux中cat命令的13中基本用法

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

随机推荐

  1. [HDOJ4612]Warm up(双连通分量,缩点,树直径)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4612 所有图论题都要往树上考虑 题意:给一张图,仅允许添加一条边,问能干掉的最多条桥有多少. 必须解决 ...

  2. mkdir -p

    git bash 或 mac terminal 我们可以使用 mkdir 命令来创建文件夹. 当前目录创建多个文件夹: $ mkdir a b c 会创建 a .b.c 三个文件夹 但是有时候我们需要 ...

  3. 对象不支持“attachEvent”属性或方法的解决办法

    有些脚本在IE11下执行会报错误: 对象不支持“attachEvent”属性或方法 解决办法 解决办法:把attachEvent改为addEventListener即可

  4. HDU 1397 Goldbach's Conjecture【素数打表】

    题意:给出n,问满足a+b=n且a,b都为素数的有多少对 将素数打表,再枚举 #include<iostream> #include<cstdio> #include<c ...

  5. (任寒韬)WebApp群主 - MobileTech 资料

    web app : http://www.lightapp.cn/brand/index/4101 https://github.com/jtyjty99999/mobileTech/blob/mas ...

  6. Web Api 接口文档制作

    参考地址: http://blogs.msdn.com/b/yaohuang1/archive/2012/05/21/asp-net-web-api-generating-a-web-api-help ...

  7. 我是红领巾,分享2014 google不能用的方法。

    那啥已经20天打不开了. 得爬qiang. 今天无意间发现一个好东东. 特记录一下.    360浏览器设置 1.   工具菜单==>选项==>高级设置==>管理搜索引擎 . 2. ...

  8. chrome 远程调试(转)

    http://www.tuicool.com/articles/ZJfeAzi 由于 appspot.com被墙,一般调试不成功. 随着智能手机的普及,移动设备的浏览器功能越来越强大,我们用手机上网时 ...

  9. Nosql释义

    NoSQL不是产品,是一项运动        ---->NoSQL(NoSQL = Not Only SQL ),意即反SQL运动,是一项全新的数据库革命性运动,早期就有人提出,发展至2009年 ...

  10. MySQL基础之第5章 操作数据库

    假设已经登录 mysql-h localhost -uroot -proot 5.1.显示.创建.删除数据库 show databases;     显示所有的数据库 create database ...