Page 1 of 2 12 LastLast
Results 1 to 10 of 12

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

  1. #1

    Default 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?

  2. #2
    Join Date
    Mar 2010
    Location
    Europe
    Posts
    657

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

    How about:
    Code:
    mkdir $(date +%F--%T)
    ==> 2011-05-07--01:23:45

  3. #3
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,686
    Blog Entries
    4

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

    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.

  4. #4
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    25,130

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

    Quote Originally Posted by abacabb View Post
    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?
    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
    Code:
    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.
    Henk van Velden

  5. #5

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

    Quote Originally Posted by Lord_Emsworth View Post
    How about:
    Code:
    mkdir $(date +%F--%T)
    ==> 2011-05-07--01:23:45
    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?

  6. #6
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,686
    Blog Entries
    4

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

    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?

  7. #7
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    25,130

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

    Quote Originally Posted by abacabb View Post
    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?
    I see two possibilities here:
    a) change Windows;
    b) study the man page as several people told you to do.
    Henk van Velden

  8. #8

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

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

  9. #9
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,686
    Blog Entries
    4

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

    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?

  10. #10
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    25,130

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

    My system:
    Code:
    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:
    Code:
    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.
    Henk van Velden

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •