Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux: compute a single hash for a given folder & contents?

Tags:

linux

bash

hash

Surely there must be a way to do this easily!

I've tried the Linux command-line apps such as sha1sum and md5sum but they seem only to be able to compute hashes of individual files and output a list of hash values, one for each file.

I need to generate a single hash for the entire contents of a folder (not just the filenames).

I'd like to do something like

sha1sum /folder/of/stuff > singlehashvalue

Edit: to clarify, my files are at multiple levels in a directory tree, they're not all sitting in the same root folder.

like image 698
Ben L Avatar asked Oct 13 '22 21:10

Ben L


People also ask

How do I find the hash value of a folder?

Example 3: Retrieve the hash of a folder and its contents using recursion. This command uses the Get-DfsrFileHash cmdlet to retrieve the hash of a folder and the hashes for the individual files in the folder. The command also uses the Get-ChildItem cmdlet to recursively find all files and folders in the path.


2 Answers

One possible way would be:

sha1sum path/to/folder/* | sha1sum

If there is a whole directory tree, you're probably better off using find and xargs. One possible command would be

find path/to/folder -type f -print0 | sort -z | xargs -0 sha1sum | sha1sum

And, finally, if you also need to take account of permissions and empty directories:

(find path/to/folder -type f -print0  | sort -z | xargs -0 sha1sum;
 find path/to/folder \( -type f -o -type d \) -print0 | sort -z | \
   xargs -0 stat -c '%n %a') \
| sha1sum

The arguments to stat will cause it to print the name of the file, followed by its octal permissions. The two finds will run one after the other, causing double the amount of disk IO, the first finding all file names and checksumming the contents, the second finding all file and directory names, printing name and mode. The list of "file names and checksums", followed by "names and directories, with permissions" will then be checksummed, for a smaller checksum.

like image 169
Vatine Avatar answered Oct 16 '22 09:10

Vatine


  • Use a file system intrusion detection tool like aide.

  • hash a tar ball of the directory:

    tar cvf - /path/to/folder | sha1sum

  • Code something yourself, like vatine's oneliner:

    find /path/to/folder -type f -print0 | sort -z | xargs -0 sha1sum | sha1sum

like image 42
David Schmitt Avatar answered Oct 16 '22 09:10

David Schmitt