How many times have you downloaded a boot image or compressed ISO file and written it to it to an SD card or CD without checking the integrity of the file?
You really should – and it isn’t very hard.
Using a hashing algorithm like SHA-1 or MD5 allows you to verify the integrity the file by computing it’s SHA-1 or MD5 checksum and allowing you to compare it to a known value. Some sites will show the checksums on the download page and others will provide a separate checksum file – usually when there are multiple files.
To generate the SHA-1 checksum for a file (or group of files).
If the download page shows the checksum then when you have downloaded your file all you need to do is check that this matches your version. You can also save the checksum information in a file and use it to verify that the files have not been modified later.
To generate the MD5 checksum for a file (or group of files).
You can save the MD5 checksums by redirecting the output from the command to a file.
Using the saved checksums you can validate the integrity of the files.
If you update the files and then updating the checksum file then when you validate the files you will get an error that indicates that the checksum file was modified, but this should be expected as you just changed it!
To overcome the problems above you could simply delete the checksum file before regenerating the checksums, but what if you don’t want to generate checksums for other files like temporary or backup files?
To do this you can use ‘find’ to select which files will be used, or in this case ignored, when generating the checksums. Although this is a much longer command than just using ‘md5sum’ by itself it has the advantage that when checking the integrity of the files later you will not get an error message, and you can also modify it to exclude other types of file as well.
If you don’t specify the depth then you can generate a single checksum file for all the files in the current folder and all sub-folders, which is something that the ‘md5sum’ or ‘sha1sum’ commands can’t do by themselves.
Since all this command does is to use ‘find’ to select the files before generating the checksum, you have the flexibility to use any of the other features of the ‘find’ command. So if you want you can also exclude hidden files and folders you can do so by adding the additional qualifier highlighted in red which causes find to ignore any files with path names beginning with a ‘.’ (this will include ALL files in hidden folders).
If you just want to generate checksums for a file of a particular type then you don’t need to explicitly ignore any files (unless you don’t want to include hidden files or folders).
You can also use find to search for every folder before generating the checksum – this allows you to create a seperate checksum file in the current folder and every sub folder with one command.
You can exclude files in hidden folders by excluding them from the directory search, but by itself this will not exclude hidden files (unless in a hidden folder).
Note – You must specify that you want to exclude all folders that match specified path after specifying the type.
To exclude both hidden folders and hidden files you need to specify both explicitly.
This has given me a few ideas for other useful things I can do using find – but they will have to wait for another day!