今天在學(xué)習(xí)部署安裝openstack的時候,看到一個關(guān)于cat的奇怪用法,可能是本人的才疏學(xué)淺沒見過這種寫法linux命令大全,于是乎查閱資料了一番linux命令大全,并進(jìn)行了總結(jié),希望也能夠幫助有需要的朋友。
以下是我總結(jié)的幾種常用方式:
1. 最普通用法
cat /proc/version
Linux version 2.6.32-5-686 (Debian 2.6.32-38)
等價于:
cat < /proc/version
cat /proc/version -n // 顯示行號
2. 從鍵盤創(chuàng)建一個文件
(1)先看個簡單的:
root@localhost:~# cat // 直接輸入cat命令回車
hello
hello
world
world
ctrl + D // 結(jié)束輸入
解釋:cat命令從標(biāo)準(zhǔn)輸入中讀取數(shù)據(jù)并打印到標(biāo)準(zhǔn)輸出, 因此屏幕上看到的2次信息
(2)再看一個擴(kuò)展的:
root@localhost:~# cat > file.txt
hello
world
ctrl + D // 相當(dāng)于EOF的符號
root@localhost:~# cat file.txt // 查看file.txt文件
hello // 將從鍵盤輸入的數(shù)據(jù)保存在了file.txt中
world
解釋:cat命令從標(biāo)準(zhǔn)輸入讀取數(shù)據(jù),并未打印到標(biāo)準(zhǔn)輸出,而是通過>重定向到文件file.txt,達(dá)到了從鍵盤創(chuàng)建文件的效果
擴(kuò)展:>符號會將原來文件覆蓋(如果存在) 如果想要追加鍵盤輸入的內(nèi)容, 需要將">" -> ">>"即可
3. 合并多個文件內(nèi)容
root@localhost:~# ls
root@localhost:~# file1.txt file2.txt
root@localhost:~# cat file1.txt
hello
root@localhost:~# cat file2.txt
world
root@localhost:~# cat file1.txt file2.txt > file3.txt // 合并2個文件, 多個文件也是一樣的
root@localhost:~# cat file3.txt
hello
world
注:同理可以合并多個文件
4. Here文檔
(1) 打印到屏幕
root@localhost:~# cat <<EOF
> This is here doc.
> Only used to display.
> The third line.
> EOF
This is here doc.
Only used to display.
The third line.
解釋:這種方式是將EOF標(biāo)識符中間的內(nèi)容輸出的標(biāo)準(zhǔn)輸出.
(2) 輸出到文件(>>可以追加)
root@localhost:~# cat <<EOF > output.txt
> This is here doc.
> Only used to display.
下一個教程:Linux diff 命令用法詳解