1.命令功能
cat 合並文件或者查看文件內容。
2.語法格式
cat option file
參數說明
參數 |
參數說明 |
-n |
打印文本,並顯示每行行號並且空白行也同樣包括 |
-b |
與-n用法類似,不同之處,-b不顯示空白行的行號,忽略空白行號 |
-s |
當遇到連續2行以上空白行時,重置成一行 |
-E |
在每行結尾處加上$符號 |
-e |
等價於-vE |
cat命令分為三類:
- 合並多個文件並標准輸出
- 文件追加到另一個文件中
- 創建文件以及寫入文件內容的用法 語法:cat > filename <<EOF
3.使用范例
范例1 顯示文件內容
[root@localhost home]# cat passwd_test
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologi
cxf:x:500:500::/home/cxf:/bin/bash
范例2 顯示行號 cat -n
[root@localhost home]# cat -n passwd_test
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3
4 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
5
6
7
8 vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
9 saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
10
11 postfix:x:89:89::/var/spool/postfix:/sbin/nologi
12 cxf:x:500:500::/home/cxf:/bin/bash
范例3 空白行不顯示行號
[root@localhost home]# cat -b passwd_test
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
4 vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
5 saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
6 postfix:x:89:89::/var/spool/postfix:/sbin/nologi
7 cxf:x:500:500::/home/cxf:/bin/bash
范例4 連續多行空白,重置成一行空白
[root@localhost home]# cat -sn passwd_test
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3
4 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
5
6 vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
7 saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
8
9 postfix:x:89:89::/var/spool/postfix:/sbin/nologi
10 cxf:x:500:500::/home/cxf:/bin/bash
范例5 cat 合並多個文件
[root@localhost test]# cat 1.txt
123 123
123 123
[root@localhost test]# cat 2.txt
234 234
234 234
[root@localhost test]# cat 1.txt 2.txt
123 123
123 123
234 234
234 234
范例6 cat 追加(當追加文件不存在時,創建文件)
[root@localhost test]# cat 1.txt 2.txt > 3.txt
[root@localhost test]# cat 3.txt
123 123
123 123
234 234
234 234
范例7 cat不覆蓋追加
[root@localhost test]# cat 1.txt 2.txt >> 3.txt
[root@localhost test]# cat 3.txt
123 123
123 123
234 234
234 234
123 123
123 123
234 234
234 234
范例8 cat 寫入文件
[root@localhost test]# cat > test.txt << EOF
> HELLO WORLD
> WELCOME TO LINUX
> EOF
[root@localhost test]# cat test.txt
HELLO WORLD
WELCOME TO LINUX
范例 9 cat追加文件,不覆蓋
[root@localhost test]# cat >> test.txt << EOF
> OK OK OK
> EOF
[root@localhost test]# cat test.txt
HELLO WORLD
WELCOME TO LINUX
OK OK OK