September 09, 2019

How to check the hash value of a directory

In fact, by their nature, Directory file cannot be obtained hash value. However, hash of each file in the directory can be obtained.

디렉터리 파일은 그 특성상 해시 값을 구할 수는 없다. 하지만, 디렉터리에 포함된 각 파일의 해시값을 구할 수는 있다.

$ find [path] -exec sha256sum {} \; > 20190430.txt
$ find [path] -exec sha256sum {} \; > 20190530.txt
$ diff 20190430.txt 20190530.txt

The find command recursively traverse all the files in a directory. Then apply a hash command to each file and save the result in a specific file. You can then check the integrity by comparing the two files as needed.

find 명령어를 통해 재귀적인 방식으로 디렉터리 내부의 모든 파일을 순회한다. 그리고 각 파일에 대해 해시 명령어를 적용하여 그 결과를 특정 파일에 저장한다. 이후 필요한 시점에 두 파일을 비교하면 무결성을 점검할 수 있다.




$ find [path] -exec sha256sum {} \; 2>/dev/null | sha256sum
0e64e1ab31b2... *-

As you can see, the result of the traversal is entered again into the hash command, which can be expressed as a single line.

순회한 결과를 다시 해시 명령어에 입력하면, 간단하게 한 줄로 표현할 수 있다.