cat命令的用途是连接文件或标准输入并打印。这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用。
1.命令格式:
cat [选项] [文件]...
2.命令功能:
cat主要有三大功能:
1.一次显示整个文件:cat /etc/fstab
2.从键盘创建一个文件:cat > filename 只能创建新文件,不能编辑已有文件
3.将几个文件合并为一个文件:cat file1 file2 > file
3.命令参数:
-E, --show-ends 在每行结束处显示 $
-n, --number 对输出的所有行编号,由1开始对所有输出的行数编号
4.使用实例:
实例一:把 fstab 的文件内容显示到终端上
命令:
cat /etc/fstab
输出:
[BEGIN] 2015/12/27 1:03:29 [[email protected] ~]# [[email protected] ~]# cat /etc/fstab # # /etc/fstab # Created by anaconda on Thu Dec 3 08:33:34 2015 # # Accessible filesystems, by reference, are maintained under ‘/dev/disk‘ # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=8a9c5d3a-e525-4fa2-bcfe-90ced11a0b08 / ext4 defaults 1 1 UUID=0167fd91-560f-4baf-bb20-b54d429faf9d /boot ext4 defaults 1 2 UUID=7b81c9d0-9139-4fcb-8700-0173c7ba0f60 swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 [END] 2015/12/27 1:03:39
实例二:对显示出的每一行进行编号
命令:
cat -n /etc/fstab
输出:
[BEGIN] 2015/12/27 1:01:55 [[email protected] ~]# cat /etc/fstab # # /etc/fstab # Created by anaconda on Thu Dec 3 08:33:34 2015 # # Accessible filesystems, by reference, are maintained under ‘/dev/disk‘ # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=8a9c5d3a-e525-4fa2-bcfe-90ced11a0b08 / ext4 defaults 1 1 UUID=0167fd91-560f-4baf-bb20-b54d429faf9d /boot ext4 defaults 1 2 UUID=7b81c9d0-9139-4fcb-8700-0173c7ba0f60 swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 [[email protected] ~]# cat -n /etc/fstab 1 2 # 3 # /etc/fstab 4 # Created by anaconda on Thu Dec 3 08:33:34 2015 5 # 6 # Accessible filesystems, by reference, are maintained under ‘/dev/disk‘ 7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info 8 # 9 UUID=8a9c5d3a-e525-4fa2-bcfe-90ced11a0b08 / ext4 defaults 1 1 10 UUID=0167fd91-560f-4baf-bb20-b54d429faf9d /boot ext4 defaults 1 2 11 UUID=7b81c9d0-9139-4fcb-8700-0173c7ba0f60 swap swap defaults 0 0 12 tmpfs /dev/shm tmpfs defaults 0 0 13 devpts /dev/pts devpts gid=5,mode=620 0 0 14 sysfs /sys sysfs defaults 0 0 15 proc /proc proc defaults 0 0 [END] 2015/12/27 1:02:15
实例三:-E显示行后面加上结束符$
命令:
cat -E /etc/fstab
输出:
[BEGIN] 2015/12/27 1:10:19 [[email protected] ~]# [[email protected] ~]# cat -E /etc/fstab $ #$ # /etc/fstab$ # Created by anaconda on Thu Dec 3 08:33:34 2015$ #$ # Accessible filesystems, by reference, are maintained under ‘/dev/disk‘$ # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info$ #$ UUID=8a9c5d3a-e525-4fa2-bcfe-90ced11a0b08 / ext4 defaults 1 1$ UUID=0167fd91-560f-4baf-bb20-b54d429faf9d /boot ext4 defaults 1 2$ UUID=7b81c9d0-9139-4fcb-8700-0173c7ba0f60 swap swap defaults 0 0$ tmpfs /dev/shm tmpfs defaults 0 0$ devpts /dev/pts devpts gid=5,mode=620 0 0$ sysfs /sys sysfs defaults 0 0$ proc /proc proc defaults 0 0$ [END] 2015/12/27 1:10:31
备注:
tac (反向列示)
命令:
tac /etc/fstab
输出:
[BEGIN] 2015/12/27 1:12:25 [[email protected] ~]# cat /etc/fstab # # /etc/fstab # Created by anaconda on Thu Dec 3 08:33:34 2015 # # Accessible filesystems, by reference, are maintained under ‘/dev/disk‘ # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=8a9c5d3a-e525-4fa2-bcfe-90ced11a0b08 / ext4 defaults 1 1 UUID=0167fd91-560f-4baf-bb20-b54d429faf9d /boot ext4 defaults 1 2 UUID=7b81c9d0-9139-4fcb-8700-0173c7ba0f60 swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 [[email protected] ~]# tac /etc/fstab proc /proc proc defaults 0 0 sysfs /sys sysfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 tmpfs /dev/shm tmpfs defaults 0 0 UUID=7b81c9d0-9139-4fcb-8700-0173c7ba0f60 swap swap defaults 0 0 UUID=0167fd91-560f-4baf-bb20-b54d429faf9d /boot ext4 defaults 1 2 UUID=8a9c5d3a-e525-4fa2-bcfe-90ced11a0b08 / ext4 defaults 1 1 # # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # Accessible filesystems, by reference, are maintained under ‘/dev/disk‘ # # Created by anaconda on Thu Dec 3 08:33:34 2015 # /etc/fstab # [END] 2015/12/27 1:12:40
说明:tac 是将 cat 反写过来,所以他的功能就跟 cat 相反, cat 是由第一行到最后一行连续显示在终端上,而 tac 则是由最后一行到第一行反向在终端上显示出来!
时间: 2024-11-05 02:17:12