Linux常用文件管理命令

常用文件管理命令

cat

cat命令連接文件并打印到標準輸出設備上,cat經常用來顯示文件的內容

語法

    cat(選項)(參數)

選項

    -n或-number:有1開始對所有輸出的行數編號;
    -b或--number-nonblank:和-n相似,只不過對于空白行不編號;
    -s或--squeeze-blank:當遇到有連續兩行以上的空白行,就代換為一行的空白行;
    -A:顯示不可打印字符,行尾顯示“$”;
    -e:等價于"-vE"選項;
    -t:等價于"-vT"選項;

參數

文件列表:指定要連接的文件列表。

實例

  • 顯示文本
    [root@localhost tmp]# cat /tmp/test
    hello world
    
    
    
    
    
    hello world 
    
  • 輸出帶編號文本
    [root@localhost tmp]# cat -n /tmp/test
         1  hello world
         2
         3
         4
         5
         6
         7  hello world 
    
  • 輸出帶編號文本,忽略空白行編號
    [root@localhost tmp]# cat -b /tmp/test
         1  hello world
    
    
    
    
    
         2  hello world 
    
  • 輸出帶編號的文本,如遇到連續空白行,就代換為一行空白行
    [root@localhost tmp]# cat -ns /tmp/test
         1  hello world
         2
         3  hello world 
    
  • 文本操作

創建文本

    [root@localhost tmp]# cat > /tmp/test2
    hello everyone

    hello world
    ^C
    [root@localhost tmp]# cat /tmp/test2
    hello everyone

    hello world

把test的文件加上行號輸入到test2中

    [root@localhost tmp]# cat -n /tmp/test > /tmp/test2
    [root@localhost tmp]# cat /tmp/test2
         1  hello world
         2
         3
         4
         5
         6
         7  hello world 

把test和test2的文本內容加上行號(空白行不加)之后將內容附加到test3里

    [root@localhost tmp]# cat -b /tmp/test /tmp/test2 > /tmp/test3
    [root@localhost tmp]# cat /tmp/test3
         1  hello world





         2  hello world 
         3       1  hello world
         4       2
         5       3
         6       4
         7       5
         8       6
         9       7  hello world 

清空test2的內容

    [root@localhost tmp]# cat /dev/null > /tmp/test2
    [root@localhost tmp]# cat /tmp/test2
    [root@localhost tmp]# 

tac

tac命令用于將文件已行為單位的反序輸出,即第一行最后顯示,最后一行先顯示。

語法

    tac(選項)(參數)     

選項

    -a或——append:將內容追加到文件的末尾;
    -i或——ignore-interrupts:忽略中斷信號。

參數

文件列表:指定要保存內容的文件列表。

實例

  • 反向顯示test3的文本
    [root@localhost tmp]# tac /tmp/test3
         9       7  hello world 
         8       6
         7       5
         6       4
         5       3
         4       2
         3       1  hello world
         2  hello world 
    
    
    
    
    
    
         1  hello world
    

head

head命令用于顯示文件的開頭的內容。在默認情況下,head命令顯示文件的頭10行內容。

語法

    head(選項)(參數)

選項

    -n<數字>:指定顯示頭部內容的行數;
    -c<字符數>:指定顯示頭部內容的字符數;
    -v:總是顯示文件名的頭信息;
    -q:不顯示文件名的頭信息。

參數

文件列表:指定顯示頭部內容的文件列表。

實例

  • 顯示前5行的內容
    [root@localhost tmp]# head  -5 /tmp/test3
         1  hello world
    
    
    
    
    [root@localhost tmp]# 
    

tail

tail命令用于輸入文件中的尾部內容。tail命令默認在屏幕上顯示指定文件的末尾10行。

語法

    tail(選項)(參數)

選項

    --retry:即是在tail命令啟動時,文件不可訪問或者文件稍后變得不可訪問,都始終嘗試打開文件。使用此選項時需要與選項“——follow=name”連用;
    -c<N>或——bytes=<N>:輸出文件尾部的N(N為整數)個字節內容;
    -f<name/descriptor>或;--follow<nameldescript>:顯示文件最新追加的內容?!皀ame”表示以文件名的方式監視文件的變化?!?f”與“-fdescriptor”等效;
    -F:與選項“-follow=name”和“--retry"連用時功能相同;
    -n<N>或——line=<N>:輸出文件的尾部N(N位數字)行內容。
    --pid=<進程號>:與“-f”選項連用,當指定的進程號的進程終止后,自動退出tail命令;
    -q或——quiet或——silent:當有多個文件參數時,不輸出各個文件名;
    -s<秒數>或——sleep-interal=<秒數>:與“-f”選項連用,指定監視文件變化時間隔的秒數;
    -v或——verbose:當有多個文件參數時,總是輸出各個文件名;
    --help:顯示指令的幫助信息;
    --version:顯示指令的版本信息。

