Found an old HDD that I would like to show activity for fun. Got a bash script that would help demonstrate the hard drive motion


A while ago, I found a an old laptop HDD and found a transparent sata to USB adapter.

This would be a good demo for others to see what old tech was like before SSDs.

But to best demo its motion to other people, I needed a bash script to rapidly read the hdd to show the arm of the HDD in motion.

This bash script will rapidly read from random sections of the HDD.

#!/bin/bash
set -euo pipefail

# ---- Configuration ----
workdir="thrashtest"
num_files=60   # total files
file_size="1G" # per-file size
block_size=$(stat -f --format="%S" .)

# calculate blocks per file
file_bytes=$(numfmt --from=iec $file_size)
blocks_per_file=$(( file_bytes / block_size ))

echo "Creating $num_files × $file_size files in ./$workdir"
echo "Filesystem block size: $block_size bytes"
echo "→ $blocks_per_file blocks per file"

mkdir -p "$workdir"
cd "$workdir"

echo "creating $workdir ($num_files bigfiles)"
for i in $(seq 1 $num_files)
do
    fname="file_${i}.bin"
    if [ ! -f "$fname" ]
    then
        echo "writing $fname"
        dd if=/dev/zero of="$fname" bs=$block_size count=$blocks_per_file status=progress
    fi
done
echo "finished creating $workdir"

# ---- Thrash loop (random read or write) ----
while true; do
  # pick a random file index and offset
  idx=$(( RANDOM % num_files + 1 ))
  offset=$(( RANDOM % blocks_per_file ))
  fname="file_${idx}.bin"

  # Random Read
  dd if="$fname" of=/dev/null bs=$block_size count=1 skip=$offset status=none
done

You could replace the random read with below if you want to read and write as well. It's slower and does't look that much different in practice so not really needed here, but it might be of interest to you from an understanding perspective.

# choose action: 0 = read, 1 = write
action=$(( RANDOM % 2 ))
if [ "$action" -eq 0 ]; then
  echo "[READ ] $fname @ block $offset"
  dd if="$fname" of=/dev/null bs=$block_size count=1 skip=$offset status=none
else
  echo "[WRITE] $fname @ block $offset"
  dd if=/dev/urandom of="$fname" bs=$block_size count=1 seek=$offset conv=notrunc status=none
fi

After running it, do take note about how the arm moves around and think about how the HDD has to physically move the arm to the right spot to read.

This is why random access is super slow compared to SSD flash memories.