Eric Anderson
Eric Anderson

Reputation: 366

How to change delimiter of md5sum command output

My code looks like this:

for i in `find` ; 
do
    if [ -f $i ]; then
        if [ "$i" != "./ex.sh" ]; then
            md5sum $i >> checksums.txt;
        fi
    fi
done

The problem is I want to use awk on the file later with a "|" as the delimiter. However I don't know how to append to file checksums.txt with a "|" between the md5sum and the $i. Thanks

Upvotes: 2

Views: 892

Answers (3)

Mechanical snail
Mechanical snail

Reputation: 30637

md5sum (at least the version in GNU coreutils) doesn't provide options for controlling the output format. You should change your awk script to treat the characters 0...15 as the md5sum, and characters 18 to the end of the line as the filename. If you really need the particular format, you should parse the output of md5sum. For example:

user@host:~$ md5sum "/dev/null" | python -c 'import sys; s = sys.stdin.read(); print s[0:32] + "|" + s[34:],'
d41d8cd98f00b204e9800998ecf8427e|/dev/null

Also, you should be enclosing the argument to md5sum in quotes (md5sum "$i"). As written, the script will fail if there are any filenames containing spaces or special characters.

Upvotes: 1

Kent
Kent

Reputation: 195039

Including this question, I've read at least 3 questions from you regarding the same problem. I guess you want to find all duplicated files under some directory, right?

then you can try the one liner below, it would save your later looping or double looping and awk processing:

find {what you want to find comes here} -exec md5sum '{}' \; | sort | uniq -d -w 33

it will list duplicated md5sum and file names.

if my guessing was wrong, just ignore my answer.

Upvotes: 1

jman
jman

Reputation: 11606

Use this:

md5sum  $i | tr -s " " | tr " " "|" >> checksums.txt

Upvotes: 1

Related Questions