How do I find the file from given Hash value in Linux?
Please note that I don't want to find the hash value of a file, I need the opposite of that.
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.
Sign up to join this communityFrom my assumptions and understanding of the question, you could probably do it in several ways.
One way would be, if you know exactly which directory to look for the hash match then you could just pass the hash and then check the files in that directory. So basically what you are doing is this:
myHash="$1"
[[ $(sha256sum fileName | awk {'print $1'}) == "$myHash" ]] && echo "File found"
Simplifying this with a for loop:
checkHash(){
myHash="$1" ## The hash of the file that you want to find
if (( $# == 1 )); then ## Must pass at least one argument
## Checking each file in the pwd
for X in *; do
if [[ -f $X ]]; then
local hash=$( sha256sum $X | awk '{print $1}' )
[[ "$myHash" == "$hash" ]] && {
echo "Hash matches for $X";
}
fi
done
fi
}
checkHash "$@"
However, this would not scan the hash of any files started with dot.
Another way would be to do with xargs
and find
command.
As @MelcomX pointed out with find
command you can do it in a one liner, however it might take long time. Good thing about this solution is that you can pass part of the hash and it would still work since it is grep
ing.
find / -exec sha512sum {} + | grep YOURHASHHERE
If this is not what you were looking for then please elaborate your question as @Eliah Kagan suggested in the comment so that we can understand what you were looking for, or consider accepting the answer.
find / -exec sha512sum {} + | grep YOURHASHHERE
But this operation would take very long for the hole system....
xargs -P <10 or 15>
to make it go faster would be also very nice. @MelcomX
Nov 24, 2019 at 8:36
find ~ -exec md5sum {} + | tee ~/homeMD5s.txt | grep yourhash