How to use the command 'b2sum' (with examples)
The ‘b2sum’ command is used to calculate BLAKE2 cryptographic checksums for files. BLAKE2 is a cryptographic hash function that is faster than MD5 and SHA-1, and offers a stronger level of security. The ‘b2sum’ command allows you to calculate checksums for files, save them to a file, verify checksums from a file, and more.
Use case 1: Calculate the BLAKE2 checksum for one or more files
Code:
b2sum path/to/file1 path/to/file2 ...
Motivation: Calculating the checksum for one or more files can be helpful for verifying the integrity of the files. You may want to use this feature when transferring files over a network or copying files to multiple locations to ensure that they have not been tampered with or corrupted.
Explanation: The ‘b2sum’ command is followed by the paths of one or more files for which you want to calculate the checksums. The command will calculate the BLAKE2 checksum for each file and display the results on the terminal.
Example output:
83a84f1191f55f982dff6b8e8ef7975638660cb2 path/to/file1
4323bed06c69eb12e78e2e0b2fdc3c1aafe29a6e path/to/file2
Use case 2: Calculate and save the list of BLAKE2 checksums to a file
Code:
b2sum path/to/file1 path/to/file2 ... > path/to/file.b2
Motivation: Saving the list of BLAKE2 checksums to a file can be useful for later verification. This way, you can calculate the checksums once and store them in a separate file. Later, you can verify the integrity of the files by comparing their checksums with the saved file.
Explanation:
After calculating the checksums for the specified files, you can use the output redirection operator (>
) to save the checksums to a file. The file mentioned after the redirection operator will be created or overwritten, and it will contain the list of BLAKE2 checksums.
Example output:
If the files ‘file1’ and ‘file2’ have the following checksums:
- file1: 83a84f1191f55f982dff6b8e8ef7975638660cb2
- file2: 4323bed06c69eb12e78e2e0b2fdc3c1aafe29a6e
The content of the ‘file.b2’ will be:
83a84f1191f55f982dff6b8e8ef7975638660cb2 path/to/file1
4323bed06c69eb12e78e2e0b2fdc3c1aafe29a6e path/to/file2
Use case 3: Calculate a BLAKE2 checksum from stdin
Code:
command | b2sum
Motivation: This use case can be helpful when you want to calculate the BLAKE2 checksum for the output of a command without saving it to a file. Instead of redirecting the output to a file, you can directly calculate and display the checksum on the terminal.
Explanation:
In this use case, you can pipe the output of a command to the ‘b2sum’ command using the pipe operator (|
). The input data will be read from the standard input (stdin
) and the ‘b2sum’ command will calculate the BLAKE2 checksum for it.
Example output:
c3c3baf11d5b2ab0096800aa65aa512469c157fb # Output of the command
Use case 4: Read a file of BLAKE2 sums and filenames and verify all files have matching checksums
Code:
b2sum --check path/to/file.b2
Motivation: This use case is beneficial when you want to verify the integrity of files by comparing their checksums with a saved checksum file. It allows you to ensure that the files have not been modified or corrupted after the checksums were calculated.
Explanation: The ‘–check’ option is used to indicate that the ‘b2sum’ command should perform the checksum verification. The command is followed by the path to the file containing the BLAKE2 checksums. The ‘b2sum’ command will read the file and compare the checksums with the corresponding files. If any checksum does not match, an error message will be displayed.
Example output:
path/to/file1: OK
path/to/file2: OK
Use case 5: Only show a message for missing files or when verification fails
Code:
b2sum --check --quiet path/to/file.b2
Motivation: This use case is useful when you want to check the integrity of files without seeing any unnecessary output. By using the ‘–quiet’ option, you can suppress the normal output and only receive messages when there are missing files or verification failures.
Explanation: The ‘–check’ option is used to indicate that the ‘b2sum’ command should perform the checksum verification. The ‘–quiet’ option is used to suppress the normal output. If there are any missing files or checksum verification failures, a message will be displayed.
Example output:
Error: file2: FAILED
Use case 6: Only show a message when verification fails, ignoring missing files
Code:
b2sum --ignore-missing --check --quiet path/to/file.b2
Motivation: This use case is helpful when you want to verify the checksums of a large number of files and only want to see messages for verification failures, without being notified about missing files. Ignoring the missing files can save you time and prevent unnecessary clutter in the output.
Explanation: The ‘–check’ option is used to indicate that the ‘b2sum’ command should perform the checksum verification. The ‘–ignore-missing’ option is used to ignore any missing files and continue the verification process. The ‘–quiet’ option is used to suppress the normal output. If there are any checksum verification failures, a message will be displayed.
Example output:
Error: file2: FAILED
Conclusion:
The ‘b2sum’ command is a powerful tool for calculating and verifying BLAKE2 cryptographic checksums. It provides various options for different use cases, allowing you to calculate checksums for files, save them to a file, verify checksums from a file, and receive messages for missing files or verification failures. By using the ‘b2sum’ command, you can ensure the integrity and security of your files.