I want the terminal to output the current upload and download speeds of my network connection. I do not want to monitor the speeds in real time. I just want a "static" value to be able to write it in a file.

How do I achieve it?


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.

  • 3
    There we go. Boiled it down into one awk statement. Bootiful. – Oli Apr 21 '14 at 14:26

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
  • Note: on Ubuntu 18.04 I had to use wlp1s0 instead of wlan0. I won't edit the answer because I know this depends on the distro. – Daniel Serodio May 14 '19 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.