uptime
's output usually has "x days, y min" or "x days, y:z" - but sometimes if the uptime is less than a day, it has the hours/minutes as the first time-based output and doesn't bother with the number of days. The rest of the output is very consistent in terms of the number of columns. As a result of that, personally I've been using sed to just remove the last four columns:
$ uptime | sed -e 's/^ [^ ]* up \(.*\)\(,[^,]*\)\{4\}$/\1/'
31 days, 35 min
Alternatively, if you want just the most significant data, you can use a similar method others have mentioned where you just get the first bit of data. With less than a day, this would result in <y> min
or <y>:<z>
. With more than a day, it would result in <x> days
.
$ uptime | sed -e 's/^ [^ ]* up \([^,]*\).*/\1/'
31 days
Lastly, if you want to reflect zero when it is less than 24 hours, you also have the option of using some math on /proc/uptime
using bc
or shell expressions:
$ uptime
13:58:41 up 31 days, 55 min, 9 users, load average: 0.80, 0.89, 0.81
$ echo $(( $(awk -F . '{print $1}' /proc/uptime) / 86400)) days
31 days
$ uptime
13:59:01 up 21:18, 1 user, load average: 0.00, 0.00, 0.00
$ echo $(( $(awk -F . '{print $1}' /proc/uptime) / 86400)) days
0 days