cat命令的用途是連接文件或標準輸入并打印。這個命令常用來顯示文件內容,或者將幾個文件連接起來顯示,或者從標準輸入讀取內容并顯示,它常與重定向符號配合使用。
cat [選項] [文件]…
cat主要有三大功能:
cat filenamecat > filename 只能創(chuàng)建新文件,不能編輯已有文件.cat file1 file2 > file-A, --show-all 等價于 -vET-b, --number-nonblank 對非空輸出行編號-e 等價于 -vE-E, --show-ends 在每行結束處顯示$-n, --number 對輸出的所有行編號,由1開始對所有輸出的行數(shù)編號-s, --squeeze-blank 有連續(xù)兩行以上的空白行,就代換為一行的空白行 -t 與 -vT 等價-T, --show-tabs 將跳格字符顯示為 ^I-u (被忽略)-v, --show-nonprinting 使用 ^ 和 M- 引用,除了 LFD 和 TAB 之外把 mylog1.log 的文件內容加上行號后輸入 mylog2.log 這個文件里。
命令:
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 的文件內容加上行號(空白行不加)之后將內容附加到 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來生成文件
[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]$