11

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?

2 Answers 2

16

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.

2
  • 4
    There we go. Boiled it down into one awk statement. Bootiful.
    – Oli
    Apr 21, 2014 at 14:26
  • Worth mention that '/em1/' should be changed to '/<part of your interface name>/'
    – matan7890
    Dec 12, 2023 at 11:27
12

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
1
  • 1
    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. May 14, 2019 at 20:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .