Last active
November 19, 2025 23:53
-
-
Save andriitishchenko/00b9a32a6e64b35e8b0ff388d8c78fff to your computer and use it in GitHub Desktop.
Bash script for testing USB disk read and write speed for macOS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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" |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Navigate to your disk
Execute:
Expected output