Search code examples
parsingunixsubdirectorychecksum

Unix. Parse file with full paths to SHA256 checksums files. Run command in each path/file


I have a file file.txt with filenames ending with *.sha256, including the full paths of each file. This is a toy example:

file.txt:

/path/a/9b/x3.sha256
/path/7c/7j/y2.vcf.gz.sha256
/path/e/g/7z.sha256

Each line has a different path/file. The *.sha256 files have checksums.

I want to run the command "sha256sum -c" on each of these *.sha256 files and write the output to an output_file.txt. However, this command only accepts the name of the .sha256 file, not the name including its full path. I have tried the following:

while read in; do
    sha256sum -c "$in" >> output_file.txt    
done < file.txt

but I get:

"sha256sum: WARNING: 1 listed file could not be read"

which is due to the path included in the command.

Any suggestion is welcome


Solution

  • #!/bin/bash
    
    while read in
    do
        thedir=$(dirname "$in")
        thefile=$(basename "$in")
        cd "$thedir"
        sha256sum -c "$thefile" >>output_file.txt
    done < file.txt
    

    Modify your code to extract the directory and file parts of your in variable.