Linux向文件添加內容的幾種方法


需求:向 file.txt 添加內容 "hello world"

 

方法1:文本編輯器

[root@linux test_shell]# vim file.txt

 

方法2:輸出重定向

[root@linux test_shell]# echo "hello world" >> file.txt

 

[root@linux test_shell]# cat >> file.txt
hello world
[root@linux test_shell]#

備注:按 Ctrl + D 結束編輯。

 

方法3:輸入輸出重定向

[root@linux test_shell]# cat >> file.txt << end
> hello world
> end
[root@linux test_shell]#

 

方法4:sed流編輯器

[root@linux test_shell]# sed -i '$a hello world' file.txt

 

總結:

添加內容較少時,推薦使用 方法2

添加內容較多時,推薦使用 方法3

在指定位置 添加/插入/修改 內容時,使用 方法4

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM