We all know cat can show multiple file contents easily, for example:
$ ls *.txt 1.txt 2.txt $ cat *.txt content of 1.txt content of 2.txt
But the problem is you don’t know what from which file, and sometimes you really want to see the contents with filename rather than being concatenated into a big chuck of text as that’s what cat does and it can’t help you with that.
Some answers give good solutions to it.
Solutions
1 more
Using more probably is the easiest way and simply memorable:
$ more *.txt | cat :::::::::::::: 1.txt :::::::::::::: content of 1.txt :::::::::::::: 2.txt :::::::::::::: content of 2.txt
2 head and tail
I could never remember the correct meaning when using -n -#, I always have to try it out first.
$ head -n -0 *.txt ==> 1.txt <== content of 1.txt ==> 2.txt <== content of 2.txt $ tail -n +1 *.txt ==> 1.txt <== content of 1.txt ==> 2.txt <== content of 2.txt
3 find
The benefit of using find is you can filter and find files deep in, or some funky patterns.
find . -name \*.txt -print -exec cat {} \; ./2.txt content of 2.txt ./1.txt content of 1.txt
4 Script
Of course, you can always come up with a simple one-liner, but if you need to do this task, a simpler way definitely is the best.
$ for f in *.txt; do echo "Filename: $f"; cat "$f"; done Filename: 1.txt content of 1.txt Filename: 2.txt content of 2.txt
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.