I want to convert uptime to date DD:MM:YY without the | and I want to put a string like "the computer is on since 23-feb-16"
3 Answers
You may get it for free from the output of last reboot
:
$ last reboot
reboot system boot 4.14.81-i7 Sat Nov 17 23:25 still running
reboot system boot 4.14.80-i7 Fri Nov 16 09:16 - 15:49 (06:33)
$ printf "On since: "; last reboot | grep "still running" | cut -c 40-56
On since: Sat Nov 17 23:25
$ printf "On since: " ; last reboot --time-format iso | grep "still running" | cut -c 40-49
On since: 2018-11-17
Your uptime
command might also have the -s
option:
$ uptime -s
2018-11-17 23:25:23
Since this format is acceptable to date -d
, you can reformat the time however you wish like this::
$ date -d "$(uptime -s)" "+On since: %d:%m:%y"
On since: 17:11:18
-
"%d:%m:%y" must be the most perverse date format string I've seen outside of xkcd.com/1179 :) May 12 at 22:20
Single command.
$ date -r /proc/1 '+The computer is on since %d-%b-%y'
The computer is on since 09-Oct-18
$
Using the last data modification of /proc
directory given by stat
:
date -d "@$(stat -c '%Y' "/proc")" +'%F %T %z'
Example:
$ stat /proc
File: `/proc'
Size: 0 Blocks: 0 IO Block: 1024 directory
Device: 3h/3d Inode: 1 Links: 188
Access: (0555/dr-xr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2020-09-04 16:36:02.016000956 +0200
Modify: 2020-09-04 16:36:02.016000956 +0200
Change: 2020-09-04 16:36:02.016000956 +0200
Birth: -
$ date -d "@$(stat -c '%Y' "/proc")" +'%F %T %z'
2020-09-04 16:36:02 +0200
uptime -p
?