Linux對檔案內容基本操作(學習筆記七)

來源:互聯網
上載者:User

標籤:

一、cat1.1、查看檔案內容

格式:cat 檔案名稱

[[email protected] /]# cat /etc/resolv.conf # Generated by NetworkManagernameserver 10.198.1.1nameserver 8.8.8.8[[email protected] /]# 

但是當檔案內容比較多,超過一屏時,cat命令就不能滿足查看要求,就要用到more|less來查看

1.2、重新導向輸入檔案內容到新的檔案
[[email protected] Documents]# cat /etc/resolv.conf  > a.txt[[email protected] Documents]# cat a.txt # Generated by NetworkManagernameserver 10.198.1.1nameserver 8.8.8.8[[email protected] Documents]# 

一個">"表示清空檔案,重新寫入新的內容,如果檔案存在就清空,如果檔案不存在就建立

二個">>"表示在檔案末尾追加的末尾,如果檔案存在就增加,如果檔案不存在就建立

[[email protected] Documents]# cat /etc/httpd/conf.d/welcome.conf >>a.txt[[email protected] Documents]# cat a.txt # Generated by NetworkManagernameserver 10.198.1.1nameserver 8.8.8.8# # This configuration file enables the default "Welcome"# page if there is no default index page present for# the root URL.  To disable the Welcome page, comment# out all the lines below.#<LocationMatch "^/+$">    Options -Indexes    ErrorDocument 403 /error/noindex.html</LocationMatch>[[email protected] Documents]# 
1.3 、重新導向檔案清空或者追加內容
[[email protected] Documents]# cat > a.txt<<gg> hello linux> gg[[email protected] Documents]# cat a.txt hello linux[[email protected] Documents]# 
[[email protected] Documents]# cat >> a.txt <<fw> rrrrrrrrrrrrrr> qqqqqqqqq> wwwwwwwwwww> eeeeee> fw[[email protected] Documents]# cat a.txt hello linuxrrrrrrrrrrrrrrqqqqqqqqqwwwwwwwwwwweeeeee[[email protected] Documents]# 

將2個標識符之間的肉寫入檔案中,2個標識符可自訂,成對出現即可

一個">"表示清空檔案,重新寫入新的內容,如果檔案存在就清空,如果檔案不存在就建立

二個">>"表示在檔案末尾追加的末尾,如果檔案存在就增加,如果檔案不存在就建立

二、more/less

格式: more/less 檔案名稱

用於檔案內容比較多時查看檔案內容:用斷行符號或者空格向下翻頁,按b向上翻頁,按q鍵退出

cat 翻頁功能因此經常和more命令搭配使用,cat命令還有就是將數個檔案合并成一個檔案的功能。
more命令功能:讓畫面在顯示滿一頁時暫停,此時可按空格健繼續顯示下一個畫面,或按Q鍵停止顯示。
less命令功能:less命令的用法與more命令類似,也可以用來瀏覽超過一頁的檔案。所不同的是less命令除了可以按空格鍵向下顯示檔案外,還可以利用上下鍵來捲動檔案。當要結束瀏覽時,只要在less命令的提示符“:”下按Q鍵即可。
其實這三個命令除了cat命令有合并檔案的功能,其餘功能上相近,只是從瀏覽習慣和顯示方式上有所不同。

三、grep

是一種強大的文本搜尋工具,它能使用Regex搜尋文本,並把匹配的行列印出來

[[email protected] Documents]# grep -i "q" a.txt qqqqqqqqq[[email protected] Documents]# grep -iv "q" a.txt hello linuxrrrrrrrrrrrrrrwwwwwwwwwwweeeeee[[email protected] Documents]# 
四、vi

簡單命令:

簡單應用:

vi 檔案名稱

按i鍵進行編輯

按ESC,:q退出不儲存

按ESC,:q!退出不儲存

按ESC,:wq退出儲存

 

Linux對檔案內容基本操作(學習筆記七)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.