How to Create Date‑Named Directories and Files in Linux with One Command
This guide explains how to use the Linux date command together with mkdir or touch to automatically generate directories or files whose names reflect the current date or time, providing handy shortcuts for log management and other automation tasks.
When working on a Linux system you often need log files or other data organized by date; this article shows how to use the date command with mkdir or touch to create directories or files whose names are generated from the current date or time.
Creating a directory named YYYY‑MM‑DD
$ mkdir $(date +"%Y-%m-%d")or, with explicit quoting: $ mkdir "$(date +"%Y-%m-%d")" After running the command, a directory such as 2023-08-15 is created. To move into it, replace mkdir with cd:
$ cd "$(date +"%Y-%m-%d")"Creating a file with the current date
$ touch "$(date +"%Y-%m-%d")"Custom names that include the date
You can prepend or append any string to the date output:
$ mkdir ZhangSan-$(date +"%Y-%m-%d") $ touch ZhangSan-$(date +"%Y-%m-%d")Because the %Y-%m-%d format follows the ISO standard, the same result can be obtained with the shorter forms:
$ mkdir $(date -I) $ mkdir $(date +%F)Other examples
Only the day: $ mkdir "$(date +%d)" Only the month (numeric): $ mkdir "$(date +%m)" Only the month (full name): $ mkdir "$(date +%B)" Only the year (two‑digit): $ mkdir "$(date +%y)" Only the year (four‑digit): $ mkdir "$(date +%Y)" Current time (12‑hour format with AM/PM): $ mkdir "$(date +%r)" Current seconds: $ mkdir "$(date +%S)" Current minutes: $ mkdir "$(date +%M)" Current weekday name:
$ mkdir "$(date +%A)"Supported format specifiers for the date command
%a abbreviated weekday name (Sun, Mon, ...)
%A full weekday name (Sunday, Monday, ...)
%b abbreviated month name (Jan, Feb, ...)
%B full month name (January, February, ...)
%c date and time (e.g., Sat 29 Oct 2020 05:02:25 PM CST)
%C century
%d day of month (01..31)
%D same as %m/%d/%y
%F same as %Y-%m-%d (ISO 8601)
%g last two digits of ISO week-numbering year
%G ISO week-numbering year (four digits)
%h same as %b
%H hour (00..23)
%I hour (01..12)
%j day of year (001..366)
%k hour, space padded ( 0..23)
%l hour, space padded ( 1..12)
%m month (01..12)
%M minute (00..59)
%N nanoseconds (000000000..999999999)
%p AM or PM
%P am or pm
%q quarter of year (1..4)
%r 12‑hour clock time (e.g., 11:11:04 PM)
%R 24‑hour clock time (HH:MM)
%s seconds since 1970‑01‑01 00:00:00 UTC
%S second (00..60)
%t tab character
%T time (HH:MM:SS)
%u day of week (1..7, Monday=1)
%U week number of year, Sunday as first day (00..53)
%V ISO week number (Monday as first day, 01..53)
%w day of week (0..6, Sunday=0)
%W week number of year, Monday as first day (00..53)
%x date (e.g., 12/31/99)
%X time (e.g., 23:13:48)
%y year (two digits)
%Y year (four digits)
%z numeric time zone (+hhmm)
%:z numeric time zone with colon (+hh:mm)
%::z numeric time zone with seconds (+hh:mm:ss)
%:::z numeric time zone with optional minutes/seconds (+hh, +hh:mm, +hh:mm:ss)
%Z time zone abbreviation (e.g., EDT)For detailed usage of date, mkdir, and touch, consult their manual pages:
$ man date
$ man mkdir
$ man touchSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
