Reputation: 115
I would like to have a unique ID for filenames so I can iterate over the IDs and compare the checksums of the files? Is it possible to have a checksum for the name of the file so I can have a unique ID per filename? I would welcome other ideas.
Upvotes: 1
Views: 15217
Reputation:
Yes, it is possible to obtain the MD5 of an string:
$ printf '%s' "This-Filename" | md5sum
dd829ba5a7ba7bdf7a391f2e0bd7cd1f -
It is important to understand that there is no newline at the end of the printed string. An equivalent in bash would be to use echo -n
:
$ echo -n "This-Filename" | md5sum
dd829ba5a7ba7bdf7a391f2e0bd7cd1f -
The -n
(valid in bash) is important because otherwise your hash would change with the inclusion of a newline that is not part of the text:
$ echo "This-Filename" | md5sum
7ccba9dffa4baf9ca0e56c078aa09a07 -
That also apply to file contents:
$ echo -n "This-Filename" > infile
$ md5sum infile
dd829ba5a7ba7bdf7a391f2e0bd7cd1f infile
$ echo "This-Filename" > infile
$ md5sum infile
7ccba9dffa4baf9ca0e56c078aa09a07 infile
Upvotes: 2
Reputation: 6036
Is it what you want?
Plain string:
serce@unit:~$ echo "Hello, checksum!" | md5sum
9f898618b071286a14d1937f9db13b8f -
And file content:
serce@unit:~$ md5sum agent.yml
3ed53c48f073bd321339cd6a4c716c17 -
Upvotes: 4
Reputation: 85855
Yes it is possible using md5sum
and basename $0
gives the name of current file
Assuming I have the script as below named md5Gen.sh
#!/bin/bash
mdf5string=$(basename "$0" | md5sum )
echo -e `basename "$0"` $mdf5string
Running the script would give me
md5Gen.sh 911949bd2ab8467162e27c1b6b5633c0 -
Upvotes: 3