How to store the output of a terminal command in a text files but prepend an additional line

I want to run this command in a folder to generate the audio checksum for all FLAC files, like so:

$ metaflac --show-md5sum *.flac > flacsum.txt

It generated a flacsum.txt file that looks like so:

01 - First Track.flac:0d10049cfb5e675e2adff811d6f918a3
02 - Second Track.flac:3adab834d1508e4cd3a72551eb5f1ae4
03 - Third Track.flac:747b61a2004d1b278591a6f3fe27b1bd

That's fine, however I would like to do the same thing but prepend a title in the "flacsum.txt" file plus a blank new line below it, before the output goes into the file, like so:

MD5 hashes generated using metaflac --show-md5sum

01 - First Track.flac:0d10049cfb5e675e2adff811d6f918a3
02 - Second Track.flac:3adab834d1508e4cd3a72551eb5f1ae4
03 - Third Track.flac:747b61a2004d1b278591a6f3fe27b1bd

Is a way to accomplish that?


Solution 1:

You can redirect output of many commands, each on its own:

echo "MD5 hashes generated using metaflac --show-md5sum" > flacsum.txt
echo >> flacsum.txt
metaflac --show-md5sum *.flac >> flacsum.txt

Note all but the first command use the >> operator which appends to the file.

Or better group the commands and redirect output from the whole group:

{ echo "MD5 hashes generated using metaflac --show-md5sum"
echo
metaflac --show-md5sum *.flac
} > flacsum.txt