標籤:style blog http ar color os 使用 sp java
cat命令的用途是串連檔案或標準輸入並列印。
1.一次顯示整個檔案:cat filename
2.從鍵盤建立一個檔案:cat > filename 只能建立新檔案,不能編輯已有檔案.
3.將幾個檔案合并為一個檔案:cat file1 file2 > file
相比cat,還有一些其他的命令可以更靈活的查看檔案內容。
more命令
more命令,功能類似 cat ,cat命令是整個檔案的內容從上到下顯示在螢幕上。 more會以一頁一頁的顯示方便使用者逐頁閱讀,而最基本的指令就是按空白鍵(space)就往下一頁顯示,按 b 鍵就會往回(back)一頁顯示,而且還有搜尋字串的功能 。more命令從前向後讀取檔案,因此在啟動時就載入整個檔案。
1.命令格式:
more [-dlfpcsu ] [-num ] [+/ pattern] [+ linenum] [file ... ]
2.命令功能:
more命令和cat的功能一樣都是查看檔案裡的內容,但有所不同的是more可以按頁來查看檔案的內容,還支援直接跳轉行等功能。
3.命令參數:
+n 從笫n行開始顯示
-n 定義螢幕大小為n行
+/pattern 在每個檔案顯示前搜尋該字串(pattern),然後從該字串前兩行之後開始顯示
-c 從頂部清屏,然後顯示
-d 提示“Press space to continue,’q’ to quit(按空格鍵繼續,按q鍵退出)”,禁用響鈴功能
-l 忽略Ctrl+l(換頁)字元
-p 通過清除視窗而不是滾屏來對檔案進行換頁,與-c選項相似
-s 把連續的多個空行顯示為一行
-u 把檔案內容中的下畫線去掉
4.常用操作命令:
Enter 向下n行,需要定義。預設為1行
Ctrl+F 向下滾動一屏
空格鍵 向下滾動一屏
Ctrl+B 返回上一屏
= 輸出當前行的行號
:f 輸出檔案名和當前行的行號
V 調用vi編輯器
!命令 調用Shell,並執行命令
q 退出more
5.命令執行個體:
執行個體1:顯示檔案中從第3行起的內容
命令:
more +3 log2012.log
輸出:
[[email protected] test]# cat log2012.log
2012-01
2012-02
2012-03
2012-04-day1
2012-04-day2
2012-04-day3
======[[email protected] test]# more +3 log2012.log
2012-03
2012-04-day1
2012-04-day2
2012-04-day3
======[[email protected] test]#
執行個體2:從檔案中尋找第一個出現"day3"字串的行,並從該處前兩行開始顯示輸出
命令:
more +/day3 log2012.log
輸出:
[[email protected] test]# more +/day3 log2012.log
...skipping
2012-04-day1
2012-04-day2
2012-04-day3
2012-05
2012-05-day1
======[[email protected] test]#
執行個體3:設定每屏顯示行數
命令:
more -5 log2012.log
輸出:
[[email protected] test]# more -5 log2012.log
2012-01
2012-02
2012-03
2012-04-day1
2012-04-day2
按 Ctrl+F 或者 空格鍵 將會顯示下一屏5條內容,百分比也會跟著變化。
執行個體4:列一個目錄下的檔案,由於內容太多,我們應該學會用more來分頁顯示。這得和管道 | 結合起來
命令:
ls -l | more -5
輸出:
[[email protected] test]# ls -l | more -5
總計 36
-rw-r--r-- 1 root root 308 11-01 16:49 log2012.log
-rw-r--r-- 1 root root 33 10-28 16:54 log2013.log
-rw-r--r-- 1 root root 127 10-28 16:51 log2014.log
lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log -> log.log
-rw-r--r-- 1 root root 25 10-28 17:02 log.log
-rw-r--r-- 1 root root 37 10-28 17:07 log.txt
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
drwxrwxrwx 2 root root 4096 10-28 14:47 test4
說明:
每頁顯示5個檔案資訊,按 Ctrl+F 或者 空格鍵 將會顯示下5條檔案資訊。
less命令
less 工具也是對檔案或其它輸出進行分頁顯示的工具,應該說是linux正統查看檔案內容的工具,功能極其強大。less 的用法比起 more 更加的有彈性。在 more 的時候,我們並沒有辦法向前面翻, 只能往後面看,但若使用了 less 時,就可以使用 [pageup] [pagedown] 等按鍵的功能來往前往後翻看檔案,更容易用來查看一個檔案的內容!除此之外,在 less 裡頭可以擁有更多的搜尋功能,不止可以向下搜,也可以向上搜。
1.命令格式:
less [參數] 檔案
2.命令功能:
less 與 more 類似,但使用 less 可以隨意瀏覽檔案,而 more 僅能向前移動,卻不能向後移動,而且 less 在查看之前不會載入整個檔案。
3.命令參數:
-b <緩衝區大小> 設定緩衝區的大小
-e 當檔案顯示結束後,自動離開
-f 強迫開啟特殊檔案,例如外圍裝置代號、目錄和二進位檔案
-g 只標誌最後搜尋的關鍵詞
-i 忽略搜尋時的大小寫
-m 顯示類似more命令的百分比
-N 顯示每行的行號
-o <檔案名稱> 將less 輸出的內容在指定檔案中儲存起來
-Q 不使用警告音
-s 顯示連續空行為一行
-S 行過長時間將超出部分捨棄
-x <數字> 將“tab”鍵顯示為規定的數字空格
/字串:向下搜尋“字串”的功能
?字串:向上搜尋“字串”的功能
n:重複前一個搜尋(與 / 或 ? 有關)
N:反向重複前一個搜尋(與 / 或 ? 有關)
b 向後翻一頁
d 向後翻半頁
h 顯示協助介面
Q 退出less 命令
u 向前滾動半頁
y 向前滾動一行
空格鍵 滾動一行
斷行符號鍵 滾動一頁
[pagedown]: 向下翻動一頁
[pageup]: 向上翻動一頁
4.使用執行個體:
執行個體1:查看檔案
命令:
less log2013.log
輸出:
執行個體2:ps查看進程資訊並通過less分頁顯示
命令:
ps -ef |less
輸出:
執行個體3:查看命令曆史使用記錄並通過less分頁顯示
命令:
history | less
輸出:
[[email protected] test]# history | less
22 scp -r tomcat6.0.32 [email protected]:/opt/soft
23 cd ..
24 scp -r web [email protected]:/opt/
25 cd soft
26 ls
27 scp -r jdk1.6.0_16/ [email protected]:/opt/soft
28 clear
29 vim /etc/profile
30 vim /etc/profile
31 cd tomcat6.0.32/bin/
32 ls
33 ./shutdown.sh
34 ./startup.sh
35 vim startup.sh
36 ls
37 echo $JAVA_HOME
38 java
39 ls
40 ls
41 clear
42 cd /opt
43 ls
44 cp apache-tomcat-6.0.32.tar.gz soft/
45 ls
46 rm -f apache-tomcat-6.0.32.tar.gz
47 ls
48 cd soft
49 ls
50 tar -vzf apache-tomcat-6.0.32.tar.gz
51 tar -vzfx apache-tomcat-6.0.32.tar.gz
52 tar -zxvf apache-tomcat-6.0.32.tar.gz
53 ls
54 cd apache-tomcat-6.0.32
55 ls
56 cd ..
57 mv apache-tomcat-6.0.32 tomcat6.0.32
58 ls
59 cd tomcat6.0.32/
60 ls
執行個體5:瀏覽多個檔案
命令:
Less log2013.log log2014.log
輸出:
說明:
輸入 :n後,切換到 log2014.log
輸入 :p 後,切換到log2013.log
5.附加備忘
1.全屏導航
ctrl + F - 向前移動一屏
ctrl + B - 向後移動一屏
ctrl + D - 向前移動半屏
ctrl + U - 向後移動半屏
2.單行導航
j - 向前移動一行
k - 向後移動一行
3.其它導航
G - 移動到最後一行
g - 移動到第一行
q / ZZ - 退出 less 命令
4.其它有用的命令
v - 使用配置的編輯器編輯當前檔案
h - 顯示 less 的協助文檔
&pattern - 僅顯示匹配模式的行,而不是整個檔案
5.標記導航
當使用 less 查看大檔案時,可以在任何一個位置作標記,可以通過命令導航到標有特定標記的文本位置:
ma - 使用 a 標記文本的當前位置
‘a - 導航到標記 a 處
head命令
head 與 tail 就像它的名字一樣的淺顯易懂,它是用來顯示開頭或結尾某個數量的文字區塊,head 用來顯示檔案的開頭至標準輸出中,而 tail 想當然爾就是看檔案的結尾。
1.命令格式:
head [參數]... [檔案]...
2.命令功能:
head 用來顯示檔案的開頭至標準輸出中,預設head命令列印其相應檔案的開頭10行。
3.命令參數:
-q 隱藏檔案名稱
-v 顯示檔案名稱
-c<位元組> 顯示位元組數
-n<行數> 顯示的行數
4.使用執行個體:
執行個體1:顯示檔案的前n行
命令:
head -n 5 log2014.log
輸出:
[[email protected] test]# cat log2014.log
2014-01
2014-02
2014-03
2014-04
2014-05
2014-06
2014-07
2014-08
2014-09
2014-10
2014-11
2014-12
==============================
[[email protected] test]# head -n 5 log2014.log
2014-01
2014-02
2014-03
2014-04
2014-05[[email protected] test]#
執行個體2:顯示檔案前n個位元組
命令:
head -c 20 log2014.log
輸出:
[[email protected] test]# head -c 20 log2014.log
2014-01
2014-02
2014
[[email protected] test]#
執行個體3:檔案的除了最後n個位元組以外的內容
命令:
head -c -32 log2014.log
輸出:
[[email protected] test]# head -c -32 log2014.log
2014-01
2014-02
2014-03
2014-04
2014-05
2014-06
2014-07
2014-08
2014-09
2014-10
2014-11
2014-12[[email protected] test]#
執行個體4:輸出檔案除了最後n行的全部內容
命令:
head -n -6 log2014.log
輸出:
[[email protected] test]# head -n -6 log2014.log
2014-01
2014-02
2014-03
2014-04
2014-05
2014-06
2014-07[[email protected] test]#
tail命令
tail 命令從指定點開始將檔案寫到標準輸出.使用tail命令的-f選項可以方便的查閱正在改變的記錄檔,tail -f filename會把filename裡最尾部的內容顯示在螢幕上,並且不但重新整理,使你看到最新的檔案內容.
1.命令格式;
tail[必要參數][選擇參數][檔案]
2.命令功能:
用於顯示指定檔案末尾內容,不指定檔案時,作為輸入資訊進行處理。常用查看記錄檔。
3.命令參數:
-f 迴圈讀取
-q 不顯示處理資訊
-v 顯示詳細的處理資訊
-c<數目> 顯示的位元組數
-n<行數> 顯示行數
--pid=PID 與-f合用,表示在進程ID,PID死掉之後結束.
-q, --quiet, --silent 從不輸出給出檔案名稱的首部
-s, --sleep-interval=S 與-f合用,表示在每次反覆的間隔休眠S秒
4.使用執行個體:
執行個體1:顯示檔案末尾內容
命令:
tail -n 5 log2014.log
輸出:
[[email protected] test]# tail -n 5 log2014.log
2014-09
2014-10
2014-11
2014-12
==============================[[email protected] test]#
說明:
顯示檔案最後5行內容
執行個體2:迴圈查看檔案內容
命令:
tail -f test.log
輸出:
[[email protected] ~]# ping 192.168.120.204 > test.log &
[1] 11891[[email protected] ~]# tail -f test.log
PING 192.168.120.204 (192.168.120.204) 56(84) bytes of data.
64 bytes from 192.168.120.204: icmp_seq=1 ttl=64 time=0.038 ms
64 bytes from 192.168.120.204: icmp_seq=2 ttl=64 time=0.036 ms
64 bytes from 192.168.120.204: icmp_seq=3 ttl=64 time=0.033 ms
64 bytes from 192.168.120.204: icmp_seq=4 ttl=64 time=0.027 ms
64 bytes from 192.168.120.204: icmp_seq=5 ttl=64 time=0.032 ms
64 bytes from 192.168.120.204: icmp_seq=6 ttl=64 time=0.026 ms
64 bytes from 192.168.120.204: icmp_seq=7 ttl=64 time=0.030 ms
64 bytes from 192.168.120.204: icmp_seq=8 ttl=64 time=0.029 ms
64 bytes from 192.168.120.204: icmp_seq=9 ttl=64 time=0.044 ms
64 bytes from 192.168.120.204: icmp_seq=10 ttl=64 time=0.033 ms
64 bytes from 192.168.120.204: icmp_seq=11 ttl=64 time=0.027 ms
[[email protected] ~]#
說明:
ping 192.168.120.204 > test.log & //在後台ping遠程主機。並輸出檔案到test.log;這種做法也使用於一個以上的檔案監視。用Ctrl+c來終止。
執行個體3:從第5行開始顯示檔案
命令:
tail -n +5 log2014.log
輸出:
[[email protected] test]# cat log2014.log
2014-01
2014-02
2014-03
2014-04
2014-05
2014-06
2014-07
2014-08
2014-09
2014-10
2014-11
2014-12
==============================
[[email protected] test]# tail -n +5 log2014.log
2014-05
2014-06
2014-07
2014-08
2014-09
2014-10
2014-11
2014-12
==============================
轉自 每天一個linux命令
[Z]Linux less/more和head/tail命令