Linux cat命令

参考资料

  1. Linux 常用命令 - cat 【快速显示文件内容】

目录

  • [零. 文件准备](#零. 文件准备)
  • [一. 用法简介](#一. 用法简介)
  • [二. 文件合并](#二. 文件合并)
  • [三. 创建文件](#三. 创建文件)
  • [四. 清空文件](#四. 清空文件)
  • [五. `-T`显示制表符](#五. -T显示制表符)
  • [六. tac反向显示文件内容](#六. tac反向显示文件内容)

零. 文件准备

bash 复制代码
apluser@ubuntu24-01:~/work/20250326$ cat file1.txt
dav.jianguoyun.com
GetNetworkCredential
apluser@ubuntu24-01:~/work/20250326$ cat file2.txt
Password
UserName
apluser@ubuntu24-01:~/work/20250326$ cat file3.txt
username
password

一. 用法简介

⏹ cat命令主要用于显示文件内容

  • -n, --number:为所有行输出行号
  • -T, --show-tabs:将制表符(TAB)显示为 ^I
bash 复制代码
cat [选项...] [文件...]

二. 文件合并

  • -n:用于显示行号
  • 用于文件合并的时候,按照文件名默认排序的方式获取内容并合并
bash 复制代码
apluser@ubuntu24-01:~/work/20250326$ cat -n file1.txt file2.txt file3.txt
     1  dav.jianguoyun.com
     2  GetNetworkCredential
     3  Password
     4  UserName
     5  username
     6  password
apluser@ubuntu24-01:~/work/20250326$
apluser@ubuntu24-01:~/work/20250326$ cat -n file{1..3}.txt
     1  dav.jianguoyun.com
     2  GetNetworkCredential
     3  Password
     4  UserName
     5  username
     6  password
apluser@ubuntu24-01:~/work/20250326$ cat -n file*.txt
     1  dav.jianguoyun.com
     2  GetNetworkCredential
     3  Password
     4  UserName
     5  username
     6  password
apluser@ubuntu24-01:~/work/20250326$
  • 也可以通过下面这种方式进行倒序排序之后再合并
bash 复制代码
apluser@ubuntu24-01:~/work/20250326$ cat $(ls -r file*)
username
password
Password
UserName
dav.jianguoyun.com
GetNetworkCredential
apluser@ubuntu24-01:~/work/20250326$ find . -maxdepth 1 -type f -name "file*" | sort -r | xargs cat
username
password
Password
UserName
dav.jianguoyun.com
GetNetworkCredential
apluser@ubuntu24-01:~/work/20250326$
  • 将标准输入与另外一个文件中的数据组合在一起
bash 复制代码
apluser@ubuntu24-01:~/work/20250326$ cat <(echo "hello world") file1.txt
hello world
dav.jianguoyun.com
GetNetworkCredential
apluser@ubuntu24-01:~/work/20250326$ cat <(echo -e "hello world\n你好 世界") file1.txt
hello world
你好 世界
dav.jianguoyun.com
GetNetworkCredential
apluser@ubuntu24-01:~/work/20250326$ echo -e "hello world\n你好 世界" | cat file1.txt -
dav.jianguoyun.com
GetNetworkCredential
hello world
你好 世界

三. 创建文件

  • 通过 cat > 文件名 的方式来新建文件
  • 输入命令后,然后回车,光标会停留在终端,等待用户输入
  • 输入或者粘贴完内容之后,最后一行若想要保留的话,最后一行一定要敲一个回车
  • 最后按下键盘的 Ctrl + C 即可完成文件的保存
  • ^C便是按下Ctrl + C后的印记
  • 🧐可以通过这种方式来创建.sh脚本文件
bash 复制代码
apluser@ubuntu24-01:~/work/20250326$ cat > info.txt
lsof命令

        查看文件夹被哪个进程占用
^C
apluser@ubuntu24-01:~/work/20250326$ cat info.txt
lsof命令

        查看文件夹被哪个进程占用
apluser@ubuntu24-01:~/work/20250326$

四. 清空文件

  • /dev/null 是 Linux 和 Unix 系统中的一个特殊设备文件,俗称黑洞
  • 作用是 丢弃所有写入它的数据,并且读取时返回空内容
bash 复制代码
cat /dev/null > file.txt

五. -T显示制表符

  • -T:用于将制表符TAB表示为^I
bash 复制代码
apluser@ubuntu24-01:~/work/20250326$ cat info.txt
lsof命令

        查看文件夹被哪个进程占用
apluser@ubuntu24-01:~/work/20250326$ cat -T info.txt
lsof命令

^I查看文件夹被哪个进程占用
apluser@ubuntu24-01:~/work/20250326$ cat --show-tabs info.txt
lsof命令

^I查看文件夹被哪个进程占用
apluser@ubuntu24-01:~/work/20250326$

六. tac反向显示文件内容

bash 复制代码
apluser@ubuntu24-01:~/work/20250326$ cat file1.txt
dav.jianguoyun.com
GetNetworkCredential
apluser@ubuntu24-01:~/work/20250326$ tac file1.txt
GetNetworkCredential
dav.jianguoyun.com
apluser@ubuntu24-01:~/work/20250326$
相关推荐
深圳市九鼎创展科技1 小时前
瑞芯微 RK3399 开发板 X3399 评测:高性能 ARM 平台的多面手
linux·arm开发·人工智能·单片机·嵌入式硬件·边缘计算
全栈工程师修炼指南1 小时前
Nginx | stream content 阶段:TCP 协议四层反向代理浅析与实践
运维·网络·网络协议·tcp/ip·nginx
hweiyu001 小时前
Linux 命令:tr
linux·运维·服务器
Trouvaille ~2 小时前
【Linux】应用层协议设计实战(一):自定义协议与网络计算器
linux·运维·服务器·网络·c++·http·应用层协议
allway22 小时前
基于华为taishan200服务器、arm架构kunpeng920 cpu的虚拟化实战
linux·运维·服务器
CSCN新手听安2 小时前
【linux】高级IO,I/O多路转接之poll,接口和原理讲解,poll版本的TCP服务器
linux·运维·服务器·c++·计算机网络·高级io·poll
熊文豪2 小时前
服务器炸了才知道?Ward+cpolar让异常无处藏
运维·服务器·cpolar
杜子不疼.2 小时前
【Linux】教你在 Linux 上搭建 Web 服务器,步骤清晰无门槛
linux·服务器·前端
荔枝吻2 小时前
忘记服务器密码,在Xshell7中查看已保存密码
运维·服务器·github
码农阿豪2 小时前
多服务器批量指令管理:从Xshell到自动化运维
运维·服务器·自动化