tail
命令在 Linux 中主要用于查看文件的最后幾行
同時查看多個文件的末尾:
你可以在命令后面列出多個文件,用空格隔開。例如:
tail file1.txt file2.txt file3.txt
這將顯示 file1.txt
、file2.txt
和 file3.txt
文件的最后 10 行。
監視多個文件的更改:
要實時查看多個文件的更改,你可以使用 tail
命令結合 -f
(follow)選項。例如:
tail -f file1.txt file2.txt file3.txt
這將實時顯示 file1.txt
、file2.txt
和 file3.txt
文件的新內容。要停止監視,可以按 Ctrl + C
。
對多個文件執行相同的操作:
如果你需要對多個文件執行相同的操作,例如刪除最后 10 行,你可以先使用 tail
命令查看每個文件的最后幾行,然后根據輸出結果手動執行操作。例如:
tail -n 10 file1.txt > temp1.txt && tail -n 10 file2.txt > temp2.txt && tail -n 10 file3.txt > temp3.txt
這將創建三個臨時文件(temp1.txt
、temp2.txt
和 temp3.txt
),其中包含每個文件的最后 10 行。接下來,你可以對這些臨時文件執行相同的操作,例如刪除最后 10 行:
sed -i '10d' temp1.txt && sed -i '10d' temp2.txt && sed -i '10d' temp3.txt
最后,你可以刪除這些臨時文件:
rm temp1.txt temp2.txt temp3.txt
請注意,這些示例適用于類 Unix 系統(如 Linux 和 macOS)。在其他系統上,可能需要使用不同的命令或選項。