參數

文件列表:指定要顯示尾部內容的文件列表。

實例

  • 顯示最后5行的內容
    [root@localhost tmp]# tail -5 /tmp/test3
         5       3
         6       4
         7       5
         8       6
         9       7  hello world 
    
  • 從第二行開始顯示整個文本
    [root@localhost tmp]# tail -n +2 /tmp/test3
    
    
    
    
    
         2  hello world 
         3       1  hello world
         4       2
         5       3
         6       4
         7       5
         8       6
         9       7  hello world 
    
  • 從第2行開始顯示10條文本
    [root@localhost tmp]# tail -n +2 /tmp/test3 | head -n 10
    
    
    
    
    
         2  hello world 
         3       1  hello world
         4       2
         5       3
         6       4
    
  • 動態顯示文本,先進先出
    [root@localhost tmp]# tail -f /tmp/test3
    
    
         2  hello world 
         3       1  hello world
         4       2
         5       3
         6       4
         7       5
         8       6
         9       7  hello world 
    

more

more命令是一個基于vi編輯器文本過濾器,它以全屏幕的方式按頁顯示文本文件的內容,支持vi中的關鍵字定位操作。

該命令一次顯示一屏文本,滿屏后停下來,并且在屏幕的底部出現一個提示信息,給出至今己顯示的該文件的百分比:–More–(XX%)可以用下列不同的方法對提示做出回答:

按Space鍵:顯示文本的下一屏內容。
按Enter鍵:只顯示文本的下一行內容。 按斜線符|:接著輸入一個模式,可以在文本中尋找下一個相匹配的模式。
按H鍵:顯示幫助屏,該屏上有相關的幫助信息。
按B鍵:顯示上一屏內容。
按Q鍵:退出rnore命令。

語法

    more(語法)(參數)

選項

    -<數字>:指定每屏顯示的行數;
    -d:顯示“[press space to continue,'q' to quit.]”和“[Press 'h' for instructions]”;
    -c:不進行滾屏操作。每次刷新這個屏幕;
    -s:將多個空行壓縮成一行顯示;
    -u:禁止下劃線;
    +<數字>:從指定數字的行開始顯示

參數

文件:指定分頁顯示內容的文件。

實例

  • 一次顯示8行文本
    [root@localhost tmp]# more -8 /tmp/test3
         1  hello world
    
    
    
    
    
         2  hello world 
         3       1  hello world
    --More--(40%)
    
  • 一次顯示8行的內容
    [root@localhost tmp]# ll /etc | more -8
    total 1088
    -rw-r--r--.  1 root root     16 Dec 23 22:07 adjtime
    -rw-r--r--.  1 root root   1518 Jun  7  2013 aliases
    -rw-r--r--.  1 root root  12288 Dec 23 22:08 aliases.db
    drwxr-xr-x.  2 root root    236 Dec 23 22:04 alternatives
    -rw-------.  1 root root    541 Mar 31  2016 anacrontab
    drwxr-xr-x.  3 root root     51 Jan  1 13:44 ansible
    -rw-r--r--.  1 root root     55 Nov  5  2016 asound.conf
    --More--
    
  • 從第3行開始顯示
    [root@localhost tmp]# more +3 /tmp/test3
    
    
    
    
         2  hello world 
         3       1  hello world
         4       2
         5       3
         6       4
         7       5
         8       6
         9       7  hello world 
    

less

less命令的作用與more十分相似,都可以用來瀏覽文字檔案的內容,不同的是less命令允許用戶向前或向后瀏覽文件,而more命令只能向前瀏覽。

用less命令顯示文件時,用PageUp鍵向上翻頁,用PageDown鍵向下翻頁。要退出less程序,應按Q鍵。

語法

    less(選項)(參數)

選項

    -e:文件內容顯示完畢后,自動退出;
    -f:強制顯示文件;
    -g:不加亮顯示搜索到的所有關鍵詞,僅顯示當前顯示的關鍵字,以提高顯示速度;
    -l:搜索時忽略大小寫的差異;
    -N:每一行行首顯示行號;
    -s:將連續多個空行壓縮成一行顯示;
    -S:在單行顯示較長的內容,而不換行顯示;
    -x<數字>:將TAB字符顯示為指定個數的空格字符。

