Did you know we can create directories or files named with current date, time, month, and year from command line? Yes! This tutorial explains how to create a directory or file with current timestamp in the name in Linux.
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 on October 16, 1984, you can create a folder named "16-10-1984".
Creating files with timestamps will make your work easier to arrange the files in order. This will also be useful when you want to automate the task using a script.
The following commands will create a directory or file and name it the current date/time/month/year based on your computer's clock. So make sure you have setup correct time on your system.
Create Directories And Files Named With Current Date, Time, Month And 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 a 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")"
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)
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 (i.e. 2020
) in the name, use Y
(Uppercase 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 uppercase.
To name directory with current minutes, use uppercase 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.
6 comments
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.
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.
I recommend YYYY-MM-DD so that sort by name is proper.
Just what I’ve been looking for, a lot of information on how to do the date and year.
I found some code that works for me but gives me the YYYYMMDD.
I was wondering if you could help me get it to just do the Name of the Month and year (Ex: March 2021).
@Echo Off
Set “sd=C:\EverNote”
Set “dd=E:\EverNote”
Set “ds=”
If Not Exist “%sd%\” Exit /B
For /F “Tokens=1-3Delims=/ ” %%A In (‘RoboCopy/NJH /L “\|” Null’ M D Year
) Do If Not Defined ds Set “ds=%%A_%%B_%%C”
If Not Defined ds Exit /B
RoboCopy “%sd%” “%dd%\%ds%” /E
Actually I want run a session in informatica and give the target path , every day if I run session that particular file should create in target and date wise and new folder should create dynamically every day. so can u tell how we can do that dynamically Using unix command.
I think you can do it with a script. I have very limited in scripting, so I can’t comment about it.