Skip to content

Instantly share code, notes, and snippets.

@andriitishchenko
Last active November 19, 2025 23:53
Show Gist options
  • Select an option

  • Save andriitishchenko/00b9a32a6e64b35e8b0ff388d8c78fff to your computer and use it in GitHub Desktop.

Select an option

Save andriitishchenko/00b9a32a6e64b35e8b0ff388d8c78fff to your computer and use it in GitHub Desktop.
Bash script for testing USB disk read and write speed for macOS
#!/bin/bash
# Disk info + speed test for mounted volume
# --- PARAMETERS ---
FILE="tstfile"
COUNT=1024
BS=1024k
# ------------------
VOLUME_PATH="$PWD"
VOLUME_NAME=$(echo "$VOLUME_PATH" | sed -E 's|^/Volumes/([^/]+)(/.*)?$|\1|')
echo "$VOLUME_NAME"
disk_info=$(system_profiler SPUSBDataType 2>/dev/null)
disk_block=$(echo "$disk_info" | grep -E "^\s*$VOLUME_NAME:$" -B20 -A8)
capacity=$(echo "$disk_block" | awk '/Capacity:/ {print $2 " " $3; exit}')
free_space=$(echo "$disk_block" | awk '/Free:/ {print $2 " " $3; exit}')
filesystem=$(echo "$disk_block" | awk '/File System:/ {print $3; exit}')
bsd_name=$(echo "$disk_block" | awk '/BSD Name:/ {print $3; exit}')
mount_point=$(echo "$disk_block" | sed -n 's/^.*Mount Point:[[:space:]]*//p')
usb_speed=$(echo "$disk_block" | sed -n 's/^.*Speed:[[:space:]]*//p')
echo "Mount Point: $mount_point"
echo " Capacity: $capacity"
echo " Free: $free_space"
echo " File System: $filesystem"
echo " BSD Name: $bsd_name"
echo " Bus Speed: $usb_speed"
TMP_FILE="$VOLUME_PATH/$FILE"
spinner() {
local pid=$1
local message="${2}"
local delay=0.1
local spin='-\|/-'
local i=0
while kill -0 "$pid" 2>/dev/null; do
i=$(( (i + 1) % 4 ))
printf "\r[ %c ] ${message}..." "${spin:i:1}" >/dev/tty
sleep "$delay"
done
}
spinwait() {
local pid=$1
local message="${2:-Task}"
spinner $pid "$message" &
local spid=$!
wait $pid
kill $spid 2>/dev/null
printf "\r[ %s ] %s - Done\n" "*" "$message" >/dev/tty
}
write_speed_bytes=$(
{
echo "$( { dd if=/dev/zero of="$TMP_FILE" oflag=sync bs=$BS count=$COUNT; } 2>&1 | awk -F'[()]' '/bytes\/sec/ {gsub(/ bytes\/sec/,"",$2); print $2; exit}' )"
} &
spinwait $! "Testing write 1GB"
)
[ -z "$write_speed_bytes" ] && write_speed_bytes=0
write_speed=$(awk -v b="$write_speed_bytes" 'BEGIN {printf "%.1f", b/1024/1024}')
echo "Write Speed: $write_speed MB/s"
read_speed_bytes=$(
{
echo "$( { dd if="$TMP_FILE" of=/dev/null bs=$BS count=$COUNT; } 2>&1 | awk -F'[()]' '/bytes\/sec/ {gsub(/ bytes\/sec/,"",$2); print $2; exit}' )"
} &
spinwait $! "Testing read 1GB"
)
[ -z "$read_speed_bytes" ] && read_speed_bytes=0
read_speed=$(awk -v b="$read_speed_bytes" 'BEGIN {printf "%.1f", b/1024/1024}')
echo "Read Speed: $read_speed MB/s"
rm -f "$TMP_FILE"
@andriitishchenko
Copy link
Author

andriitishchenko commented Nov 19, 2025

Navigate to your disk

cd /Volume/...

Execute:

bash -c "$(curl -fsSL https://gist.githubusercontent.com/andriitishchenko/00b9a32a6e64b35e8b0ff388d8c78fff/raw/148f185954bdac513b329a0f428acabef5140258/check_disk_speed.sh)"

Expected output

Mount Point: /Volumes/Untitled
Capacity: 62,91 GB
Free: 62,9 GB
File System: ExFAT
BSD Name: disk2
Bus Speed: Up to 480 Mb/s
[ * ] Testing write 1GB - Done
Write Speed: 16.2 MB/s
[ * ] Testing read 1GB - Done
Read Speed: 33.5 MB/s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment