linux cat 命令

本文介绍了cat命令的基本用法,包括如何连接或显示文件内容,以及如何使用各种选项进行行号显示、非空白行显示、空白行压缩显示等操作。通过示例演示了命令的应用场景,包括显示文件内容、指定行号显示、合并文件内容等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

功能

连接文件或显示文件的内容

语法

cat [选项] 文件

选项

-n,--number 显示行号
-b,--number-nonblank 不显示空白行的行号
-s,--squeeze-blank 连续两个或以上的空白行替换成一个来显示

示例

cat a.txt显示文件的内容

lychie@ubuntu:/test$ cat a.txt

You laugh at me for being different,

but I laugh at you for being the same.

Don't go around saying the world owes you a living.

The world owes you nothing. It was here first.

cat -n a.txt显示文件的内容

lychie@ubuntu:/test$ cat -n a.txt
     1
     2  You laugh at me for being different,
     3
     4  but I laugh at you for being the same.
     5
     6  Don't go around saying the world owes you a living.
     7
     8  The world owes you nothing. It was here first.

cat -b a.txt显示文件的内容

lychie@ubuntu:/test$ cat -b a.txt

     1  You laugh at me for being different,

     2  but I laugh at you for being the same.

     3  Don't go around saying the world owes you a living.

     4  The world owes you nothing. It was here first.

cat > b.txt << OFF生成文件

lychie@ubuntu:/test$ cat > b.txt << OFF
> ------ END ------
> OFF
lychie@ubuntu:/test$

cat a.txt b.txt > c.txt合并 a.txt、b.txt 的内容并存储到 c.txt 中

lychie@ubuntu:/test$ ls
a.txt  b.txt
lychie@ubuntu:/test$ cat a.txt b.txt > c.txt
lychie@ubuntu:/test$ ls
a.txt  b.txt  c.txt
lychie@ubuntu:/test$ cat -b c.txt

     1  You laugh at me for being different,

     2  but I laugh at you for being the same.

     3  Don't go around saying the world owes you a living.

     4  The world owes you nothing. It was here first.

     5  ------ END ------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值