cat command – Display File Names with Contents

catfilenames

Is there a command to show the directory/file name when cat files?

For example: assume two files f1.txt and f2.txt are under ./tmp

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

Then when I do cat ./tmp/*.txt, only the content of files will be shown. But how to firstly show the file name, then the content?, e.g.:

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

Is there a command to do it? (There seems to be no option for 'cat' to show the file names)

Best Answer

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

-or-

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