3

I would like to generate sha256 hash for each log file in a folder and store that hash in a file named by the original file name suffixed with ".sha265" int he same folder. For example, I would like to store the hash for /path/file.ext into /path/file.ext.sha256

I succeed in generating the hashes but fail in creating the .sha256 files. Here is how are generated the hashes :

find /data/dump/ -type f -name "*.log" -exec sha256sum {} \;

Any help would be appreciated ! :)

4

Try with the following test command

find /data/dump/ -type f -name "*.log" -exec echo bash -c "sha256sum '{}' > '{}'.sha256" \;

and if it works, remove the echo:

find /data/dump/ -type f -name "*.log" -exec bash -c "sha256sum '{}' > '{}'.sha256" \;

The single and double quotes are there to make it work. It is extra complicated if there are special characters in the file names, for example spaces.

It works for me in a test directory. If it does not work for you, you can 'see' what happens and modify the expression using the output when echo is there.

| improve this answer | |
  • It works perfectly. Thanks a lot ! – Piero Aug 4 at 14:58
  • @Piero, You are welcome, I'm glad that I could help you :-) – sudodus Aug 4 at 14:59
4

This should work:

find /data/dump/ -type f -name "*.log" -exec sh -c "sha256sum {} | tee {}.sha256" \;

The sh -c part is called inline-script. See this for reference.

If you want to save only the sha256, you can pipe the sha256 utility with awk:

find /data/dump/ -type f -name "*.log" -exec sh -c "sha256sum {} | awk '{print \$1}' | tee {}.sha256" \;

The backslash before $1 is required to avoid unwanted parameter expansion.

| improve this answer | |
  • This works too. Thanks ! – Piero Aug 4 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.