-1

i need bash /shell/ script to md5sum hash all current directory tree files to one single .csv file like this :

"index.php","add95c73ccafa79b4936cf7236a074f4"
"logs/index.html","1c7b413c3fa39d0fed40556d2658ac73"

Thank You very much ;)

sehe
  • 374,641
  • 47
  • 450
  • 633

5 Answers5

3

You can try the command below, but it will only work if:

  • There are no " characters in your filenames
  • There are no newlines in your filenames

If that's OK, then this should work for you:

find . -type f -print0 | xargs -0 md5sum | \
    sed -r 's/^([0-9a-f]{32})  (.*)/"\2","\1"/'

Otherwise you'll need to do proper CSV quoting, in which case I'd suggest writing a short Python script to do this, using the csv module. For example:

#!/usr/bin/env python

import os, csv, sys, subprocess, hashlib

writer = csv.writer(sys.stdout, quoting=csv.QUOTE_ALL)

for directory, subdirectories, filenames in os.walk('.'):
    for filename in filenames:
        h = hashlib.md5()
        full_filename = os.path.join(directory, filename)
        with open(full_filename, 'rb') as f:
            while True:
                data = f.read(8096)
                if len(data) == 0:
                    break
                h.update(data)
        writer.writerow([h.hexdigest(), full_filename])
Mark Longair
  • 446,582
  • 72
  • 411
  • 327
2

Try:

find . -type f -print0 | xargs -0 md5sum | perl -pe 's/^(.*?)\s+(.*)$/"$2","$1"/'
> md5.csv
codaddict
  • 445,704
  • 82
  • 492
  • 529
0
md5sum * | awk '{ print "\"" $2 "\",\"" $1 "\"" }'
lhf
  • 70,581
  • 9
  • 108
  • 149
  • 1
    This would fail if the filename had a space it. – codaddict Nov 21 '11 at 12:19
  • Thank You! Work with correct output like this but i need it to process through all directories recursively, without this error : md5sum: templ: is directory "index.php","31fa74772d49e0c661ce8ee28f52b590" – Evan Pal McDan Nov 21 '11 at 12:31
0

This should work

perl -pne 's/^"(.*)","([0-9a-f]+)"$/$2 *$1/io' < input | md5sum -c
sehe
  • 374,641
  • 47
  • 450
  • 633
0

You can do it as shown below. Below, I am using the cksum utility which calculates the CRC checksum. You can use your utility which generates the MD5 checksum. You can redirect the output to a .csv file.

#!/bin/ksh

for file in $(find $1 -type f)
do
    filename=$(basename $file)
    checksum=$(cksum $file | cut -d " " -f 1)
    echo \"${filename}\",\"${checksum}\"
done
Drona
  • 6,886
  • 1
  • 29
  • 35
  • work perfect, but without path of file :( – Evan Pal McDan Nov 21 '11 at 12:42
  • If you need the complete file path as well in the output then check the following #!/bin/ksh for file in $(find $1 -type f) do checksum=$(cksum $file | cut -d " " -f 1) echo \"${file}\",\"${checksum}\" done – Drona Nov 22 '11 at 09:37