Concatenate multiple files but include filename as section headers Concatenate multiple files but include filename as section headers unix unix

Concatenate multiple files but include filename as section headers


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.


I used grep for something similar:

grep "" *.txt

It does not give you a 'header', but prefixes every line with the filename.


This should do the trick as well:

find . -type f -print -exec cat {} \;

Means:

find    = linux `find` command finds filenames, see `man find` for more info.       = in current directory-type f = only files, not directories-print  = show found file-exec   = additionally execute another linux commandcat     = linux `cat` command, see `man cat`, displays file contents{}      = placeholder for the currently found filename\;      = tell `find` command that it ends now here

You further can combine searches trough boolean operators like -and or -or. find -ls is nice, too.