md5sum output append

Hi there,

I have 2 fairly simple lines I am running and both work as expected, but I am trying to append the two of them to output a single line.

The first command get the MAC times of each file :-

find /media/Vista/Garmin/PCBSMP2/ -type f -printf "%A+ (a) %p\n%T+ (m) %p\n%C+ (c) %p\n"

And the output is :

2009-10-25+16:01:05.8765787000 (a) /media/Vista/Garmin/PCBSMP2/eula_ENU.txt
2008-03-12+08:36:16.0000000000 (m) /media/Vista/Garmin/PCBSMP2/eula_ENU.txt
2009-10-25+16:01:05.8765787000 (c) /media/Vista/Garmin/PCBSMP2/eula_ENU.txt

The second command is simply calculates the md5sum of each file:-

find /media/Vista/Garmin/ -type f -print0 | xargs -0 md5sum

And the output is :-

12e6bdb52d0036cabd6f898def29dd6a  /media/Vista/Garmin/Training CenterSVE.dll
4b4f81c294b9a07479f4f4f8ff20e58c  /media/Vista/Garmin/gStart.exe
32da0f05975b3426c0ad76296abf3073  /media/Vista/Garmin/gStart_Lang.dll

So what I would like to do is have a single command that when run, not only outputs the MAC times but also appends the files md5sum.. like so

2009-10-25+16:01:05.8765787000 (a) /media/Vista/Garmin/PCBSMP2/eula_ENU.txt 12e6bdb52d0036cabd6f898def29dd6a
2008-03-12+08:36:16.0000000000 (m) /media/Vista/Garmin/PCBSMP2/eula_ENU.txt 12e6bdb52d0036cabd6f898def29dd6a
2009-10-25+16:01:05.8765787000 (c) /media/Vista/Garmin/PCBSMP2/eula_ENU.txt 12e6bdb52d0036cabd6f898def29dd6a

I realise that there will be duplication of the md5 hash 3 times for each file but thats cool.

Any help is appreciated.

I am not sure if this would help you but you can try:

command1 > file1.txt 

will write the output of command1 to file1.txt

Now,

command2 >> file1.txt

will append command2 output to the existing file1.txt

Hi dahlia84,
Unfortunately that method wont work as the second command would append the data to the end of the file, what I really need is one line of output with the outputs of both commands written together on one line... but thanks a million for your reply.

Oh sorry, I am not on a Unix machine now but can you please try this and tell me if it works?

(command1; command2 )> filename.txt

Hi dahlia84,
No that does the same thing again, runs command one first writes to a file, and the runs command 2.

My bad. I shall give this one last try. I do not want to waste you time though.

echo `(command1; command2 )` > filename.txt

Try using echo with -n option as well because that is the option which writes the output in one single line.

If this also does not work then I am sorry! :frowning:

not at all its cool.. I tried the last code but alas it dint pretty much the same again...
anyway not to worry, and thanks a million for giving it a shot or 3 :)...

all the best.

---------- Post updated at 03:10 PM ---------- Previous update was at 01:49 PM ----------

Hi, I managed to get this to work for me. Please close.

Here is how I got it to work.

find /media/Vista/Garmin/ -type f -printf "%A+ (a),%6s, %p\n%T+ (m),%6s, %p\n%C+ (c),%6s, %p\n" | while read line;   
do   
name="$(echo $line | cut -d, -f2)";   
hash="$(echo $name | md5sum | awk -F" " '{print $1}')";   
echo $line $hash;   
done

Running complex external utilities like cut and awk to operate on single lines is very wasteful. You might be spending more time loading and quitting these things than doing anything else. You can improve that a lot by using read's own built-in processing abilities.

find /path/to/files -type f -printf "%p %A+ %6s %T+ %C+\n" |
while read name atime size mtime ctime
do
        # sets $1 to md5sum output, $2 to filename, etc.
        set -- `md5sum $name`
        echo "$atime (a), $size, $name $1"
        echo "$mtime (m), $size, $name $1"
        echo "$ctime (c), $size, $name $1"
done

Or since you were using awk anyway, do it all in awk:

# -print pattern has no %p or newline.  then it runs md5sum which adds both 
find /path/to/files -type f -printf "%A+ %6s %T+ %C+ " -exec md5sum '{}' ';' |
awk '{
        print $1, "(a),", $2 ",", $6, $5;
        print $3, "(m),", $2 ",", $6, $5;
        print $4, "(c),", $2 ",", $6, $5;
}'

Hi Corona688,
Thanks a million for the reply. The reason I had to use cut was because it was the only way I could get it to work with spaces in the directory. No matter what I tried I couldn't get awk to work with md5sum correctly. You first method has problems with spaces as well., but your second method using awk seems to work although it does truncate the directory it does seem to generate the md5sum fine... and it seems faster than my method so I will have a better look at that one.

Thanks a million Corona688...

---------- Post updated at 06:04 AM ---------- Previous update was at 05:34 AM ----------

Hi Corona688,

Your second piece of code seems to be perfect. And unless I am missing something I don't need the awk part.

find /media/Vista/ProgramData/ -type f -printf "%A+ %T+ %C+ %6s " -exec md5sum '{}' ';'

It does create it all on one line, but to be honest this is probably for the best, meaning I need only have one line for each file I work with. Its a lot faster and only uses much less storage space (file or db)... I will test it a bit more, but think its the perfect answer. Thank you very much.

No, you don't need the awk part, all that was for was to turn it into three lines :b: