Why md5sum is prepending "\" in front of the checksum when finding the checksum of a file with "\" in the name?
$ md5sum /tmp/test\\test \d41d8cd98f00b204e9800998ecf8427e /tmp/test\\test
The same is noted for every other utility.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
This is documented, for Coreutils’
If file contains a backslash or newline, the line is started with a backslash, and each problematic character in the file name is escaped with a backslash, making the output unambiguous even in the presence of arbitrary file names.
(file is the filename, not the file’s contents).
sha1sum, and the various SHA-2 tools behave in the same way as
sum is only provided for backwards-compatibility (and its ancestors don’t produce quoted output), and
cksum is specified by POSIX and doesn’t allow this type of output.
This behaviour was introduced in November 2015 and released in version 8.25 (January 2016), with the following
md5sumnow ensures a single line per file for status on standard output, by using a '\' at the start of the line, and replacing any newlines with '\n'. This also affects
The backslash at the start of the line serves as a flag: escapes in filenames are only processed if the line starts with a backslash. (Unescaping can’t be the default behaviour: it would break sums generated with older versions of Coreutils containing
\n in the stored filenames.)
Stephen Kitt's answer covers the what and I will try to cover why this change was implemented. First, someone observed that a filename containing newlines1 could result in ambiguous output. For example, consider this output:
d41d8cd98f00b204e9800998ecf8427e foo 25af89c92254a806b2e93fffd8ac1814 bar
Does this mean there were two files
bar, or only one file whose filename is
"foo\n25af89c92254a806b2e93fffd8ac1814 bar"? Granted, this latter possibility is highly unlikely, but it is possible. To resolve the ambiguity the developers chose to escape newlines with a backslash (
\). The output then becomes distinguishable. However, then there is a further ambiguity:
Does this file's name contain a newline, or a backslash followed by an
n? To resolve this we need to escape backslashes too, so that the latter case becomes:
Finally, they elected to prepend each output line which contains such escapes with a
\\ to make it easy for a parser to detect whether escaping has been done. Presumably this was done to allow parsers to handle output both from escaping versions of
md5sum and from non-escaping versions (non-GNU). The flag also means that "costly" un-escaping does not need to be done when not necessary. You can see an example of this parsing in action in
md5sum.c itself (line 382 in the linked version).
1 By newline I mean the character
\n which is sometimes also specifically referred to as a linefeed or LF; see