• Rutvik Hajare

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


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. Here is simple step by step guide to you.


Create Directories Or Files Named With Current Date / Time / Month / Year:


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 directory and name it the today's date in dd:mm:yyyy format.


Sample output:


06-06-2020

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

What about a custom name for the directory or file with date/time/month/year? It is also possible.


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

This command will create a directory name "doceeo.com-date-month-year".




To create a file with custom name:


$ touch doceeo.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.date.

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. year 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 tile in hh:mm:ss format.

Sample output:


'06:21:52 PM IST'

We can even create directories with current minutes and seconds in the name.For example, the following command will create a directories 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 example,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“Sunday”i.e today’s name.




To create a directory with name of current month (i.e June), run:


$ mkdir "$(date +%B)"

Here is the list of supported operators .


%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


If you know another trick please comment down

Recent Posts

See All

Overview

Doceeo gives the ideas, tricks, tips and tutorials about the technology.
get info about the OS.
@2020 all right are reserved by Doceeo

Contact

  • Facebook
  • Twitter
  • Pinterest

Follow on Reddit

reddit-alien-red-st.jpg