Combining (”Feathering”) Linux Output
Regarding an earlier article entitled, Using MD5SUM to Validate the Integrity of (Downloaded) Files, one reader, by the name of Faheem, wanted to know how to calculate the checksum for multiple files and show those checksums correlated together with other properties of those files. In other words, he wants to be able to issue the “ls -l” command to list all of the properties of all of the files in the current folder, and the do the same with the md5sum command, except that he wants the checksums to be side-by-side with the ls -l results, not following it.
For example, say that the current folder contains two files, Great_Gatsby.txt and Huck_Finn.asc.
We could issue the following compound-command:
ls -l; md5sum *.*
and the output would be something like this:
-rwx------+ Great_Gatsby.txt -rwx------+ Huck_Finn.asc 4823c9398e405df4db0320312c91d365 *Great_Gatsby.txt adb3ae8c055879e65b386b57f016055b *Huck_Finn.asc
But, what we need is it for the output to be more like this:
-rwx------+ Great_Gatsby.txt 4823c9398e405df4db0320312c91d365 -rwx------+ Huck_Finn.asc adb3ae8c055879e65b386b57f016055b
Well, Faheem, there is a way to do what you want. It’s a little complicated, but at least it all fits on one line as a longer compound command. It goes like this:
for i in $( ls ); do ls -l $i; md5sum $i; done | sed '$!N;s/n/ /'
or, if you are placing it in a shell script, then you may as well use multiple lines for clarity anyway:
for i in $( ls ); do ls -l $i; md5sum $i; done | sed '$!N;s/n/ /'
The first part, up to and including “done,” is a for-loop. It says, for every file in the current folder (which is determined by the “$( ls )” part), run the commands that appear between “do” and “done”. Since we said, “for i in…”, the letter i is going to be associated with the file name for each file found, one at a time. Anywhere between the “do” and “done” that the letter i appears, if preceded by a dollar sign, will be replaced by the actual name.
Thus, the “ls -l $i” command lists the file’s attributes for one of the files. The “md5sum $i” command then calculates the MD5 digest for that same file. Then, the loop repeats for the next file in the folder.
So, going back to our example file folder that contains two files, the output would be something like:
-rwx------+ Great_Gatsby.txt 4823c9398e405df4db0320312c91d365 *Great_Gatsby.txt -rwx------+ Huck_Finn.asc adb3ae8c055879e65b386b57f016055b *Huck_Finn.asc
Every other line being the ls -l result, and the other lines being the MD5 digest.
Now, all we need to do is merge the pairs of lines together. That’s where the sed (”stream editor”) command comes in. The pipe (|) symbol says feed the combined ls/md5sum output to sed. The sed argument (everything between the single-quotes) is a mini computer program that consists of 2 instructions, “$!N” and “s/\n/ /” which are separated by a semicolon. Without going into details, the “$!N” command tells sed to read and process two lines at a time, rather than one line at a time, as it normally does. The second command tells it to substitute the first (only) line break (represented by \n) with a single space. Since we are processing two lines at a time, it will find the line breaks within those pairs, but not between them.
For more information about the sed command (especially the “s/foo/bar/” substitution expression), see Automatic Spelling Correction with SED, as well as the sed project on SourceForge (http://sed.sourceforge.net).
Related articles:
- Using MD5SUM to Validate the Integrity of (Downloaded) Files
- Automatic Spelling Correction with SED
- Learning Linux Commands
- Convert Many Word Documents to ASCII At Once
- Use Linux Commands and Shell Scripts directly in Windows
- Tips for Launching CygWin
Read more: Linux

Post a Comment