LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to use md5sum check {SOLVED} (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-use-md5sum-check-%7Bsolved%7D-4175470373/)

AndyInMokum 07-21-2013 05:34 AM

How to use md5sum check {SOLVED}
 
Greetings from Amsterdam

I'm having problems with md5sum check. I cannot get it to work. I download a iso file and save it. I then open a terminal and type:

md5sum "space"

I go to the saved ISO file and right click and go to properties. I then copy the file name and past it into the terminal.

andy1960@debimint-li-3710 ~ $ md5sum linuxmint-15-cinnamon-dvd-32bit.iso
md5sum: linuxmint-15-cinnamon-dvd-32bit.iso: No such file or directory
andy1960@debimint-li-3710 ~ $

Tried it as root, the result was the same.

Where and what am I doing wrong? Many thanks.

druuna 07-21-2013 06:09 AM

When you open a terminal your current directory is your home directory (/home/username). Your downloaded iso file is stored at a different location.

Assuming you have downloaded it to /home/username/Downloads, you need to do the following (as normal user, from a terminal):
Code:

cd Downloads
md5sum linuxmint-15-cinnamon-dvd-32bit.iso

This would also work (no cd needed):
Code:

md5sum Downloads/linuxmint-15-cinnamon-dvd-32bit.iso

AndyInMokum 07-21-2013 06:26 AM

Quote:

Originally Posted by druuna (Post 4994093)
When you open a terminal your current directory is your home directory (/home/username). Your downloaded iso file is stored at a different location.

Assuming you have downloaded it to /home/username/Downloads, you need to do the following (as normal user, from a terminal):
Code:

cd Downloads
md5sum linuxmint-15-cinnamon-dvd-32bit.iso

This would also work (no cd needed):
Code:

md5sum Downloads/linuxmint-15-cinnamon-dvd-32bit.iso

Hi druuna

Many thanks, that's done the trick. The file was on my Desktop. I just forgot to change the directory. Now I can enjoy the sun in Westerpark. Thanks again.

druuna 07-21-2013 06:53 AM

You're welcome :)

BTW: There's a click-able link on the top right-hand side that puts up the [SOLVED] tag, no need to do this by hand ;)

David the H. 07-21-2013 11:23 AM

A few years ago during a bout of serious scriptwriting practice I created one that makes comparing checksums easier. Perhaps it will be of help to you.

I named it "comparisum", but you can call it whatever you want. Copy it to a file, chmod it to be executable, and run the -h option for a full description of how to use it.

Code:

#!/bin/bash

checktype="sha1sum"        #Sets the default checksum used for file comparisons.
                        #string is the name of the command you want it to use:
                        #md5sum,sha1sum,sha224sum,sha256sum,sha384sum,sha512sum

EX_HELPOUT=79                #User-defined exit.  Successful output of usage information.
EX_USAGE=64                #Standard C exit code for bad arguments.  See /usr/include/sysexits.h
EX_RESULT=                #Used for the valid comparison result exit code.  Will be either 0 or 1

IFS=$'\n'                #Set IFS to newline, to deal with names with spaces
                        #(Not really necessary, but it doesn't hurt)

BCYAN="${BCYAN:-\e[1;36m}"        #Define some color codes, for prettified output.
BGREEN="${BGREEN:-\e[1;32m}"        #Use environment defaults if they exist.
BRED="${BRED:-\e[1;31m}"
BBLUE="${BBLUE:-\e[1;34m}"
BMAGENTA="${BMAGENTA:-\e[1;35m}"
RESET="${RESET:-\e[0m}"


