The md5sum
program is used to generate MD5 hashes of files. These can be used later to determine if the file has been modified or corrupted. I usually generate md5sums for all my backed up files and check the sums occasionally. If I find any corrupted files I can restore them from other backups. It's easiest to run md5sum on an entire directory recursively so that all files have an md5sum produced.
To run md5sum recursively for the current directory, run the following command from a Bash shell:
find . -type f -exec md5sum {} \;
The find
command finds all files (-type f
) in the current directory recursively and then executes md5sum
for each of them. To only process files in the current directory, use -maxdepth 1
with find
. You can change the value of -maxdepth
as needed to include more levels of subdirectories. Omitting it will recurse through all levels of subdirectories.
You can save the MD5 results by redirecting the output to a file:
find . -type f -exec md5sum {} \; > md5sums.txt
When output is redirected, an empty md5sums.txt
file is created first. The find command is then run for each file, including the md5sums.txt
file that is in the directory. The MD5 hash for that file is going to be wrong because the file is still being generated when the hash for it is calculated. Therefore, you should edit the md5sums.txt
file and remove the entry for itself. You can use a grep
command for that to save time, e.g.:
grep -v \.\/md5sums\.txt md5sums.txt > md5sums_fixed.txt
The hashes of files can be checked by the following:
md5sum -c md5sums.txt
Discussion