1

I like to write a Bash script to generate sha256sum hash of a file, and save its output with hash and only filename.extension, excluding the path.

I'm not getting the output I want with both of the following scripts I have tried so far:

Note: There will be a new file with a new name every time the script is run, hence I'm using *.iso in the script.

(1)

#!/bin/bash

cd /home/admn/Downloads

find -maxdepth 1 -type f -name "*.iso" -exec bash -c "sha256sum '{}' > '{}'.sha256" \;

exit;

This creates a file Test.iso.sha256 but with an output: e64d11052abf5c3e19f0cd60e0b9c6196d8cb8615eba415ef1a3ac746f4b0c29 ./Test.iso

While I just want Test.iso without ./

(2)

#!/bin/bash

cd /home/admn/Downloads

fullfilename="/home/admn/Downloads/*.iso"
filename=$(basename "$fullfilename")

sha256sum $filename > "$filename".sha256

exit;

This does generate the output I want: e64d11052abf5c3e19f0cd60e0b9c6196d8cb8615eba415ef1a3ac746f4b0c29 Test.iso but the file it creates have * in it instead of name: *.iso.sha256. Thanks.

OS: Ubuntu MATE 21.10

Bash: Version 5.1.8(1)-release (x86_64-pc-linux-gnu)

1 Answer 1

2

It should be sufficient to do

cd /home/admn/Downloads

for f in *.iso; do
  sha256sum "$f" > "$f.sha256"
done

Change $f.sha256 to ${f%.iso}.sha256 if you want to remove the .iso extension before adding the sha256 extension.

1
  • it's perfect as I do want name.iso.sha256. Thank you so much as always.
    – Jags
    Mar 22, 2022 at 17:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .