2

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"

2
  • 1
    What is the output of your uptime command? What distro are you using? On Ubuntu, uptime outputs something like "16:25:06 up 47 days, 8:50, 2 users, load average: ..." The "16:25:06" is the time. The "up 47 days, 8:50" is up 47 days, 8 hours and 50 minutes" So, are you wanting to do a date/time calculation on the "47 days, 8:50" or is the output of your uptime different? Could you edit your question to show the output of the uptime command you are using? – Lewis M Nov 20 '18 at 21:28
  • do you like uptime -p ? – Jeff Schaller Nov 20 '18 at 21:29
6

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
3

Single command.

$ date -r /proc/1 '+The computer is on since %d-%b-%y'
The computer is on since 09-Oct-18
$
0

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

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.