Green and Blue g
Green and Blue g

Reputation: 21

Is there a way of making an md5sum of all files in subfolders?

I have several exports of telegram data and I would like to calculate the md5 and sha256 hash of all files but it only calculates those in the root directory

$ md5sum `ls` > hash.md5

md5sum: chats: Is a directory
md5sum: css: Is a directory
md5sum: images: Is a directory
md5sum: js: Is a directory
md5sum: lists: Is a directory
md5sum: profile_pictures: Is a directory

This is in the output file

7e315ce28aa2f6474e69a7b7da2b5886  export_results.html
66281ec07a2c942f50938f93b47ad404  hash.md5
da5e2fde21c3e7bbbdb08a4686c3d936  ID.txt

There is a way to get something like this out?

5750125fe13943f6b265505b25828400  js/script.js

Sorry for my english

Upvotes: 1

Views: 6605

Answers (5)

abu_bua
abu_bua

Reputation: 1647

On zsh use the following command:

md5sum */**(.) > md5sum.txt

And to test the file type

md5sum -c md5sum.txt    

Upvotes: 0

Du-Lacoste
Du-Lacoste

Reputation: 12807

You could use the following to achieve the task.

find . -type f -exec md5sum {} + >> log_checksum.txt

#. (dot) can be replaced with the location you need to run the command

#curly braces {} to mention, filenames of the command output will be passed to the md5sum command as arguments

#(+) plus sign is added to make sure files are passed as arguments to a single md5sum command, to prevent running a separate md5sum process for each file

Upvotes: 0

M. Nejat Aydin
M. Nejat Aydin

Reputation: 10133

Alternatively, you can use find with -exec option:

find topdir -type f -exec md5sum {} + > MD5SUMS

Replace the topdir with the actual directory name, or drop it if you want to work on the current directory (and its subdirectories, if any). This will only compute the checksums of regular files (so, no "md5sum: something: Is a directory" errors), and won't suffer from the "argument list too long" problem.

Upvotes: 3

Marco
Marco

Reputation: 902

A tool which helps, but might not be installed by default, is hashdeep. hashdeep does it directly and has some more advantages, e.g. binary is available for Windows, too.

Your question would be answered using hashdeep with this command:

hashdeep -c md5,sha256 -r -o f -l . > hash.md5

This calculates md5 and sha256 of all files in all subdirs with one command.

Creating md5 and sha256 together might be faster due to caching effects of the files. Additionally the command has an option to use multiple threads, which could fasten up the task with multi-core CPUs and fast disks.

Upvotes: 5

Cyrus
Cyrus

Reputation: 88989

With bash:

shopt -s globstar
md5sum ** >/tmp/hash.md5

Ignore errors of the kind: md5sum: foobar: Is a directory

From man bash:

globstar: If set, the pattern ** used in a pathname expansion context will match all files and zero or more directories and subdirectories. If the pattern is followed by a /, only directories and subdirectories match.

Upvotes: 3

Related Questions