Add, verify or remove a checksum as filename’s suffix

The Bash script name_hash is part of Bash AVpres, released under a 3-Clause BSD License and provided “as is” without warranty or support of any kind. The programm can perform three different tasks:

  • add a checksum to the filename:
    path/to/filename_checksum.extension
  • verify if the integrated checksum matches the file’s content;
  • remove the checksum from the filename.

The script dismisses the necessity of handling additional manifest files containing the checksums. It works with both a single file or all the files inside a folder recursively. Note that this is fully reversible and therefore adapting to an evolving archive’s policy is not an issue.

Documentation

A short help message is embedded into the script:

$ name_hash -h
Usage:
  name_hash (-a|-v|-r) <input_path>
  name_hash (-h|-x)
Options:
  -a  add checksum to filename
  -v  verify if file's content and checksum matches
  -r  remove checksum from filename
  -h  this help
  -x  advanced options with their default arguments
Dependency:
  one of md5sum (default), sha1sum, sha256sum or sha512sum
See also:
  man name_hash
  https://avpres.net/Bash_AVpres/
About:
  Abstract: Add, verify or remove a checksum as filename's suffix.
  Version:  2020-11-07
$

The detailed manual page is installed together with the script (man name_hash) and is available also as a PDF file.

Example

Create an empty file add the checksum to the filename
$ touch my_file.txt
$ name_hash -a my_file.txt
my_file_d41d8cd98f00b204e9800998ecf8427e.txt
$
Verify if the content matches the checksum
$ name_hash -v my_file_d41d8cd98f00b204e9800998ecf8427e.txt
my_file_d41d8cd98f00b204e9800998ecf8427e.txt
$
Modify the last digit of the checksum and verify again
$ mv my_file_d41d8cd98f00b204e9800998ecf8427e.txt my_file_d41d8cd98f00b204e9800998ecf8427f.txt
$ name_hash -v my_file_d41d8cd98f00b204e9800998ecf8427f.txt
Error: 'my_file_d41d8cd98f00b204e9800998ecf8427f.txt' doesn't match.
$
Remove the checksum from the filename
$ name_hash -r my_file_d41d8cd98f00b204e9800998ecf8427f.txt
my_file.txt
$
The details can be retrieved from the log files
$ ls -l /tmp/AVpres
total 32
-rw-------   1 test  wheel  316 13 Sep 13:33 name_hash.3FelhJDcYm
-rw-------   1 test  wheel  321 13 Sep 13:31 name_hash.C1UmvPJGj3
-rw-------   1 test  wheel  423 13 Sep 13:32 name_hash.bv6BRdM2hs
-rw-------   1 test  wheel  285 13 Sep 13:31 name_hash.imSwjwA3jC
$ cat /tmp/AVpres/name_hash.bv6BRdM2hs
[2020-09-13 11:32:15 UTC] name_hash 2020-09-01
[2020-09-13 11:32:15 UTC] /usr/local/bin/name_hash -v my_file_d41d8cd98f00b204e9800998ecf8427f.txt
[2020-09-13 11:32:15 UTC] START
[2020-09-13 11:32:15 UTC] verify checksum
1c1
< d41d8cd98f00b204e9800998ecf8427f
---
> d41d8cd98f00b204e9800998ecf8427e
[2020-09-13 11:32:15 UTC] Error: 'my_file_d41d8cd98f00b204e9800998ecf8427f.txt' doesn't match.
[2020-09-13 11:32:15 UTC] END
$

2020–11–08