linux cat 命令操作-显示,排序,建立

查看文件并且显示行号[-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

在非空格行首显示行号:[-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

显示tab制表符[-T]

当你想要显示文本中的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

显示换行符[-E]

-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$

同时显示制表符及换行符[-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$

分屏显示

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

# cat /proc/meminfo | less

# cat /proc/meminfo | more

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

同时查看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

排序显示[sort]

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

# cat /root/linux | sort

centos
mint
redhat
slackware
Ubuntu

输入重定向

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

# cat /root/linux > /root/linuxdistro

新建文件

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

# cat > operating_system

Unix
Linux
Windows
MacOS

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

向文件中追加内容

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

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

# cat /root/desktop

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

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

gnome
kde
xfce
enlightment
cinnamon
ubuntu
centos
redhat
mint
slackware

重定向输入

你可使用 <命令(小于号)将文件输入到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
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值