Bash and DD: Testing Flash Drive Speed with a Simple Script

Flash drives are commonly used for storing and transferring data, but their speed can vary depending on the device and the environment in which it is used. If you’re looking to measure the speed of your flash drive, you can use a simple script written in bash and using the dd command. In this article, we’ll explain how to write and run a bash script that will test the read and write speed of your flash drive, and provide an overview of how the script works. Whether you’re a beginner or an experienced Linux user, this script is an easy and efficient way to measure the performance of your flash drive.

In this tutorial you will learn:

  • How to write and run a bash script to test the read and write speed of a flash drive
  • An overview of the dd command and how it can be used to test flash drive speed
  • How to interpret the results of the flash drive speed test and understand the performance of your device
  • Tips for improving the speed of your flash drive, if necessary

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software N/A
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

Measuring USB Stick Performance with a Simple Shell Script

Measuring the read and write speed of a USB stick is a simple but important task to perform, especially when you are looking to buy a new USB stick or want to test the performance of an existing one. This can be easily done in Linux by using a shell script. In this article, we will look at a simple shell script that can be used to measure the read and write speed of a USB stick. Save the contetent of the below script into a file using any text editor.

Before you run the script ensure that the USB Drive you wish to test is mounted under the /media/usbstick directory.

#!/bin/bash

if [ ! -d "/media/usbstick" ]; then
  # If the USB stick is not mounted, print an error message and exit
  echo "Error: USB stick is not mounted at /media/usbstick. Please mount the USB stick and try again."
  exit 1
fi

dd if=/dev/zero of=/media/usbstick/testfile bs=1M count=1024 2> /dev/null

# Clear the disk cache
sh -c 'echo 3 > /proc/sys/vm/drop_caches'

{ time dd if=/dev/zero of=/media/usbstick/testfile bs=1M count=1024 oflag=dsync; } 2>&1 | awk '/real/ {print $2}' > write_time.txt

write_time_raw=$(cat write_time.txt)
write_time_minutes=$(echo $write_time_raw | awk -Fm '{print $1}')
write_time_seconds=$(echo $write_time_raw | awk -Fm '{print $2}' | awk -Fs '{print $1}')
write_time=$(echo "$write_time_minutes * 60 + $write_time_seconds" | bc)
write_speed=$(echo "1024 / $write_time" | bc)

echo "Write speed: $write_speed MB/s"

rm write_time.txt

# Clear the disk cache
sh -c 'echo 3 > /proc/sys/vm/drop_caches'

{ time dd if=/media/usbstick/testfile of=/dev/null bs=1M count=1024; } 2>&1 | awk '/real/ {print $2}' > read_time.txt

read_time_raw=$(cat read_time.txt)
read_time_minutes=$(echo $read_time_raw | awk -Fm '{print $1}')
read_time_seconds=$(echo $read_time_raw | awk -Fm '{print $2}' | awk -Fs '{print $1}')
read_time=$(echo "$read_time_minutes * 60 + $read_time_seconds" | bc)
read_speed=$(echo "1024 / $read_time" | bc)

echo "Read speed: $read_speed MB/s"

rm read_time.txt

Example output:

SanDisk Extreme speed test
SanDisk Extreme speed test

The purpose of this shell script is to measure the read and write speeds of a USB stick. The script first checks if the USB stick is mounted at the location /media/usbstick. If the USB stick is not mounted, the script will print an error message and exit.



Next, the script creates a test file of 1 GB on the USB stick using the dd command. The dd command is used again to measure the write speed, by writing the test file to the USB stick. The time taken to write the test file is recorded using the time command and the output is redirected to a file write_time.txt.

NOTE
The oflag=dsync option with the dd command ensures that data is immediately and safely written to the storage device. Unlike the default behavior of dd, which buffers output data, the dsync flag physically writes data to the storage device before returning, avoiding potential data loss. This option is useful for situations where data integrity is a priority, even if it results in slower write speeds.

The script then extracts the time taken to write the test file from write_time.txt and calculates the write speed in MB/s by dividing the size of the test file (1 GB) by the time taken. If the write time is zero, an error message is printed and the script exits. The write speed is then printed to the terminal.

The script repeats these steps to measure the read speed, by reading the test file from the USB stick and recording the time taken with the time command. The read speed is calculated in the same way as the write speed and is also printed to the terminal.

This script provides a simple and quick way to measure the read and write speeds of a USB stick, and can be useful for testing the performance of different USB sticks or for troubleshooting issues with slow transfer speeds.

Conclusion

In conclusion, this shell script provides a simple and effective way to measure the read and write speeds of a USB stick. By understanding the speeds of your USB stick, you can determine whether it is performing optimally or if there is room for improvement.

If the speeds of your USB stick are not as fast as you would like, there are a few tips that can help improve its performance:

  1. Use a USB 3.0 port, if available: USB 3.0 provides faster transfer speeds compared to USB 2.0, so using a USB 3.0 port can help improve the speed of your USB stick.
  2. Format the USB stick with a fast file system: Different file systems have different performance characteristics, so choosing the right file system can have a significant impact on the speed of your USB stick. File systems such as NTFS, exFAT, and FAT32 are typically faster than others, such as ext2 or ext3.
  3. Avoid using the USB stick for multiple purposes: If you use your USB stick for both storing files and running applications, it can slow down the transfer speeds. To avoid this, it’s best to use separate USB sticks for different purposes.
  4. Use a high-quality USB stick: Not all USB sticks are created equal, and the quality of the USB stick can have a big impact on its speed. Look for a high-quality USB stick from a reputable brand and be sure to read reviews before making a purchase.

By following these tips, you can help ensure that your USB stick is performing at its best, providing fast and reliable transfer speeds for all your data.



Comments and Discussions
Linux Forum