## function that outputs usage info ##
ShowHelp() {

        local -a help

        #Use array indexes 0-4 for short help.
        help+=( ""                                                                                                                                                )
        help+=( "\tUsage:  ${BCYAN}${0##*/} [-t <type>] <file1|checksum1> <file2|checksum2>${RESET}"                )
        help+=( ""                                                                                                                                                )
        help+=( "\t\t${BCYAN}${0##*/} -h${RESET} for detailed help"                                                                        )
        help+=( ""                                                                                                                                                )
        #Use array indexes 5+ for long help.
        help+=( ""                                                                                                                                                )
        help+=( "\tUsage:\t${BCYAN}${0##*/} [-t <type>] <file1|checksum1> <file2|checksum2>${RESET}"                )
        help+=( "\t\t${BCYAN}${0##*/} -h${RESET}"                                                                                                )
        help+=( ""                                                                                                                                                )
        help+=( "\t${BBLUE}-h${RESET}\t\t: Display this help message and exit."                                                )
        help+=( ""                                                                                                                                                )
        help+=( "\t${BBLUE}-t <type>${RESET}\t: Use checksum <type> instead of the default."                                )
        help+=( "\t\t\t: Valid types are: md5 sha1 sha224 sha256 sha384 sha512"                                                )
        help+=( ""                                                                                                                                                )
        help+=( "\tThis script will compare the sha1, sha2, or md5 checksums of two files,"                                )
        help+=( "\tone file and a previously-generated checksum, or two checksums."                                        )
        help+=( ""                                                                                                                                                )
        help+=( "\tIf two filenames are given it will calculate and compare their checksums."                        )
        help+=( "\tIt uses sha1 by default.  This can be changed in the script, or you can"                                )
        help+=( "\tspecify a different checksum type with the '-t' option.  If multiple types"                        )
        help+=( "\tare given, only the last one will be used."                                                                                )
        help+=( ""                                                                                                                                                )
        help+=( "\tThe '-t' option is ignored when comparing a file to an entered checksum."                                )
        help+=( "\tThe script will automatically determine the checksum type based on the"                                )
        help+=( "\tlength of the string supplied to it."                                                                                        )
        help+=( ""                                                                                                                                                )
        help+=( "\tIf neither argument is a valid file, then it will compare them as"                                        )
        help+=( "\tliteral text strings.  This may be useful for comparing long checksums."                                )
        help+=( "\tIn this case the strings can have any length or content."                                                        )
        help+=( ""                                                                                                                                                )
        help+=( "\tThe exit code will be 0 if the entries match, and 1 if they don't."                                        )
        help+=( "\t( In addition, 64 = improper arguments and 79 = this help option. )"                                        )
        help+=( ""                                                                                                                                                )

        if [[ "$1" == "short" ]]; then
                echo -e "${help[*]:0:5}"
        elif [[ "$1" == "long" ]]; then
                echo -e "${help[*]:5}"
        fi
}


## function that calculates checksums for files ##
Check_Sum() {

        sum1=$( "$checktype" "$1" )
        echo "${sum1%% *}"

}


## function that compares the checksums of two files ##
Compare2Files() {

        # run both files through the Check_Sum function
        filesum1=$( Check_Sum "$1" )
        filesum2=$( Check_Sum "$2" )

        # compare and display the results
        filename1="${1##*/}"  #strip leading directories
        filename2="${2##*/}"  # for display purposes

        echo
        echo -e "Comparing files ${BCYAN}$filename1${RESET} and ${BCYAN}$filename2${RESET}."
        echo -e "Using ${BCYAN}$checktype${RESET} for comparison."
        echo
        echo -e "Sum of $filename1 is:\n${BBLUE}$filesum1${RESET}"
        echo -e "Sum of $filename2 is:\n${BBLUE}$filesum2${RESET}"
        echo
        if [[ "$filesum1" == "$filesum2" ]]; then
                echo -e "${BGREEN}The two files match.${RESET}"
                EX_RESULT=0
        else
                echo -e "${BRED}The two files are different.${RESET}"
                EX_RESULT=1
        fi
}


