linux常用命令之查閱文件

CAT

cat – concatenate print files 連續的輸出文件內容

用法

cat [-nbA] file

選項

  • -n line number 輸出行號
  • -b line number nonblank 輸出空白行的行號
  • -A show All 顯示特殊字符

TAC

tac – concatenate print files reverse 反序的查看文件

MORE

  • more – 一頁一頁翻動
  • 功能鍵
  • 空格space 下一頁
  • enter 下一行
  • /xxx 向下搜索xxx
  • n 搜索下一個
  • :f 顯示文件名並顯示行號
  • q 退出
  • b或ctrl+b 向上翻頁

LESS

也是一頁一頁的翻動

比more的功能更強大

功能鍵

  • more有的less都有
  • 相對於more新增的:
  • pagedown 下一頁
  • pageup 上一頁
  • ?xxx 向上搜索
  • n搜索下一個
  • N反向搜索前一個

HEAD

head – output the first part of files 輸出文件的前幾行

用法

head [-n number] file

選項

  • -n 後面接數字,表示輸出前幾行

DEMO

head -n 5 demo.txt

輸出demo.txt的前五行

head -n -100 demo.txt

不輸出demo.txt的後100行

TAIL

tail – output the last part of files 輸出文件的後幾行

用法

tail [-n number] file

選項

  • -n lines 後面接數字,表示輸出後幾行
  • -f follow output data as the files grow 持續輸出文件的信息,當這個文件持續有數據寫入的時候,-f就會持續輸出出來,只有按下ctrl+c纔會停止

WHICH

which – locate a command 定位命令

which會在$PATH中查找命令

選項

  • -a show all 顯示出所有匹配的命令,不止是第一個

注意

  • which是精確查找,加通配符也沒用

WHEREIS

whereis – locate the binary、shource and manual page files for a command

$PATH中查找指令的執行(二進制)文件、源文件和操作手冊

默認:binary、shource and manual page都會顯示出來

選項

  • -b binary 顯示執行文件(二進制文件)
  • -m manual page 顯示操作手冊
  • -s source 顯示源文件

注意

  • whereis只支持精確搜索,不能模糊搜索
  • 在數據庫中搜索,速度比find在硬盤搜索要快

LOCATE

locate – find files by name 查找文件

和which、whereis不同的是,locate不僅僅只在$PATH中查找

選項

  • -i ignore case 忽略大小寫

注意

  • locate也是在數據庫搜索,比find在硬盤搜素要快
  • locate支持模糊搜索
  • updatedb可以更新數據庫

FIND

查找文件

在硬盤中查找

用法

find filename [option] [action]

選項

時間篩選

  • -atime、-mtime、-ctime,後面接數字
  • -mtime 5,表示修改時間在第五天前到第四天前的24小時
  • -mtime +5,表示修改時間在五天前
  • -mime -5,表示修改時間在五天內,不包括第五天!
  • -newer file,表示篩選出比file要新的文件

用戶篩選

  • -user name 篩選出owner爲name的文件
  • -group name 篩選出group爲name的文件
  • -nouser 篩選出owner不存在/etc/passwd的文件
  • -nogroup 篩選出group不存在/etc/group的文件
  • -uid id 篩選出文件的owner的id爲id的文件,id在/etc/passwd
  • -gid id 篩選出文件的group的id爲id的文件,id在/etc/group

文件屬性篩選

  • -name name 精確篩選文件名爲name的文件
  • -size [+-] size 篩選出文件大小 大於(+)或小於(-)size的文件
  • -type type 一般文件:f,目錄:d,鏈接文件:l,設備文件:b或c,socket文件:s,管道文件:p
  • -perm [+-] mode,mode爲數字模式,篩選出權限爲mode,或者包括全部(+)mode,或者包括任一組(-)mode

-其他可進行的操作

  • -exec command 執行其他指令

find / -size +500k -exec ls -l {} \;

在根目錄下以及根目錄的所有子目錄下,搜索出大小大於500kb的文件,並且列出詳細信息

{}:代表find搜索到的結果

\; :表示其他指令的結束

查看原文:http://139.129.55.235/2016/06/02/linux%e5%b8%b8%e7%94%a8%e5%91%bd%e4%bb%a4%e4%b9%8b%e6%9f%a5%e9%98%85%e6%96%87%e4%bb%b6/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章