The Wayback Machine - http://web.archive.org/web/20240907072249/https://www.geeksforgeeks.org/md5sum-linux-command/
Open In App

md5sum Command in Linux with Examples

Last Updated : 19 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The md5sum is designed to verify data integrity using

MD5

(Message Digest Algorithm 5). MD5 is 128-bit cryptographic hash and if used properly it can be used to verify file authenticity and integrity. Example :

Input : md5sum /home/mandeep/test/test.cpp
Output : c6779ec2960296ed9a04f08d67f64422 /home/mandeep/test/test.cpp

Importance :

Suppose, anyone wants to install an operating system , so to verify if it’s correct CD, it’s always a good idea to verify .iso file using MD5 checksum, so that you don’t end up installing wrong software (some sort of virus which can corrupt your filesystem).

Syntax :

md5sum [OPTION]... [FILE]...

It will print or check MD5(128-bit) checksum. It computes MD5 checksum for file “test.cpp” Output :

c6779ec2960296ed9a04f08d67f64422  /home/mandeep/test/test.cpp

Options :

-b :

read in binary mode

-c :

read MD5 from files and check them

–tag :

create a BSD-style checksum

-t :

read in text mode(it’s by default)

The options which are useful when verifying checksum :

–ignore-missing :

don’t report status for missing files

–quiet :

don’t print OK for each successfully verified file

–status :

don’t output anything, status code shows success

–strict :

exit non-zero for improperly formatted checksum files

-w :

warn about improperly formatted checksum files

Command usage examples with options :

Example 1: Store the MD5 checksum in file and then verify it.

# md5sum /home/mandeep/test/test.cpp > checkmd5.md5

It will store the MD5 checksum for test.cpp in file checkmd5.md5

# md5sum -c checkmd5.md5

It will verify the contents of file Output :

/home/mandeep/test/test.cpp: OK

After changing the contents of file checkmd5.md5, the output will be :

/home/mandeep/test/test.cpp: FAILED
md5sum: WARNING: 1 computed checksum did NOT match

Example 2: create a BSD-style checksum with –tag option

# md5sum --tag /home/mandeep/test/test.cpp

Output :

MD5 (/home/mandeep/test/test.cpp) = c6779ec2960296ed9a04f08d67f64422

Example 3: –quiet option, can be used when verifying checksum, don’t print OK when verification is successful.

#  md5sum -c --quiet  checkmd5.md5 

Don’t produce any output, means it’s successful. But if checksum don’t match, it produces warning.

# md5sum -c --quiet  checkmd5.md5
/home/mandeep/test/test.cpp: FAILED
md5sum: WARNING: 1 computed checksum did NOT match

Example 4: –warn option, it can be used for generating a warning for improperly formatted checksum files. content of file checkmd5.md5:

c6779ec2960296ed9a04f08d67f64422 /home/mandeep/test/test.cpp

Now, execute command with –warn option

# md5sum -c --warn  checkmd5.md5
/home/mandeep/test/test.cpp: OK

It don’t produce any warning. Now, do some formatting in file checkmd5.md5

c6779ec2960296ed9a04f08d67f64422 
/home/mandeep/test/test.cpp

Now, execute the command

# md5sum -c --warn  checkmd5.md5

Output :

md5sum: checkmd5.md5: 1: improperly formatted MD5 checksum line
md5sum: checkmd5.md5: 2: improperly formatted MD5 checksum line
md5sum: checkmd5.md5: no properly formatted MD5 checksum lines found

and if –warn is replaced with –strict option, it will exit non-zero for improperly formatted checksum lines

# md5sum -c --strict  checkmd5.md5
md5sum: checkmd5.md5: no properly formatted MD5 checksum lines found

md5sum Command in Linux with Examples – FAQs

What is the md5sum command used for in Linux?

The md5sum command in Linux is primarily used to compute and verify the MD5 (Message-Digest Algorithm 5) hashes of files. MD5 is a widely used cryptographic hash function that produces a 128-bit (16-byte) hash value. It’s typically expressed as a 32-digit hexadecimal number. The md5sum tool is commonly used for checking data integrity. Its main purpose is to ensure that files have not been altered, whether by corruption, file transfer errors, or malicious intent.

How to generate an MD5 checksum for a file using md5sum?

To generate an MD5 checksum for a file, you can use the md5sum command followed by the filename. For example, to calculate the MD5 checksum of a file named example.txt, you would use the following command:

md5sum example.txt

This command will output the MD5 checksum followed by the filename. The output might look something like this:

d41d8cd98f00b204e9800998ecf8427e  example.txt

How to verify a file’s integrity with md5sum?

To verify a file’s integrity using md5sum, you need a previously generated MD5 checksum for comparison. Here’s how you can verify the integrity:

  1. First, save the expected MD5 checksum in a file. For instance, if you have an MD5 checksum d41d8cd98f00b204e9800998ecf8427e for example.txt, you would save this in a file (often called md5sum.txt) like this:
  2. Use md5sum with the -c (check) option to verify the file against the checksum in md5sum.txt:This command reads the MD5 checksum from md5sum.txt and compares it against the checksum computed for the current state of example.txt. The output will indicate whether the file is OK or FAILED.
