Using bash, how to create a folder using the date and time?

Currently I use the following line to create folders using the date as the name:

mkdir $(date +%F)

…Does anyone know what the syntax would be if I wanted to add the time in there as well?

How about:

mkdir $(date +%F--%T)

==> 2011-05-07–01:23:45

Look at the man page for date for other format specifiers. It would be something you add, like this:

mkdir $(date +%F-%something)

I can tell you but you should learn for yourself.

When you do not even understand why this works as it works, how would you be able to extend on it. When using something like this you should at least read short through

man mkdir
man date

to give you an idea what they do. And then you would have remembered to have a more thourough read through the last one to solve your wish.

The only thing with that is that the file name is not compatible with windows. Is there a way to make the file name contain the time but without colons?

Look at the list of formatters available for date in the man page and make up your own string from the hours, minutes and seconds formatters that doesn’t contain the colons.

Hint: If you still don’t get it, what follows the + is just a string where various formatters beginning with % are substituted with dynamic values. You can make up your own string, it doesn’t have to be of the form Lord_Emsworth showed you. You could have something like:

mkdir $(date +‘DirectoryCreated-%F’)

and the directory wil be named ‘DirectoryCreated-2011-05-07’ or whatever the date is. Get it?

I see two possibilities here:
a) change Windows;
b) study the man page as several people told you to do.

I guess I’m blind. For some reason I don’t see the formatters within the man page.

The section starting:

FORMAT controls the output. Interpreted sequences are:

PS: It’s the man page for date you’re are looking at, right? man 1 date?

My system:

henk@boven:~> man date
Man: alle passende pagina's vinden, niet alleen eerste
 * date (1)
   date (1p)
Man: Welke man-pagina wilt u zien?
Man: 1

shows:

DATE(1)                                           User Commands                                           DATE(1)



NAME
       date - print or set the system date and time

SYNOPSIS
       date [OPTION]... +FORMAT]
       date -u|--utc|--universal] [MMDDhhmm[[CC]YY].ss]]

DESCRIPTION
       Display the current time in the given FORMAT, or set the system date.

       -d, --date=STRING
              display time described by STRING, not `now'

       -f, --file=DATEFILE
              like --date once for each line of DATEFILE

       -r, --reference=FILE
              display the last modification time of FILE

       -R, --rfc-2822
              output date and time in RFC 2822 format.  Example: Mon, 07 Aug 2006 12:34:56 -0600

       --rfc-3339=TIMESPEC
              output  date and time in RFC 3339 format.  TIMESPEC=`date', `seconds', or `ns' for date and time to
              the indicated precision.  Date and time components are separated  by  a  single  space:  2006-08-07
              12:34:56-06:00

       -s, --set=STRING
              set time described by STRING

       -u, --utc, --universal
              print or set Coordinated Universal Time

       --help display this help and exit

       --version
              output version information and exit

       FORMAT controls the output.  Interpreted sequences are:

       %%     a literal %

       %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 ( 0..23)

       %l     hour ( 1..12)

       %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


and so on.

And after choosing 1p it shows similar for the POSIX version.

What do you get? Only telling what you do not get is not not very illuminating for us.

I was searching the wrong man page for sure… Thanks.

Just so you understand, there are two commands on that line. First the date command within $() is run, and its output is used as the argument to the mkdir command. This is called command substitution and man 1 bash will give you the details. There is an older form of $() which is `` and if you are using a Unix machine with an older shell, you need to use that form.