How To Create Directories Named With Current Date / Time / Month / Year
Have you ever wondered how to create a directory and name it the current date or time or month or year from commandline in Linux? That’s what we are going to learn today. This brief tutorial will teach you how to create a directory with current date in the name of that directory. This will be helpful when you want to save something, for example photos, in directories named with date when they are actually taken. For example, If the photos were taken today, you can create a folder named “05-10-2019”. This will also be useful if you want to automate the task using a script. The following commands will create directories named with current date or time based on your computer’s clock. So make sure you have setup correct time on your system.
Create Directories Named With Current Date / Time / Month / Year In Linux
To create a directory and name it the current date, simply run:
$ mkdir "$(date +"%d-%m-%Y")"
This command will create a directory and name it the today’s date in dd:mm:yyyy format.
To switch into this directory, simply replace “mkdir” with “cd” command like below.
$ cd "$(date +"%d-%m-%Y")"
As one of our reader suggested in the comment section, if you want to use ISO date format (e.g. 2019-10-08) and ls will list them in date order, run:
$ mkdir $(date -I)
If you want only day of the current date, use:
$ mkdir "$(date +%d)"
This command will only create the directory with current day in the name. i.e 05.
Similarly, you can create directories with current month-only in the name:
$ mkdir "$(date +%m)"
$ mkdir "$(date +%y)"
This command will name the directories with the last two digits of current year i.e 19. If you want the whole year in the name, use Y (capital Y).
$ mkdir "$(date +%Y)"
How about directories name with current time? It is also possible.
$ mkdir "$(date +%r)"
This command will create a folder and name it with current time in hh:mm:ss format.
'02:59:52 PM IST'
We can even create directories with current minutes and seconds in the name. For example, the following command will create a directory and name it with current second.
$ mkdir "$(date +%S)"
Here, S is capital.
To name directory with current minutes, use capital M:
$ mkdir "$(date +%M)"
In all of the above examples, we created the directories with numbers on their names. What if you want to name the directories with actual name of the current day/month like Sunday, October etc? It’s simple!
$ mkdir "$(date +%A)"
The above command will create a directory named “Saturday” i.e today’s name.
To create a directory with name of current month (i.e October), run:
$ mkdir "$(date +%B)"
Here is the list of supported operators that you can use to name the directories with current day, month, time, year, day of week, day of the month, time zone etc.
%a locale's abbreviated weekday name (e.g., Sun) %A locale's full weekday name (e.g., Sunday) %b locale's abbreviated month name (e.g., Jan) %B locale's full month name (e.g., January) %c locale's date and time (e.g., Thu Mar 3 23:05:25 2005) %C century; like %Y, except omit last two digits (e.g., 20) %d day of month (e.g., 01) %D date; same as %m/%d/%y %e day of month, space padded; same as %_d %F full date; same as %Y-%m-%d %g last two digits of year of ISO week number (see %G) %G year of ISO week number (see %V); normally useful only with %V %h same as %b %H hour (00..23) %I hour (01..12) %j day of year (001..366) %k hour, space padded ( 0..23); same as %_H %l hour, space padded ( 1..12); same as %_I %m month (01..12) %M minute (00..59) %n a newline %N nanoseconds (000000000..999999999) %p locale's equivalent of either AM or PM; blank if not known %P like %p, but lower case %q quarter of year (1..4) %r locale's 12-hour clock time (e.g., 11:11:04 PM) %R 24-hour hour and minute; same as %H:%M %s seconds since 1970-01-01 00:00:00 UTC %S second (00..60) %t a tab %T time; same as %H:%M:%S %u day of week (1..7); 1 is Monday %U week number of year, with Sunday as first day of week (00..53) %V ISO week number, with Monday as first day of week (01..53) %w day of week (0..6); 0 is Sunday %W week number of year, with Monday as first day of week (00..53) %x locale's date representation (e.g., 12/31/99) %X locale's time representation (e.g., 23:13:48) %y last two digits of year (00..99) %Y year %z +hhmm numeric time zone (e.g., -0400) %:z +hh:mm numeric time zone (e.g., -04:00) %::z +hh:mm:ss numeric time zone (e.g., -04:00:00) %:::z numeric time zone with : to necessary precision (e.g., -04, +05:30) %Z alphabetic time zone abbreviation (e.g., EDT)
For more details, refer man pages.
$ man date
$ man mkdir
Hope this helps.
- How To Create Directories From A Text File In Linux
- The mktemp Command Tutorial With Examples For Beginners
- How To Quickly Create Files Inside Nested Directories In Linux