d41d8cd98f00b204e9800998ecf8427e  example.txt
md5sum -c md5sum.txt

Can md5sum check multiple files at once?

Yes, md5sum can check multiple files at once. To do this, generate MD5 checksums for all the files you want to check and save them in a single checksum file. Here’s an example of how to generate the checksums for all files in a directory and save them:

md5sum * > md5sums.txt

You can then verify all files at once using:

md5sum -c md5sums.txt

This process will check each file listed in md5sums.txt and report on their integrity individually.

What are the limitations of using md5sum?

While md5sum is useful for checking data integrity, it has several limitations:

  • Security Vulnerabilities: MD5 is considered cryptographically broken and unsuitable for further use in security-sensitive applications. It is vulnerable to hash collisions, where two different inputs produce the same output hash, which can be exploited in various cryptographic attacks.
  • Performance: For very large files, generating an MD5 hash can be computationally intensive and time-consuming.
  • Not Foolproof for Security: Because of the vulnerabilities to collisions, MD5 checksums are not recommended for security verification purposes, like ensuring that a file has not been tampered with maliciously.
  • Limited to File Contents: md5sum only checks file contents and does not consider metadata changes, such as changes to file names, timestamps, or permissions.


Previous Article
Next Article

Similar Reads

Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a command-line tool for Linux and OSX, which is similar to the cat command in Linux. But the difference between cat and ccat is that the ccat shows the content of the file with the syntax highlighted. Currently, ccat supports the following programming languages. JavaScriptJavaRubyPythonGoCJSONInstallation of Ccat First, we are going to see
2 min read
How to Display Command History in Linux | history Command
The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the vario
4 min read
select command in Linux with examples
select command in Linux is used to create a numbered menu from which a user can select an option. If the user enters a valid option then it executes the set of command written in select block and then ask again to enter a number, if a wrong option is entered it does nothing. If user enters nothing and simply press 'enter' the option menu gets print
1 min read
atrm command in Linux with examples
atrm command is used to remove the specified jobs. To remove a job, its job number is passed in the command. A user can only delete jobs that belong to him. Only superuser can delete any job even if that belongs to another user. Syntax: atrm [-V] job [job...] Options: -V : Used to print the version number atrm -V job : Job number of the job which i
1 min read
expand Command in LINUX with examples
Whenever you work with files in LINUX there can be a situation when you are stuck with a file containing many tabs and whatever you need to do with a file requires that file with no tabs but with spaces. In this situation, the task looks quite simple if you are dealing with a small file but what if the file you are dealing with is very big or you n
3 min read
help Command in Linux with examples
If you are new to LINUX operating system and having trouble dealing with the command-line utilities provided by LINUX then you really need to know first of all about the help command which as its name says help you to learn about any built-in command. help command as told before just displays information about shell built-in commands. Here's the sy
3 min read
rcp Command in Linux with examples
There comes a time while using LINUX when there is a need to copy some information stored in a file to another computer. This can be done simply using rcp command line utility . Obviously there exists some other methods to complete the above mentioned task which are more secure (like scp or rsync) but this command lets you do this in the simple way
4 min read
fmt command in Linux with examples
fmt command in LINUX actually works as a formatter for simplifying and optimizing text files. Formatting of text files can also be done manually, but it can be really time-consuming when it comes to large text files, this is where fmt comes to rescue. fmt re-formats each paragraph in the file specified, writing to standard output. Here's the syntax
4 min read
unexpand command in Linux with Examples
To convert the leading spaces and tabs into tabs, there exists a command line utility called unexpand command. The unexpand command by default convert each spaces into tabs writing the produced output to the standard output. Here's the syntax of unexpand command : Syntax : $unexpand [OPTION]... [FILE]... where, OPTION refers to the options compatib
2 min read
uniq command in Linux with examples
The linux uniq command is basically used to remove all repeated lines in a file. This command is used when a line is repeated multiple times and replace these multiple lines with one line. This command is designed to work on sorted files. Purpose : Remove repetitious lines from the sorted 'input-file' and send unique lines to the 'output-file'. If
2 min read
anacron command in linux with examples
anacron command is used to execute commands periodically with a frequency specified in days. Its main advantage over cron is that it can be used on a machine which is not running continuously. In cron if a machine is not running on time of a scheduled job then it will skip it, but anacron is a bit different as it first checks for timestamp of the j
3 min read
for command in Linux with Examples
for command in Linux is used to repeatedly execute a set of command for every element present in the list. Syntax: for NAME [in WORDS ... ] ; do COMMANDS; done Example: Options: help for : It displays help information.
1 min read
exit command in Linux with Examples
exit command in linux is used to exit the shell where it is currently running. It takes one more parameter as [N] and exits the shell with a return of status N. If n is not provided, then it simply returns the status of last command that is executed. Syntax: exit [n] Options for exit command - exit: Exit Without Parameter After pressing enter, the
1 min read
autoheader command in Linux with Examples
autoheader command in Linux is used to create a template file of C "#define" or any other template header for configure to use. If the user will give autoheader an argument, it reads the standard input instead of reading configure.ac and also writes the header file to the standard output. This command scans the configure.ac file and figures out whi
1 min read
expect command in Linux with Examples
expect command or scripting language works with scripts that expect user inputs. It automates the task by providing inputs. // We can install expect command using following if not installed // On Ubuntu $sudo apt install expect // On Redhat based systems $ yum install expect First we write a script that will be needing inputs from users and then we
2 min read
function command in Linux with examples
The function is a command in Linux that is used to create functions or methods. It is used to perform a specific task or a set of instructions. It allows users to create shortcuts for lengthy tasks making the command-line experience more efficient and convenient. The function can be created in the user's current shell session or saved in the shell
2 min read
fg command in Linux with examples
fg command in linux used to put a background job in foreground. Syntax: fg [job_spec] job_spec may be: %n : Refer to job number n. %str : Refer to a job which was started by a command beginning with str. %?str : Refer to a job which was started by a command containing str. %% or %+ : Refer to the current job. fg and bg will operate on this job if n
1 min read
bg command in Linux with Examples
bg command in linux is used to place foreground jobs in background. Syntax: bg [job_spec ...] job_spec may be: %n : Refer to job number n. %str : Refer to a job which was started by a command beginning with str. %?str : Refer to a job which was started by a command containing str. %% or %+ : Refer to the current job. fg and bg will operate on this
1 min read
Clear Command in Linux with Examples
clear is a standard Unix computer operating system command that is used to clear the terminal screen. This command first looks for a terminal type in the environment and after that, it figures out the terminfo database for how to clear the screen. And this command will ignore any command-line parameters that may be present. Also, the clear command
2 min read
Continue Command in Linux with examples
continue is a command which is used to skip the current iteration in for, while, and until loop. It is used in scripting languages and shell scripts to control the flow of executions. It takes one more parameter [N], if N is mentioned then it continues from the nth enclosing loop. The syntax for the `continue` command in Linuxcontinue or continue [
2 min read
hostid command in Linux with examples
hostid is a command in Linux that is used to display the Host's ID in hexadecimal format. It provides a quick and straightforward way to retrieve the host ID, allowing administrators to associate it with software licenses or perform system-specific operations. Syntax of `hostid` command in Linuxhostid [OPTION] Options provided by hostedhostid --hel
1 min read
Dirname Command in Linux with Examples
dirname is a command in Linux that is used to remove the trailing forward slashes "/" from the NAME and prints the remaining portion. If the argument NAME does not contain the forward slash "/" then it simply prints dot ".". In other words, we can say that the `dirname` command is a useful tool for extracting the directory portion from a given path
2 min read
sdiff command in Linux with Examples
sdiff command in linux is used to compare two files and then writes the results to standard output in a side-by-side format. It displays each line of the two files with a series of spaces between them if the lines are identical. It displays greater than sign if the line only exists in the file specified by the File2 parameter, and a | (vertical bar
2 min read
Groups Command in Linux With Examples
In Linux, there can be multiple users (those who use/operate the system), and groups are nothing but a collection of users. Groups make it easy to manage users with the same security and access privileges. A user can be part of different groups. The `groups` is a powerful tool that helps administrators to view the group memberships of a user and re
2 min read
factor command in Linux with examples
The factor command in Linux is used to print the prime factors of the given numbers, either given from command line or read from standard input. The numbers given through standard input may be delimited by tabs, spaces or newlines. Syntax: factor [NUMBER] Factor 100 (non-prime number): We get the prime factors 2 and 5 that make up 100. Factor 13 (p
1 min read
chpasswd command in Linux with examples
chpasswd command is used to change password although passwd command can also do same. But it changes the password of one user at a time so for multiple users chpasswd is used. Below figure shows the use of passwd command. Using passwd we are changing the password of the guest user. Here first you have to enter the password of the currently signed u
2 min read
dir command in Linux with examples
dir command in Linux is used to list the contents of a directory. How is dir command different from ls? dir command differs from ls command in the format of listing contents that is in default listing options. By default, dir command lists the files and folders in columns, sorted vertically and special characters are represented by backslash escape
3 min read
eval command in Linux with Examples
eval is a built-in Linux command which is used to execute arguments as a shell command. It combines arguments into a single string and uses it as an input to the shell and execute the commands. Syntax eval [arg ...] Example: In the below figure, you can see that cd Desktop command is stored in a variable "CD" as a shell command. Now you can use thi
1 min read
lsmod command in Linux with Examples
lsmod command is used to display the status of modules in the Linux kernel. It results in a list of loaded modules. lsmod is a trivial program which nicely formats the contents of the /proc/modules, showing what kernel modules are currently loaded. Syntax: lsmod Example: Run lsmod at the command line to list all active kernel modules. lsmod Output:
1 min read
emacs command in Linux with examples
Introduction to Emacs Editor in Linux/Unix Systems: The Emacs is referred to a family of editors, which means it has many versions or flavors or iterations. The most commonly used version of Emacs editor is GNU Emacs and was created by Richard Stallman. The main difference between text editors like vi, vim, nano, and the Emacs is that is faster, po
5 min read