共計(jì) 2145 個(gè)字符,預(yù)計(jì)需要花費(fèi) 6 分鐘才能閱讀完成。
自動(dòng)寫(xiě)代碼機(jī)器人,免費(fèi)開(kāi)通
這篇文章給大家分享的是有關(guān)怎么在 Linux 中將文本內(nèi)容追加到文件末尾的內(nèi)容。丸趣 TV 小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨丸趣 TV 小編過(guò)來(lái)看看吧。
使用 運(yùn)算符附加文本
運(yùn)算符會(huì)將輸出重定向到文件,如果文件不存在,則創(chuàng)建該文件,但如果存在,則輸出就會(huì)附加在文件的末尾。
例如,您可以使用 echo 命令將文本附加到文件的末尾,如圖所示。
#echo“ / mnt / pg_master / wal_archives 10.20.20.5(rw,sync,no_root_squash)” / etc / exports#echo“ / mnt / pg_master / wal_archives 10.20.20.5(rw,sync,no_root_squash)” / etc / exports
或者,您可以使用 printf 命令 (不要忘記使用 \ n 字符添加下一行)。
#printf“ / mnt / pg_master / wal_archives 10.20.20.5(rw,sync,no_root_squash)\ n” / etc / exports#printf“ / mnt / pg_master / wal_archives 10.20.20.5(rw,sync,no_root_squash)\ n” / etc / exports
您還可以使用 cat 命令連接一個(gè)或多個(gè)文件中的文本,并將其附加到另一個(gè)文件中。
在以下示例中,要添加到 / etc / exports 配置文件中的其他文件系統(tǒng)共享被添加到名為 shares.txt 的文本文件中。
# cat /etc/exports # cat shares.txt # cat shares.txt /etc/exports # cat /etc/exports# cat /etc/exports# cat shares.txt# cat shares.txt /etc/exports# cat /etc/exports
此外,您還可以使用以下此處的文檔將配置文本附加到文件的末尾,如下所示。
# cat /etc/exports # cat /etc/exports s EOF /backups 10.20.20.0/24(rw,sync) /mnt/nfs_all 10.20.20.5(rw,sync) EOF # cat /etc/exports# cat /etc/exports# cat /etc/exports s EOF /backups 10.20.20.0/24(rw,sync) /mnt/nfs_all 10.20.20.5(rw,sync) EOF# cat /etc/exports
注意:不要將 重定向運(yùn)算符誤認(rèn)為是 ; 對(duì)現(xiàn)有文件使用 會(huì)刪除該文件的內(nèi)容,然后將其覆蓋,這可能會(huì)導(dǎo)致數(shù)據(jù)丟失。
使用 tee 命令附加文本
tee 命令從標(biāo)準(zhǔn)輸入中復(fù)制文本,并將其粘貼 / 寫(xiě)入到標(biāo)準(zhǔn)輸出和文件。您可以使用它的 - a 標(biāo)志將文本附加到文件的末尾,如下所示。
# echo /mnt/pg_master/wal_archives 10.20.20.5(rw,sync,no_root_squash) | tee -a /etc/exports OR # cat shares.txt | tee -a /etc/exports# echo /mnt/pg_master/wal_archives 10.20.20.5(rw,sync,no_root_squash) | tee -a /etc/exportsOR# cat shares.txt | tee -a /etc/exports# echo /mnt/pg_master/wal_archives 10.20.20.5(rw,sync,no_root_squash) | tee -a /etc/exportsOR# cat shares.txt | tee -a /etc/exports# echo /mnt/pg_master/wal_archives 10.20.20.5(rw,sync,no_root_squash) | tee -a /etc/exportsOR# cat shares.txt | tee -a /etc/export
您還可以使用帶有 tee 命令的 here 文檔。
# cat EOF | tee -a /etc/exports /backups 10.20.20.0/24(rw,sync) /mnt/nfs_all 10.20.20.5(rw,sync) EOF# cat EOF | tee -a /etc/exports /backups 10.20.20.0/24(rw,sync) /mnt/nfs_all 10.20.20.5(rw,sync)EOF
感謝各位的閱讀!關(guān)于“怎么在 Linux 中將文本內(nèi)容追加到文件末尾”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
向 AI 問(wèn)一下細(xì)節(jié)
丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!