Generate and Verify SHA-256 Hash of File in Linux

Generate and Verify SHA-256 Hash of File in Linux

SHA-256 is a cryptographic hash function that can be used for verifying file integrity. For example, to check that the file has not been altered during transfer over the network. This tutorial provides example how to generate and verify SHA-256 hash of file in Linux.

Generate SHA-256 hash

In order to test, create a new file:

printf 'Hello world' > test.txt

Linux provides sha256sum command to generate and verify SHA-256 hash. Run the following command to generate SHA-256 hash of file and print result in the terminal:

sha256sum test.txt


64ec88ca00b268e5ba1a35678a1b5316d212f4f366b2477232534a8aeca37f3c  test.txt

Result can be written to a file by using > redirection operator:

sha256sum test.txt > test.sha256

Verify SHA-256 hash

File integrity can be verified using -c option:

sha256sum -c test.sha256


test.txt: OK

Change content of a file by appending additional text:

printf 'Hi' >> test.txt

We get the following output when SHA-256 hash of a file was verified again:

test.txt: FAILED
sha256sum: WARNING: 1 computed checksum did NOT match

Leave a Comment

Cancel reply

Your email address will not be published.