bashshellmd5sum

MD5sum of filename containing white spaces


I'm trying to get the md5sum of every file of a directory, in this format :

hashcode  dir/path/to/file

I'm using :

md5sum $(find /path/to/dir -type f | sort)

but this doesn't work if the file as a white space in its name.

And adding apostrophes: md5sum "$(find /path/to/dir -type f | sort)" doesn't seems to solve the problem (it only print the path, not the md5 anymore).


Solution

  • If the folder has only one layer deep then simply let the shell expand file names like this

    md5sum *
    

    If there are more layers then you can enable globstar and the output will still be sorted as expected

    shopt -s globstar
    md5sum ** 2>/dev/null
    

    Alternatively use find directly

    find -type f -print0 | sort -z | xargs -0 md5sum