Date command in Ubuntu

In this article, we cover date command in Ubuntu. The date command-line utility is used to set and display system’s date. Not only that, we can also modify our system’s time as well. It is a part of coreutils package. And, we don’t have have to install the package – coreutils as it’s installed as default in Ubuntu.

Note: Following operations require superuser privileges. In case you don’t have one then, we advise you to contact your System Administrator for assistance.

But, if for some reason, its not there then update the repository first –

sudo apt update

Next, to install coreutils package

sudo apt install coreutils

date command in Ubuntu

To display system date and time, just issue the following command in terminal –

date

To set system date we can use -s option –

sudo date -s "20221129"

To set both system date and time we can use –

sudo date -s "20221128 00:22"

The format for date here is YYYYMMDD and 24-hours system for time.

Notice that, we have used superuser privileges to set the date. Otherwise, we got an error –

date: cannot set date: Operation not permitted

Apart from that, if we don’t synchronize system clock over a network then we can set system date and time through date command. Furthermore, through date command itself we can get the last modification time of a file. For that, use -r option –

date -r <file-name>

For instance, if we have a file test.txt in /home/$USER/Downloads/ directory –

date -r /home/$USER/Downloads/test.txt

To display date and time in RFC 5322 format –

date -R

To display a date in specific date and time format, use –date option

date --date "Nov 28, 2022"

But, what if there are multiple of such inputs? In that case, we can put them in a file and use -f option. Let’s say we have a file – test.txt and following entries are there in the file –

Nov 28, 2022
Jan 01, 2023
Feb 21, 2024

Now, use the following command –

date -f test.txt

In conclusion, we have covered date command in Ubuntu here.

Additional Info –

Apart from what we covered for –date option above, we can also use it to get answers to question like – What was the date five years ago from today?

date --date "5 years ago"

What will be the date two years from now?

date --date "2 years"

Similar Posts