Linux print uptime in short format

547 Views Asked by At

When uptime normally prints out like this

ubuntu@ubuntu:~$ uptime -p
up 4 days, 1 hour, 43 minutes

How could I get it to print out in a shorter format, something like:

up 4d, 1h, 43m

I need to keep it short to display it on a 1602 lcd screen. I would also prefer a oneliner but that's not neccessary.

1

There are 1 best solutions below

1
Ledi On

This was way simpler question than I first thought, just replace the words...

uptime -p | sed 's/up\s*//g' | sed 's/\s*days/d/g' | sed 's/\s*hours/h/g' | sed 's/\s*minutes/m/g'

produces:

4d, 2h, 12m