head命令用於顯示文件的開頭的內容。在默認狀況下,head命令顯示文件的頭10行內容。html
tail命令用於顯示文件的結尾的內容。在默認狀況下,taild命令顯示文件的後10行內容。shell
-c, --bytes=[-]K print the first K bytes of each file; with the leading `-', print all but the last K bytes of each file -n, --lines=[-]K print the first K lines instead of the first 10; with the leading `-', print all but the last K lines of each file -q, --quiet, --silent never print headers giving file names -v, --verbose always print headers giving file names --help display this help and exit --version output version information and exit
-c, --bytes=K output the last K bytes; alternatively, use -c +K to output bytes starting with the Kth of each file -f, --follow[={name|descriptor}] output appended data as the file grows; -f, --follow, and --follow=descriptor are equivalent -F same as --follow=name --retry -n, --lines=K output the last K lines, instead of the last 10; or use -n +K to output lines starting with the Kth --max-unchanged-stats=N with --follow=name, reopen a FILE which has not changed size after N (default 5) iterations to see if it has been unlinked or renamed (this is the usual case of rotated log files). With inotify, this option is rarely useful. --pid=PID with -f, terminate after process ID, PID dies -q, --quiet, --silent never output headers giving file names --retry keep trying to open a file even when it is or becomes inaccessible; useful when following by name, i.e., with --follow=name -s, --sleep-interval=N with -f, sleep for approximately N seconds (default 1.0) between iterations. With inotify and --pid=P, check process P at least once every N seconds. -v, --verbose always output headers giving file names --help display this help and exit --version output version information and exit
head :顯示文件的前幾行,默認10行 head -n 2 /home/omd/h.txt ==>head -n 3 == head -3 能夠直接跟行數 cat h.txt | grep -v "hello" 過濾掉特定字符串,效率低,由於有管道 ==>grep -v "hello" h.txt 能夠直接跟文件名,效率快 tail: 顯示文件最後幾行,默認10行 tail -10 /home/omd/h.txt 顯示最後10行 tail -f /home/omd/h.txt 實時跟蹤文件, 若是文件不存在,則終止 tail -F /home/omd/h.txt 若是文件不存在,會繼續嘗試 head -30 /home/omd/h.txt | tail -11 /home/omd/h.txt 輸出一個文件的20-30行顯示文件的前n行 head -n 5 log2014.log輸出文件除了最後n行的所有內容 head -n -6 log2014.log從第5行開始顯示文件 tail -n +5 log2014.log 【顯示文件的所有內容】 tail -n 5 log2014.log 【只顯示最後的5行】 根more/less差很少