cat files with directory

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)

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Just as another idea, try tail -n +1 ./tmp/*.txt

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

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

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

Method 2

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

-or-

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

Method 3

Not enough reputation point to add a comment to second answer

tail -v -n +1 /tmp/*.txt

will display filename even if there is only one file.

Method 4

Not exactly what you asked for, but you can prefix each line with the filename:

$ grep ^ ./tmp/*.txt
./tmp/f1.txt: this is from f1.txt
./tmp/f1.txt: blaa, blaa, blaa...
./tmp/f1.txt: blaa, blaa, blaa...
./tmp/f2.txt: this is from f2.txt
./tmp/f2.txt: blaa, blaa, blaa...
./tmp/f2.txt: blaa, blaa, blaa...

It will be tough to do much better than this without resorting to a little scripting.

Method 5

You could easily write a tiny script doing just that,

for f in "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="587c18">[email protected]</a>" do; echo "This is from $f"; cat -- "$f"; done

Method 6

grep . *.txt

Matches all lines and also shows file names

Method 7

find . -name '*' -execdir cat '{}' ;

When a directory is passed to cat, you’ll see something like:

cat: ./chapter_01: Is a directory

Immediately following, the find will cat the contents of that directory.

Method 8

cat is (intentionally) an extremely simple command that just reads one file stream and dumps it to another (with a few basic formatting options). It’d be fairly easy to create a utility based on cat that did provide the filename, but standard versions won’t do this — probably because it’s easy to replicate with other commands.

If you want to examine the pages manually you could use ‘less’. This will give you the filename at the end of every file, in the format: ‘foo.txt (file 1 of 100) (END) – Next: bar.txt).


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x