cat命令的用途是連接文件或標(biāo)準(zhǔn)輸入并打印。這個(gè)命令常用來(lái)顯示文件內(nèi)容,或者將幾個(gè)文件連接起來(lái)顯示,或者從標(biāo)準(zhǔn)輸入讀取內(nèi)容并顯示,它常與重定向符號(hào)配合使用。
cat [選項(xiàng)] [文件]…
cat主要有三大功能:
cat filenamecat > filename 只能創(chuàng)建新文件,不能編輯已有文件.cat file1 file2 > file-A, --show-all 等價(jià)于 -vET-b, --number-nonblank 對(duì)非空輸出行編號(hào)-e 等價(jià)于 -vE-E, --show-ends 在每行結(jié)束處顯示$-n, --number 對(duì)輸出的所有行編號(hào),由1開(kāi)始對(duì)所有輸出的行數(shù)編號(hào)-s, --squeeze-blank 有連續(xù)兩行以上的空白行,就代換為一行的空白行 -t 與 -vT 等價(jià)-T, --show-tabs 將跳格字符顯示為 ^I-u (被忽略)-v, --show-nonprinting 使用 ^ 和 M- 引用,除了 LFD 和 TAB 之外把 mylog1.log 的文件內(nèi)容加上行號(hào)后輸入 mylog2.log 這個(gè)文件里。
命令:
cat -n mylog1.log mylog2.log
輸出:
[yiibai@localhost test]$ cat mylog1.log
this is line 1
this is line 2
[yiibai@localhost test]$ cat mylog2.log
log2 this is line 1
log2 this is line 2
log2 this is line 3
[yiibai@localhost test]$ cat -n mylog1.log mylog2.log
1 this is line 1
2 this is line 2
3 log2 this is line 1
4 log2 this is line 2
5 log2 this is line 3
[yiibai@localhost test]$
把 mylog1.log 和 mylog2.log 的文件內(nèi)容加上行號(hào)(空白行不加)之后將內(nèi)容附加到 log.log 里。
命令:
cat -b mylog1.log mylog2.log log.log
輸出:
yiibai@localhost test]$ cat -b mylog1.log mylog2.log >> log.log
[yiibai@localhost test]$ cat log.log
1 this is line 1
2 this is line 2
3 log2 this is line 1
4 log2 this is line 2
5 log2 this is line 3
[yiibai@localhost test]$
使用here doc來(lái)生成文件
[yiibai@localhost test]$ cat >log.txt <<EOF
> Hello
> World
> Linux command
> PWD=$(pwd)
> EOF
[yiibai@localhost test]$ ls -l log.txt
-rw-rw-r--. 1 yiibai yiibai 49 Feb 13 03:20 log.txt
[yiibai@localhost test]$ cat log.txt
Hello
World
Linux command
PWD=/home/yiibai/test
[yiibai@localhost test]$