md5sum on a file with backslash in its name

Hi there,

I found something very weird!
Should I report that as a bug or is it me misusing the command?

I've got a file with a backslash in its name.
I know it's a horrible policy but it's not me.
The file came from a mac computer because this is a backup server.

Anyway, when using md5sum on the file, I get a very weird output.
Check it out :

ks354286:~# ls -l /tmp/file*
-rw-r--r-- 1 root root 2 2012-04-30 17:05 /tmp/file1
-rw-r--r-- 1 root root 2 2012-04-30 17:05 /tmp/file\2
ks354286:~# md5sum /tmp/file*
60b725f10c9c85c70d97880dfe8191b3  /tmp/file1
\3b5d5c3712955042212316173ccf37be  /tmp/file\\2

Do you have an explanation? a solution?

Thanks
Santiago

Weird, but the odd thing is it seems to work correctly

$ touch bad\\filename
$ md5sum bad\\filename
\d41d8cd98f00b204e9800998ecf8427e  bad\\filename
$ md5sum bad\\filename >bad_sums
$ md5sum -c bad_sums
bad\filename: OK

You have to prevent the shell from expanding the file name. Try not globbing the names, "*", rather put the name inside single quotes '/tmp/file\2'

We have C code to handle file names from a SAN directory of wierd PC shares (opendir, readdir) that encapsulates all of the filenames with single tics to be able to work with files on those directories. Assuming you do not want to rename the file.

@Skrynesaver
I see but I also use it that way:

$ mymd5sum=$(md5sum bad\\filename | cut -b-32)
$ echo "$mymd5sum"
\3b5d5c3712955042212316173ccf37b

The only way I found to circumvent the issue is to do the following:

$ mymd5sum=$(cat bad\\filename | md5sum | cut -b-32)
$ echo "$mymd5sum"
3b5d5c3712955042212316173ccf37be

@jim_mcnamara
The problem is not about globbing:

ks354286:~# md5sum '/tmp/file\2'
\3b5d5c3712955042212316173ccf37be  /tmp/file\\2

That why you should use -c option with file.

It's meant for your to avoid excessive coding and make a 3 shell line compare, use it :wink:

Hi Peasant,

I know the -c option quite well.
I use it in many different situations.
But I don't see how it can serve my current needs.

Does it create a correct output?
Does it allow me to save the md5sum in a variable?

Thanks for your help.
Santiago

md5sum is apparently attempting to warn you that the filename contains a backslash, by making the first character in the line be a backslash.

If the first character in a line is a backslash, thus, remove it, and carry on...

Documented "feature" of GNU md5sums:

not every one will do this:

$ busybox md5sum $'weird\nfile' test.ifs
d41d8cd98f00b204e9800998ecf8427e  weird
file
a1b7ff94b09092927c0cd0c59c94bd1e  test.ifs

So if creating an md5sum file to later check, this is OK, if you want md5sum of file for something else, redirection saves a cat.. sum=$(md5sum < "$file" | cut)

Thanks neutronscott,
I missed that explanation.
And thanks for

md5sum < "$file"

which is more concise than

cat "$file" | md5sum