## function that compares a file checksum to a given string ##
CompareFile2Sum() {

        if [[ -f "$1" ]]; then        #determine the order of arguments
                file="$1"
                filename="${1##*/}"
                compstring="$2"
        else
                file="$2"
                filename="${2##*/}"
                compstring="$1"
        fi

        #Determine if $compstring is a valid checksum, and which type it is.
        #First reject non-hex strings, then test for length.

        if [[ "$compstring" =~ [^[:xdigit:]] ]]; then
                echo
                echo -e "${BMAGENTA}String to compare is not a valid checksum.  Exiting.${RESET}" >&2
                ShowHelp short >&2
                exit "$EX_USAGE"
        fi

        case "${#compstring}" in
                32) checktype=md5sum ;;
                40) checktype=sha1sum ;;
                56) checktype=sha224sum ;;
                64) checktype=sha256sum ;;
                96) checktype=sha384sum ;;
                128) checktype=sha512sum ;;
                *)        echo
                        echo -e "${BMAGENTA}String to compare is not a valid checksum.  Exiting.${RESET}" >&2
                        ShowHelp short >&2
                        exit "$EX_USAGE" ;;
        esac

        filesum=$( Check_Sum "$file" )

        echo
        echo -e "Comparing ${BCYAN}$filename${RESET} to the checksum given."
        echo -e "Using ${BCYAN}$checktype${RESET} for comparison."
        echo
        echo -e "Sum of $filename is:\n${BBLUE}$filesum${RESET}"
        echo -e "Sum to compare against is:\n${BBLUE}$compstring${RESET}"
        echo
        if [[ "$filesum" == "$compstring" ]]; then
                echo -e "${BGREEN}The two checksums match.${RESET}"
                EX_RESULT=0
        else
                echo -e "${BRED}The two checksums are different.${RESET}"
                EX_RESULT=1
        fi

}

## function that compares two strings ##
Compare2Strings() {

        echo
        echo "Comparing text string one to text string two."
        echo
        echo -e "Text string one is:\n${BBLUE}$1${RESET}"
        echo -e "Text string two is:\n${BBLUE}$2${RESET}"
        echo
        if [[ "$1" == "$2" ]]; then
                echo -e "${BGREEN}The two text strings match.${RESET}"
                EX_RESULT=0
        else
                echo -e "${BRED}The two text strings are different.${RESET}"
                EX_RESULT=1
        fi

}


###### Start main script operations ######
##########################################

## Process input options ##
while getopts ":ht:" opt; do
        case "$opt" in
                h) ShowHelp long >&2
                  exit "$EX_HELPOUT"
                ;;
                t) case "$OPTARG" in
                        md5|sha1|sha224|sha256|sha384|sha512) checktype="${OPTARG}sum"
                        ;;
                        *) echo
                          echo -e "${BMAGENTA}Invalid type \"${BRED}$OPTARG${BMAGENTA}\".${RESET}" >&2
                          echo -e "${BMAGENTA}Valid types are ${BRED}md5 sha1 sha224 sha256 sha384 sha512${RESET}" >&2
                          ShowHelp short >&2
                          exit "$EX_USAGE"
                        ;;
                  esac
                ;;
                \?) echo -e "${BMAGENTA}Invalid option \"${BRED}-$OPTARG${BMAGENTA}\".  Ignoring.${RESET}" >&2
                ;;
        esac
done

shift $(( OPTIND - 1 )) ; OPTIND=1


## Analyze input arguments (after shifting away options) and call the appropriate functions ##

if [[ ! "$2" || "$3" ]]; then                                #must have exactly two arguments.

        echo
        echo -e "${BMAGENTA}Invalid number of arguments.${RESET}" >&2
        ShowHelp short >&2
        exit "$EX_USAGE"

elif [[ -f  "$1" && -f "$2" ]]; then                # If both are files...

        Compare2Files "$1" "$2"

elif [[ ! -f  "$1" && ! -f "$2" ]]; then        # If neither are files...

        Compare2Strings "$1" "$2"

elif [[ ! -f "$1" || ! -f "$2" ]]; then                # If only one is a file...

        CompareFile2Sum "$1" "$2"

fi

echo

exit "$EX_RESULT"



All times are GMT -5. The time now is 06:13 PM.