1

I'm currently saving each folder on a hash file with the following code

find folder/ -type f -exec sha256sum {} > checksumfolder.txt \;

I'm wondering if it is possible instead of checking/recalculating and recreating that txt file to just update the old txt file and add the newly added files that got no hash?

Yareli
  • 11

1 Answers1

0

Create a Makefile with the following contents:

SHAS := $(patsubst folder/%, sha/%, $(shell find folder/ -type f))

all: sha $(SHAS) checksumfolder.txt

sha:
    mkdir sha

sha/%: folder/%
    sha256sum $< > $@

checksumfolder.txt: $(SHAS)
    cat $(SHAS) > checksumfolder.txt

When you now run make inside the parent directory of folder/, a new sha/ directory will be created containing the sha256sum of each file in folder/. At the end we concatenate all files into checksumfolder.txt.

When you run make a second time, nothing will be done.

When you touch a file in folder/ or create a new one and run make the sha256sum of this specific file gets updated.

To learn more about make run info make.


If you never change a file (only add new ones), you could simply remember the file name with the most recent modification time when you last ran the find command and select only the newer files with:

find -newer REFERENCE_FILE

… or a direct timestamp (see date(1) DATE STRING) with

find -newermt TIME_STAMP
Devon
  • 847