How can I improve my command to verify integrity of a directory?

IUseDebianBTW

New Member
Joined
Jun 20, 2025
Messages
4
Reaction score
0
Credits
34
I found this command:
$ find "Directory Name" -type f -print0 | sort -z | xargs -r0 sha256sum | sha256sum

It fulfills my requirement of generating a checksum for a whole directory without disk writes, but with a few issues:

1. Different result generated on gnome compared to kde
Maybe because gnome puts some hidden definition files in each directory? im not sure. How can I skip checking hidden files & resolve this issue?

2. sha256sum is a bit cpu slow, I just need to verify integrity for the purpose of detecting corruption/missing files
What would be the best checksum to use that works by default on all normal distributions?
 


Do files ever change(content and existence/names)? Are they supposed to?
If not, why not make back up into tar.gz or something?
Overall a bit poor information on the topic.

You could just
 
Shot in the dark... In the meta data is things like the last time the file was accessed. Could moving them around have changed that meta data and thus changed the checksum?
 
Shot in the dark... In the meta data is things like the last time the file was accessed. Could moving them around have changed that meta data and thus changed the checksum?
I have moved around the same files a lot and made edits to them to change up the meta data but the command seems to properly ignore meta data.
 
Do files ever change(content and existence/names)? Are they supposed to?
If not, why not make back up into tar.gz or something?
Overall a bit poor information on the topic.

You could just
The goal is to be able to make small edits to directories and not have to recompress a massive file.
 
The goal is to be able to make small edits to directories and not have to recompress a massive file.
i just realized:
find "Directory Name" -type f -print0
This finds not FOLDER but individual files with "Directory Name".
Also, checksum does not work with metadata, it only works with file content.

also, "find "Directory Name" -type f -print0 | sort -z | xargs -r0 sha256sum | sha256sum"
could be translated in
find "file names" -type f -exec sha256sum '{}' \;
it will find files and run checksum on all found files.
You can redirect it to text file, and then do redirection again in a month or what not.
than do "diff" on two files to compare
 
i just realized:
find "Directory Name" -type f -print0
This finds not FOLDER but individual files with "Directory Name".
Also, checksum does not work with metadata, it only works with file content.

also, "find "Directory Name" -type f -print0 | sort -z | xargs -r0 sha256sum | sha256sum"
could be translated in
find "file names" -type f -exec sha256sum '{}' \;
it will find files and run checksum on all found files.
You can redirect it to text file, and then do redirection again in a month or what not.
than do "diff" on two files to compare
Using the command you provided seems to cause files to be found in a different order on different systems. The goal is to get a single checksum for the contents of a directory.
 
find $path_to_folder "$file_names" -type f -exec sha256sum '{}' \;

change $path_to_folder for path of the folder that you need to make sure is not changing
change $file_names to filename mask or for just *

perhaps redirect output to text file, just make sure that redirect file is ABOVE $path_to_folder

to do redirect:
find $path_to_folder "$file_names" -type f -exec sha256sum '{}' \; 2>>/dev/null | sort >> report"$(date)".txt

this will have all checksums in folder where command is run, make sure not to run said command from same location where folder in question is.

once you have report - run it again when you need to check things for changes.
you will have two files.
something like:
'reportFri Jun 20 10:47:37 PM IST 2025.txt'
'reportFri Jun 25 10:47:37 PM IST 2025.txt'

you can check them with diff command
diff 'reportFri Jun 20 10:47:37 PM IST 2025.txt' 'reportFri Jun 25 10:47:37 PM IST 2025.txt'

it will tell you whats the difference between those 2.
 
Last edited:
2. sha256sum is a bit cpu slow, I just need to verify integrity for the purpose of detecting corruption/missing files
What would be the best checksum to use that works by default on all normal distributions?

This command will tell you which algorithm is the fastest:
Bash:
openssl speed md5 sha1 sha256 sha512
 


Follow Linux.org

Staff online

Members online


Top