Home FAQ Create Directories or Files Named With Current Date / Time / Month / Year
Create Directories Or Files Named With Current Date, Time, Month, Year In Linux

Create Directories or Files Named With Current Date / Time / Month / Year

By sk
1165 Views

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")"

Or,

$ mkdir $(date +"%d-%m-%Y")

This command will create a directory and name it the today's date in dd:mm:yyyy format.

Sample output:

06-06-2020

Create Directories Named With Current Date, Time, Month, Year In Linux

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".

Create directories or files with custom name with current date

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)

Or,

$ mkdir $(date +%F)

Or,

$ 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.

More examples

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)"

Year-only:

$ 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.

Sample output:

'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.

create directory with name of current day

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.


Suggested read:


Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

2 comments

Iordanis June 8, 2020 - 3:41 pm

Exellent site. I am a consultant and I often use the information you publish here. Also when something is really useful to me I keep a blog where I save such info . If you do mind i can stop this republishing. Check it and let me know. lazoudis.wordpress.com

I always give credit to the creators of the info i get. This is the latest post.

Reply
sk June 8, 2020 - 3:50 pm

Hi,

Thanks for your positive feedback. You are allowed to use our materials in your blog. However, please “do not copy/paste the entire article” in your blog. Some bloggers are copy/pasting the full article and claims it their own work. Please don’t do that. It hurts our search ranking in Google search. Instead, I request you to just post a few lines (5 or 6 lines) and add the source link at the end (like you did now) to avoid duplicates. Thanks for understanding.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More