Have you ever wanted to create a directory or file and name it the current date/time/month/year from command line in Linux? This brief tutorial will teach you how to create a directory or file with current timestamp in the name. 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". Creating files with timestamps will make your work easier to arrange the files in order. This will also be useful if you want to automate the task using a script. The following commands will create directories or files 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 Or Files 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")"
$ 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")"
Similarly, to create a file named with current date, time, month, year, simply replace "mkdir" with "touch" command:
$ touch "$(date +"%d-%m-%Y")"
Create directories or files with custom name with current date
What about a custom name for the directory or file with date/time/month/year? It is also possible.
$ mkdir ostechnix.com-$(date +"%d-%m-%Y")
This command will create a directory named "ostechnix.com-06-06-2020".
To create a file with custom name:
$ touch ostechnix.com-$(date +"%d-%m-%Y")
Create directories of files with ISO format
If you want to use ISO date format (e.g. 2020-06-06) and ls will list them in date order, run:
$ mkdir $(date -I)
$ mkdir $(date +%F)
$ mkdir $(date +"%Y-%m-%d")
All of the above three commands will produce the same result.
To create files, just replace mkdir with "touch" command.
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 06.
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 20. If you want the whole year (2020) 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
$ man touch
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