Dernière modification : 14/07/2024

Date and Time Commands - Linux Bash

In this article, we will explore the main commands using dates or times.

1. Date Command

Display the current date:

date

Display the current date in a predefined format:

date "+%Y-%m-%dT%H:%M:%S%z" # Output: 2019-12-01T15:33:54+0200

Note: The symbol "+" precedes the date format.

The different formats available can be found at this address: https://man7.org/linux/man-pages/man1/date.1.html

It is possible to provide a specific date as input:

date -jf '%Y-%m-%d %H:%M:%S' '2017-05-10 13:40:01'

It is also possible to manipulate the date:

date -jf %Y-%m-%dT%H:%M:%S%z +%Y-%m-%dT%H:%M:%S%z 2017-05-10T02:40:01+0500 # Format input date, output date with updated timezone.

date -v +1y # Add 1 year to the current date. -v for "Adjust" on UNIX
date -d "+1 years" # on LINUX

Retrieve the Unix Timestamp:

date +%s // 1654172212

Generic example table:

        Format/result           |       Command              |          Output
--------------------------------+----------------------------+------------------------------
YYYY-MM-DD                      | date -I                    | $(date -I)
YYYY-MM-DD_hh:mm:ss             | date +%F_%T                | $(date +%F_%T)
YYYYMMDD_hhmmss                 | date +%Y%m%d_%H%M%S        | $(date +%Y%m%d_%H%M%S)
YYYYMMDD_hhmmss (UTC version)   | date --utc +%Y%m%d_%H%M%SZ | $(date --utc +%Y%m%d_%H%M%SZ)
YYYYMMDD_hhmmss (with local TZ) | date +%Y%m%d_%H%M%S%Z      | $(date +%Y%m%d_%H%M%S%Z)
YYYYMMSShhmmss                  | date +%Y%m%d%H%M%S         | $(date +%Y%m%d%H%M%S)
YYYYMMSShhmmssnnnnnnnnn         | date +%Y%m%d%H%M%S%N       | $(date +%Y%m%d%H%M%S%N)
YYMMDD_hhmmss                   | date +%y%m%d_%H%M%S        | $(date +%y%m%d_%H%M%S)
Seconds since UNIX epoch:       | date +%s                   | $(date +%s)
Nanoseconds only:               | date +%N                   | $(date +%N)
Nanoseconds since UNIX epoch:   | date +%s%N                 | $(date +%s%N)
ISO8601 UTC timestamp           | date --utc +%FT%TZ         | $(date --utc +%FT%TZ)
ISO8601 UTC timestamp + ms      | date --utc +%FT%T.%3NZ     | $(date --utc +%FT%T.%3NZ)
ISO8601 Local TZ timestamp      | date +%FT%T%Z              | $(date +%FT%T%Z)
YYYY-MM-DD (Short day)          | date +%F\(%a\)             | $(date +%F\(%a\))
YYYY-MM-DD (Long day)           | date +%F\(%A\)             | $(date +%F\(%A\))

2. Time Command

Used to calculate the execution time of a command, example:

time ls # Output: ls  0,00s user 0,00s system 38% cpu 0,007 total

3. Touch Command

Used to create an empty file or to update the date of an existing file without modifying it.

touch myExistingFile # Only updates the last modification date

touch myNewFile # Creates the file if it does not exist

>> myNewFile # this command is equivalent to touch (note that there are two ">")

4. At Command

Allows a script to be executed once at a predefined date.

at 2:30 am Friday < at-script.sh

5. Cal Command

Displays a monthly calendar:

cal

6. Sleep Command

Waits for X seconds. Useful for scripts waiting for an event, or to not overload the server during an infinite loop.

sleep 7 # waits 7 seconds

Note: The precision is not accurate enough for use with critical timing requirements.

7. Usleep Command

Identical to the previous command (sleep), except that instead of seconds, it takes microseconds as input.

usleep 1000 # waits 1000 microseconds

Note: As with the sleep command, the precision is not accurate enough for use with critical timing requirements.

 

LauLem.com - (Pages in French) Conditions of Use - Legal Information - Cookie Policy - Personal Data Protection Policy - About