温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

cat命令怎么在Linux系统中使用

发布时间:2021-03-12 16:37:11 来源:亿速云 阅读:147 作者:Leah 栏目:系统运维

cat命令怎么在Linux系统中使用?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

cat命令的主要作用是显示一个文件的内容,常与重定向符号>搭配使用.

主要使用方法
cat FILE 在Terminal中显示文件的所有内容.
cat > FILE 直接从Terminal中输入文件内容并创建文件.只能用于创建,不能修改已有文件.
cat FILE1 FILE2 > FILE 将多个文件内容合并到一个文件里.

参数介绍
-v, --show-nonprinting 除了LFD和TAB之外,使用^和M-标注.
-T, --show-tabs 将TAB显示为^I.
-E, --show-ends 每行的末尾显示$.
-A, --show-all 等同于-vET.
-n, --number 对所有行标行号输出.
-b, --numbernonblank 对于不为空的行标行号显示,会覆盖-n.
-e 等同于-vE.
-s, --squeeze-blank 多个空行只显示为一个空行.
-t 等同于-vT.
--help 显示帮助信息.
--version 显示版本信息.

技巧

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命令怎么在Linux系统中使用的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI