Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate multiple files but include filename as section headers

I would like to concatenate a number of text files into one large file in terminal. I know I can do this using the cat command. However, I would like the filename of each file to precede the "data dump" for that file. Anyone know how to do this?

what I currently have:

file1.txt = bluemoongoodbeer  file2.txt = awesomepossum  file3.txt = hownowbrowncow  cat file1.txt file2.txt file3.txt 

desired output:

file1  bluemoongoodbeer  file2  awesomepossum  file3  hownowbrowncow 
like image 977
Nick Avatar asked May 06 '11 21:05

Nick


1 Answers

Was looking for the same thing, and found this to suggest:

tail -n +1 file1.txt file2.txt file3.txt 

Output:

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

If there is only a single file then the header will not be printed. If using GNU utils, you can use -v to always print a header.

like image 79
DS. Avatar answered Oct 10 '22 04:10

DS.