Nono.MA

Timestamp with bash

AUGUST 27, 2022

Here's how to print the current date and time with bash.

date +%y%m%d_%H%M%S
# 220715_124140

You can store the timestamp on a variable.

DATE_NOW=$(date '+%y%m%d_%H%M%S')

And make use of it later on your command for clarity.

echo "Today is $DATE_NOW."
# Today is 220715_124805.

Note that you can customize the format of your date or timestamp by adjusting the formatting template.

Here's how the command works.

date +{formatting_code}

And here's a full list of options.1

# Gives name of the weekday as Mon, Sun, Fri
date +%a

# Gives name of the weekday as Monday, Sunday, Friday
date +%A

# Gives name of the month as Jan, Feb, Mar
date +%b

# Gives name of the month as January, February, March
date +%B

# Displays day of the month as 05
date +%d

# Displays current date MM/DD/YY format as 11-01-21
date +%D

# Shows date in YYYY-MM-DD format as 2021-11-01
date +%F

# Shows hour in 24-hour format as 22
date +%H

# Shows hour in 12-hour format as 11
date +%I

# Displays the day of the year as 001–366
date +%j

# Displays the number of the month as 01–12
date +%m

# Displays minutes as 00-59
date +%M

# Displays seconds as 00-59
date +%S

# Displays in Nanoseconds
date +%N

# Displays time as HH:MM:SS in 24-hour format
date +%T

# Day of the week as 1-7; 1 is Monday, 6 is Saturday
date +%u

# Shows week number of the year as 00-53
date +%U

# Displays year YYYY as 2021
date +%Y

# Displays time zone
date +%Z

To learn more about what you can do with the date command, run man date to print the manual.

BlogCodeBash