參數

文件:指定要分屏顯示內容的文件。


sed

顯示中間文本的內容

語法

sed [-hnV][-e<script>][-f<script文件>][文本文件]

選項

    -e<script>或--expression=<script>:以選項中的指定的script來處理輸入的文本文件;
    -f<script文件>或--file=<script文件>:以選項中指定的script文件來處理輸入的文本文件;
    -h或--help:顯示幫助;
    -n或--quiet或——silent:僅顯示script處理后的結果;
    -V或--version:顯示版本信息。

參數

文件:指定待處理的文本文件列表。

動作

    a :新增, a 的后面可以接字串,而這些字串會在新的一行出現(目前的下一行)~
    c :取代, c 的后面可以接字串,這些字串可以取代 n1,n2 之間的行!
    d :刪除,因為是刪除啊,所以 d 后面通常不接任何咚咚;
    i :插入, i 的后面可以接字串,而這些字串會在新的一行出現(目前的上一行);
    p :打印,亦即將某個選擇的數據印出。通常 p 會與參數 sed -n 一起運行~
    s :取代,可以直接進行取代的工作哩!通常這個 s 的動作可以搭配正規表示法!例如 1,20s/old/new/g 就是啦!

實例

  • 顯示第4行文本
    [root@localhost tmp]# sed -n '8p' /tmp/test3
         3       1  hello world
    
  • 顯示第8行到第11行文本
    [root@localhost tmp]# sed -n '8,11p' /tmp/test3
         3       1  hello world
         4       2
         5       3
         6       4
    
  • 顯示第8行到最后一行文本
    [root@localhost tmp]# sed -n '8,$p' /tmp/test3
         3       1  hello world
         4       2
         5       3
         6       4
         7       5
         8       6
         9       7  hello world 

 

本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/92054

(0)
eighteenxueighteenxu
上一篇 2018-03-11 13:26
下一篇 2018-03-11 16:12

相關推薦

  • Linux 文 本 處 理 工 具

    Linux 文 本 處 理 工 具 一.學習大綱: ◎各種文本工具來查看、分析、統計文本文件 文件內容查看工具:cat, tac,rev,more,less 文件截?。篽ead和tail 按列抽?。篶ut,paste 分析文本的工具:wc , sort , uniq,diff和patch 命令使用練習題 ◎文本過濾與處理工具: grep與正則表達式…

    Linux干貨 2016-08-05
  • 第二周作業

    1、Linux上的文件管理類命令都有哪些,其常用的使用方法及其相關示例演示。 目錄管理類的命令: mkdir, rmdir mkdir:make directories mkdir [OPTION]… DIRECTORY… -p: 自動按需創建父目錄; -v: verbose,顯示詳細過程; -m MODE:直接給定權限; 注意:路徑…

    Linux干貨 2018-01-17
  • 20160802作業

    20160802作業 1、每日課堂筆記總結 2、預習 3、每日課堂pdf練習 4、在/data/testdir里創建的新文件自動屬于g1組,組g2的成員如: alice能對這些新文件有讀寫權限,組g3的成員如:tom只能對新文件有讀權限,其它用戶(不屬于g1,g2,g3)不能訪問這個文件夾。 [root@Centos7 ~]# chmod…

    Linux干貨 2016-08-04
  • 文本處理三劍客之awk

    一、知識整理 1、awk報告生成器,格式化文本輸出 發明人:a.k.a. Aho,Kernighan,weinberger awk程序通常由:BEGIN語句塊、能夠使用模式匹配的通用語句塊、END語句塊三部分組成。program通常是放在單引號或雙引號中。 基本用法:awk [] ‘program’ var=value fiel… pr…

    Linux干貨 2016-09-26
  • 網絡工具

    測試網絡 顯示主機名     hostname     centos6 /etc/sysconfig/network     更改主機名        &nbs…

    Linux干貨 2016-09-09
  • bash腳本編程實例

    bash腳本編程實例 1.寫一個腳本 接受一個以上文件路徑作為參數 顯示每個文件擁有的行數 總結說明本次共為幾個文件統計了其行數 #!/bin/bash # read -p “please input some paths:” paths if [ -z $paths ];then echo “There are not any paths inputtin…

    Linux干貨 2017-08-14
欧美性久久久久