How to get the current upload and download speeds in terminal?

Oddly the easiest way seems to be looking at /proc/net/dev. I've written the following to compare that file twice (with a second delay) and then to subtract the total bytes values. In this case em1 is the network adaptor so just change that to whatever you need to look at.

awk '/em1/ {i++; rx[i]=$2; tx[i]=$10}; END{print rx[2]-rx[1] " " tx[2]-tx[1]}' \
 <(cat /proc/net/dev; sleep 1; cat /proc/net/dev)

The output is two numbers. Received bytes per second followed by sent bytes per second.


Here's a variation on Oli's excellent solution:

awk '{if(l1){print $2-l1,$10-l2} else{l1=$2; l2=$10;}}' \
  <(grep wlan0 /proc/net/dev) <(sleep 1; grep wlan0 /proc/net/dev)

It will print the same result as Oli's approach:

$ awk '{if(l1){print $2-l1,$10-l2} else{l1=$2; l2=$10;}}' \
>   <(grep wlan0 /proc/net/dev) <(sleep 1; grep wlan0 /proc/net/dev)
401500 30286

The first value is the download rate in bytes per second and the second is the upload rate. You could get a more human-friendly format with:

$ awk '{if(l1){print ($2-l1)/1024"kB/s",($10-l2)/1024"kB/s"} else{l1=$2; l2=$10;}}' \
    <(grep wlan0 /proc/net/dev) <(sleep 1; grep wlan0 /proc/net/dev)
398.771kB/s 82.8066kB/s

Tags:

Command Line