skip to Main Content

I could manage to generate a hash file for each file not yet hashed recursively:

find . -type f ! -name '*.sha256' -execdir sh -c '[ ! -f "$1.sha256" ] && sha256sum "$1" > "$1.sha256"' _ {} ;

I can´t get it to produce hidden files prefixed with a dot. Adding a . like so

find . -type f ! -name '*.sha256' -execdir sh -c '[ ! -f "$1.sha256" ] && sha256sum "$1" > ".$1.sha256"' _ {} ;

Produces no output.

I can´t figure out how to add the . prefix or how to escape it.

I tried to isolate the variable like ${1} or {$1} also didn´t help.

I got the current generation for non hidden files extrapolated from this question and its solution
Create separate MD5 file for each file recursively

I would be perfectly happy to have bash script to call to do the job if it is easier to produce.
I would need the solution to work on debian and ubuntu linux.

2

Answers


  1. Chosen as BEST ANSWER

    As I was unable to handle it in a shell call I resorted to a python script. I could not wrap my head around how to make use of suggested basename neither had success with using exec instead of execdir.

    I'd still be interested out of curiosity if some one could provide me a working line of shell code.

    Until that I will work with the following python code

    import hashlib, os, sys
    
    
    for root, dirs, files in os.walk(".", topdown=False):
        for fileName in files:
            if(not fileName.endswith('.sha256')):
                # TODO: check if has file already present to only add if not yet computed
                absPath = os.path.join(root, fileName)
                hashFileName = "." + fileName + ".sha256"
                absPathPossibleHash = os.path.join(root, hashFileName)
                if(os.path.isfile(absPathPossibleHash)):
                    print ("hash file already exists: " + absPathPossibleHash)
                    continue
                print("fileName : " + fileName)
                print("root : " + root)
                print("creating hash of " + absPath)
                sha256_hash = hashlib.sha256()
                with open(absPath,"rb") as f:
                    # Read and update hash string value in blocks of 4K
                    for byte_block in iter(lambda: f.read(4096),b""):
                        sha256_hash.update(byte_block)
                    hashLine = sha256_hash.hexdigest() +"  " + fileName
                    print(hashLine)
                    
                    with open(os.path.join(root,hashFileName), "w") as hashFile:
                        hashFile.write(hashLine)
    

    At least I'm still able to use find to verify my hashes ;)

    find . -type f -name '.*.sha256' -execdir sh -c 'sha256sum -c "$1"' _ {} ; 
    

  2. Firstly, you check for "$1.sha256" but then try to create ".$1.sha256".

    Secondly and more importantly, when a command is invoked from execdir, its argument will always start with ./. Prepending a dot to it won’t do you much good. You can use basename to get rid of the ./.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search