Is it possible to execute a command that only adds the sha256 of a file if it has been modified?

1

1

Is it possible to execute a command that adds the sha256 of a file in the previously created list and only if it has been modified according to the last date registered in that list?

I do not know if they understand me. What I want to do is create a sha256 list of all the files along with the modification dates.

So, to keep the list updated, instead of going through all the files (a process that would take a lot of time and disk usage), I would need a command that compares the dates of changes registered in the list created with the current modification dates of each one of the files on the disk. In case of detecting a modification, then, the program generates the sha256 of the file and re-registers it in the list, overwriting the previous value (only of that file).

Is there a program that does this or some idea?

Update:

What I need is a program that in addition to creating the sha, register in turn the modification date belonging to each file.

E.g:

Contents of the file database_of_SHA256.txt:

0adca15c96d77a38aa0447fa87af9c297c *document1.txt  2018-12-03 04:12:23
dca15c96d77a9d30d2a7defad30d2a47fa *document2.txt  2018-09-09 10:19:11
77a915c9defad30d2c96d77aa0447fa87a *document3.txt  2017-01-20 17:34:04

The code what you should do is, I do not know if it is the most appropriate but you should create a list of dates and names of all the files on the computer and compare them with this data. If the dates are different, then you should generate and verify the checksum.

If the checksum does not match, then it means that the file has actually been modified and that list must be updated with the new information (it would be good to ask for a confirmation for this), if the user confirms that he wants to continue, then the list is recorded. new date and checksum of the file.

MarianoM

Posted 2018-11-09T07:37:58.797

Reputation: 19

Answers

1

You can easily achieve that with a shell script:

  • Compare the modification time of the files with that of the list (or the registered point in time)
  • If a file is newer than the list, run sha256sum on it and replace in the list

Something along the lines of

HASHCHANGED=$(stat -c '%Y' "/path/to/listfile") # Or extract the timestamp from the list
for FILE in /path/to/files/* ; do
  FILECHANGED=$(stat -c '%Y' "$FILE")
  if test $FILECHANGED -ge $HASHCHANGED; then
    NEWHASH=$(sha256sum "$FILE")
    # put the hash into your list, this depends on the list format
  fi
done

EDIT

From the comments I understand, that there is no predefined list format, as I had assumed from the OQ. this makes it natural to use the standard sha256sum format. We can now expand the script from above to a full solution:

# Configuration
HASHFILE="/path/to/hashfile"
FILEMASK="/path/to/files/*.suffix"

HASHCHANGED=$(stat -c '%Y' "$HASHFILE")
while read FILE ; do
  test -f "$FILE" || continue
  FILECHANGED=$(stat -c '%Y' "$FILE")
  if test $FILECHANGED -ge $HASHCHANGED; then
    NEWHASH=$(sha256sum "$FILE")
    cat "$HASHFILE" | grep -ve "\\s$FILE\$" > "$HASHFILE.tmp"
    echo "$NEWHASH" >> "$HASHFILE.tmp"
    cat "$HASHFILE.tmp" | sort -u > "$HASHFILE"
  fi
done < <(eval "ls -1 $FILEMASK") #Need an eval here to allow * in mask

You should seed the initial list with sha256sum /path/to/files/*mask

Eugen Rieck

Posted 2018-11-09T07:37:58.797

Reputation: 15 128

Interesting for the one who knows but not for novices like me. I still do not know how to create the list with the dates of each file and I do not understand where to add the list list in your code; You are not to blame for me asking this haha. I will try to learn more about the code and see if I can achieve it. – MarianoM – 2018-11-09T08:27:57.703

@MarianoM see my edit – Eugen Rieck – 2018-11-09T08:56:40.243

Thanks Eugen! After trying to make it work and testing line by line because the script was frozen, I analyzed its code according to my limited knowledge, and I realized that you are comparing the date of the hash file and what I need is that compare the date recorded in the list with the date of the file. If the date is different, then the new checksum is generated and registered in the list next to the updated date. – MarianoM – 2018-11-09T11:06:23.913

You may have misunderstood the translator. The While in upper case is not functional and the done gives me unexpected error, I have tried it in several ways and line by line, only in that I get an error. – MarianoM – 2018-11-09T11:07:38.087

Add some details to see if it is better understood. – MarianoM – 2018-11-09T11:21:44.690

@MarianoM the While was a typo, while is correct (lower-case w) – Eugen Rieck – 2018-11-09T11:28:05.550