1. 程式人生 > >Linux-1.4檔案操作命令(grep,cat,tail,head,less,find,chmod,tail,less)

Linux-1.4檔案操作命令(grep,cat,tail,head,less,find,chmod,tail,less)

Linux基礎命令(grep,cat,tail,head,less,find,chmod,tail,less)

grep(常用)

  •  grep 指定“檔案”搜尋檔案內容
    • grep hello 1.txt 在檔案中查詢字串
    • grep -n hello 1.txt 顯示字串的行數
    • grep -i hello 1.txt 不區分大小寫
    • grep -in hello 1.txt 顯示字串的行數&不區分大小寫
    • grep -v hello 1.txt 反向查詢不包含字串的

      

  • grep 在目錄下搜尋內容包含hello的檔案
    • grep -n hello 目錄 -r

      

 

  • 正則查詢
    • h* 查詢包含h的
    • ^h 查詢行頭是h的
    • h$ 查詢行尾是h的

      

cat

  • cat 檢視檔案內容
    • cat 1.txt 把檔案內容全部顯示在終端中 
    • cat 1.txt 2.txt 把n個檔案內容依次全部顯示在終端中 
    • cat 1.txt 2.txt > 3.txt 顯示後內容合併儲存到新的檔案中

tail

  •  tail 顯示指定檔案末尾內容(常用檢視日誌檔案)
    • tail a.log 預設顯示檔案末尾10行(等同於tail -10 a.log,等同於tail -n -10 a.log)
    • tail -n +5 a.log 從檔案的第6條顯示所有內容
    • tail -f a.log 迴圈檢視檔案內容,會刷新出檔案新增的內容。使用於文件監視

head

  • head 顯示指定檔案頭部內容
    • head a.log 預設顯示檔案頭10行(等同於head -10 a.log,等同於head -n -10 a.log)
    • head -n +5 a.log 顯示檔案的最後5條

less 

  • less 分頁顯示檔案資訊
    • less a.log(↑↓箭頭檢視檔案內容,more不能回放)
    • 按q退出
    • less -N a.log 顯示了行號
    • less -N +10 a.log 從第十行開始顯示

find

  •  find 查詢檔案
    • find /home -name a.txt 在目錄下查詢指定檔案
    • find /home -name '*txt' 在目錄下查詢以txt結尾的檔案(一定要用英文單引號!)

chmod

  •  chmod 字母修改檔案許可權
    • chmod u+x a.txt
      • r,w,x,- 許可權(read,write,excute,-表示無許可權)
      • u,g,o,a(u 檔案所有者;g 同組的人;o 其他人;a 所有人)
      • +,-,=(許可權的增加,減少,賦值)

       

  •  chmod 數字修改檔案許可權
    • chmod 761 a.txt
    • 三個數字依次表示使用者型別:ugo
    • 數字的值表示許可權r=4,w=2,x=1,-=0
      • 若要rwx屬性則4+2+1=7;
      • 若要rw-屬性則4+2=6;
      • 若要r-x屬性則4+1=7。