A quick copy-paste UNIX trick

I ran across this trick a long time ago and now use it regularly, but it occurred to me today that I don't remember seeing anyone else mention it. So, just in case it helps someone else:

I often find myself wanting to copy and paste the contents of several small files into an email message, particularly when explaining Debian packaging. I want to show all the file contents with little headers showing which lines come from each file. This gets tedious if I actually do this file-by-file. The obvious approaches, from including them into my editor to cat'ing them one-by-one and pasting them, are tedious and require writing the headers by hand. Obviously, cat doesn't help, since it obliterates the difference between the files.

What does help, however, is good old head (or tail, for that matter). People normally use those utilities with standard input or a single file, but you can give them multiple files, and they do just about exactly what you want. Consider this shell transcript:

    % head -n 20 *.install
    ==> liblog4shib-dev.install <==
    debian/log4shib-config.1 usr/share/man/man1

    ==> liblog4shib-doc.install <==
    usr/share/doc/log4shib-*/api/html usr/share/doc/liblog4shib-doc

    ==> liblog4shib1.install <==

Just pick some value for -n that's longer than any of the files (I normally want to show the contents of small files anyway), run head on all the files whose contents you want to show, and then you can cut and paste its full output directly into your email message.

Bonus: This behavior is even guaranteed by POSIX (something that I didn't realize until I checked while writing this post).

Posted: 2014-01-02 19:20 — Why no comments?

Last spun 2014-01-04 from thread modified 2014-01-03