Filenames

帶有目錄的cat文件

  • June 14, 2021

cat文件時是否有顯示目錄/文件名的命令?

例如:假設 ./tmp 下有兩個文件 f1.txt 和 f2.txt

./tmp/f1.txt
./tmp/f2.txt 

然後當我執行cat ./tmp/*.txt時,只會顯示文件的內容。但是如何先顯示文件名,然後顯示內容?例如:

(The needed command):
./tmp/f1.txt:  
This is from f1.txt
and so on
./tmp/f1.txt:
This is from f2.txt ...

有命令嗎?(‘cat’ 似乎沒有選項來顯示文件名)

$ for file in ./tmp/*.txt; do echo "$file";  cat "$file"; done

-要麼-

$ find ./tmp -maxdepth 1 -name "*.txt" -print -exec cat "{}" \;

就像另一個想法一樣,嘗試tail -n +1 ./tmp/*.txt

==> file1.txt <==
<contents of file1.txt>

==> file2.txt <==
<contents of file2.txt>

==> file3.txt <==
<contents of file3.txt>

引用自:https://unix.stackexchange.com/questions/46524