1

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.

Pablo Bianchi
  • 17,371

1 Answers1

5

From 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 greping.

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.