2

I am in a need of manipulating the system time for few minutes.

Say I need to keep the time as it is and change the date to next date.
After few minutes.. I need to go back to the normal time with out giving any manual inputs.

How is this possible with command line only.

I have gone through this What is the command line statement for changing the System clock? and it requires to input date+time.

I dont want to change the time, only the date. Once the requirement is done that is after few minutes, I dont want to enter any manual inputs for date and time..

I want to go back to regular time with command line.

1

2 Answers 2

3

This is an example of what ( I think ) you want. It will change the year only to last year for 4 minutes then back to current date again. Your machine needs to be connected to the Internet for this to work:

sudo bash -c "timedatectl set-ntp off && date -s 'last year' && sleep 4m && timedatectl set-ntp on"
2

For the below to work, you should first go to Settings → Date & Time and disable Automatic Date & Time.

You can then use the following command as a (long) one-liner:

sudo date --set="2010-05-02 $(date '+%T')" && sleep 10 && sudo date -s "$(wget -qSO- --max-redirect=0 google.com 2>&1 | grep Date: | cut -d' ' -f5-8)Z"

The above command:

  • Gets the current system time:

    date '+%T'
    

    and sets the date to the one you wish, keeping the current time:

    sudo date --set="2010-05-02 $(date '+%T')"
    

    In this example the date, which you may change to what you need, is 2010-05-02.

  • Keeps the altered date for the specified time interval using the sleep command. Here I have used sleep 10 to keep the altered time for 10 seconds. You can add an m, h, or d suffix to sleep's argument (here 10) to specify the time in minutes, hours, or days, respectively.

  • Reverts the time to the current time (requires internet access):

    sudo date -s "$(wget -qSO- --max-redirect=0 google.com 2>&1 | grep Date: | cut -d' ' -f5-8)Z"
    

    Credits to Shrukul Habib.

0

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.