本文介紹Linux如何顯示文件指定行數的內容的方法,有下面幾種方法:
1、tail -n +/-數字 文件名
2、head -n 數字 文件名
3、sed -n "開始行,結束行p" 文件名
下面分別介紹這幾種方法
cd 到要文件所在目錄。cd到要查看指定行數內容的文件所在目錄,本文以SpecialVariable.sh文件為例,cd /home/test/shell/,如下圖

1、tail -n -數字 文件名。
tail -n -數字 文件名,表示查看文件的最后幾行,比如查看SpecialVariable.sh的最后5行,tail -n -5 SpecialVariable.sh,如下圖

2、tail -n +數字 文件名。
tail -n +數字 文件名,表示查看文件的某一行到最后一行,比如查看SpecialVariable.sh的第3行到最后一行,tail -n +3 SpecialVariable.sh,如下圖

3、head -n 數字 文件名。
head -n 數字 文件名,表示查看文件前幾行的內容,比如查看SpecialVariable.sh的前3行內容,head -n 3 SpecialVariable.sh,如下圖

4、sed -n "開始行,結束行p" 文件名。
sed -n "開始行,結束行p" 文件名,表示查看文件的開始行到結束行的內容,sed -n "5,9p" SpecialVariable.sh,如下圖

相關推薦:
LINUX系統怎么使用命令清理磁盤空間?
linux常怎么使用命令創建文件和文件夾?
Linux怎么使用壓縮和解壓到指定目錄的命令?