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 program can perform three different tasks:

  • add a checksum to the filename;
  • verify if the integrated checksum matches the file’s content;
  • remove the checksum from the filename.

The format is: path/to/filename_checksum.extension

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:
  xxhsum, md5sum, sha1sum, sha256sum, sha512sum and crc32
See also:
  man name_hash
  https://avpres.net/Bash_AVpres/
About:
  Abstract: Add, verify or remove a checksum as filename's suffix.
  Version:  2023-03-26

$

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

Example

Create an empty file add the checksum to the filename
$ touch my_file.txt
$ name_hash -a my_file.txt
my_file_99aa06d3014798d86001c324468d497f.txt
$
Verify if the content matches the checksum
$ name_hash -v my_file_99aa06d3014798d86001c324468d497f.txt
my_file_99aa06d3014798d86001c324468d497f.txt
$
Modify the last digit of the checksum and verify again
$ mv my_file_99aa06d3014798d86001c324468d497f.txt my_file_99aa06d3014798d86001c324468d497e.txt
$ name_hash -v my_file_99aa06d3014798d86001c324468d497e.txt
Error: 'my_file_99aa06d3014798d86001c324468d497e.txt' doesn't match.
$
Remove the checksum from the filename
$ name_hash -r my_file_99aa06d3014798d86001c324468d497e.txt
Remove checksum from filename? (y|N) y
my_file.txt
$
The details can be retrieved from the log files
$ ls -l /tmp/AVpres
total 32
-rw-------  1 test  wheel  285 19 Dec 16:31 name_hash.K2FmkoRc57
-rw-------  1 test  wheel  337 19 Dec 16:33 name_hash.LERg6r0oFx
-rw-------  1 test  wheel  288 19 Dec 16:36 name_hash.W1zsiD3GeG
-rw-------  1 test  wheel  530 19 Dec 16:34 name_hash.YaUuYTwOwS
$ cat /tmp/AVpres/name_hash.YaUuYTwOwS
[2020-12-19 15:34:18 UTC] name_hash 2020-12-19
[2020-12-19 15:34:18 UTC] /usr/local/bin/name_hash -v my_file_99aa06d3014798d86001c324468d497e.txt
[2020-12-19 15:34:18 UTC] START
[2020-12-19 15:34:18 UTC] verify checksum
Trying 'XXH128'
1c1
< 99aa06d3014798d86001c324468d497e
---
> 99aa06d3014798d86001c324468d497f
Trying 'MD5'
1c1
< 99aa06d3014798d86001c324468d497e
---
> d41d8cd98f00b204e9800998ecf8427e
[2020-12-19 15:34:18 UTC] Error: 'my_file_99aa06d3014798d86001c324468d497e.txt' doesn't match.
[2020-12-19 15:34:18 UTC] END
$

2023-03-26