Bash script to test hard drive transfer speed

The Linux operating system gives us many ways to measure the performance of our computer, including individual components such as the hard drive. There are multiple tools for the job, and it is also possible to use built in tools and create our own test to measure real results. In this tutorial, we will see how to use a Bash script to test the transfer speed of a hard drive on a Linux system. We will also learn about some other tools which can supplement our Bash script and give us and give us additional data points when it comes to the transfer speed of our hard drive, including read and write speed.

In this tutorial you will learn:

  • How to test hard drive speed with hdparm
  • How to test hard dive speed with dd and a Bash script
Bash script to test hard drive transfer speed
Bash script to test hard drive transfer speed
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software hdparm, dd
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Testing Hard Drive Speed

Linux has plenty of tools to test hard drive speed already. We can conduct a speed test using hdparm command as seen below:

$ sudo hdparm -t --direct /dev/sda

 Timing O_DIRECT disk reads: 2900 MB in  3.00 seconds = 966.60 MB/sec

However, in this case the hdparm command is accessing the raw hard drive, disregarding all partitions and file systems. In some cases, this does not give us a realistic transfer speed that we could expect to see when transferring files ourselves. Furthermore, it does not allow us an easy way to test the transfer speed between one or more devices or multiple file systems.

Bash Script For Testing Hard Drive Speed

Here is a small Bash scrip to test the hard drive transfer speed. It should be taken as an approximation. The speed value is taken via the Linux dd command output.

The weakness of the following script is that it does not take source hard drive reading speed into consideration, however it is accurate when measuring transfer speed between two hard drives or speed between two nodes over the network using NFS or samba.

# ./ /path/to/my/file /path/to/destination number_of_tests

for i in $(seq 1 $NUM_TESTs); do

        REC=$(dd if=$1 of=$2 2>some_random_file_ ; cat some_random_file_ | cut -d " " -f8 | tail -1)

        SUM=$(echo $SUM + $REC | bc)


RESULT=$(echo $SUM / $NUM_TESTs | bc | awk '{ str1=str1 $0 }END{ print str1 }')

echo $RESULT MB/s

#clean up
rm some_random_file_
rm $2

Note: If you do not have a file to copy, simply create one by running the following Linux command for a couple of seconds and interrupt with CTRL + C:

$ cat /dev/zero >

Run the script with 3 arguments: source file, destination file, and number of runs to make an average:

$ ./ /mnt/sdb1/ubuntu.iso /mnt/sda1/ubuntu.dd 3

The output will be shown in megabytes per second:

57 MB/s

Closing Thoughts

In this tutorial, we saw how to use a Bash script to test hard drive transfer speed on a Linux system. Our script utilizes the dd command to transfer a sample file on the file system itself, which is a great way to measure real transfer speed. While other tools like hdparm make it easy to test raw speed, this does not always yield a realistic transfer speed that can be expected when copying files onto the file system or between different devices.

Comments and Discussions
Linux Forum