Creating dir with date and then increment by 1

Hi, I can't seem to do this or find how to do it. Can someone please help:

I have a dir and within it I have dirs with date name example

Test1/
2017-08-12
2017-09-13
2017-10-14
2017-11-15
2017-11-16

what I want is a a script to scan the above and then take 2017-11-16 and increase this by 1 to create the next days folder which would be 2017-11-17

can someone please help

Questions like this are easy with the date utility available on some operating systems and with some shells.

What operating system (including version) are you using?

What shell (including version) are you using?

I am using
RHL 6.2
shell version: BASH 4.1.2

Is it OK to presume you want the LATEST directory incremented (and not an arbitrary entry of that list)?

Hi RudiC yes thats correct

Would

mkdir $(date -d"$(ls -d [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] | tail -1)+1day" +"%Y-%m-%d")
  • given your current working directory is the target directory - come close to what you need?
1 Like

Hi RudiC,

I ran this and it created me a folder with 2017-11-22 date. What I was expecting was to go through the list: as below

Test1/
2017-08-12
2017-09-13
2017-10-14
2017-11-15
2017-11-16

and then create me the next folder with date 2017-11-17 .

That's strange. With a directory contents of 2017-11-16 , for me it yields

date -d"$(ls -d [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] | tail -1)+1day" +"%Y-%m-%d"
2017-11-17

and not -22 . Are there other files (NOT directories)?

Have you considered ksh93 shell builtin printf date options ?
I have found that to be quite portable across operating systems.

Relaying on GNU date is worst then relaying on ksh93 in my opinion :wink:

Regards
Peasant

Note that if israr75 ran the above date command in the directory containing Test1 (instead of in directory Test1) and there aren't any matching files there, there will be a diagnostic message saying no matching files found from ls written to stderr and nothing written to stdout, the tail will return no output, and the date -d option argument will just be +1day instead of something like 2017-11-16+1day .

One might guess that a cd Test1 at the start of the script might solve the problem, but this is just wild conjecture.

My preferred shell is also ksh . It is frequently faster than bash, can handle floating point expressions in addition to handling integer expressions in arithmetic commands and arithmetic expressions, has associative and indexed arrays (associative arrays came later in bash and are declared differently than in ksh ), and always runs the last element of a pipeline in the current shell execution environment (available as a settable option in recent bash 4.x ). But:

printf '%(date_format_specifications)T' [optional_date/time_specification]

isn't available in all versions of ksh . It is at least present in ksh 93u+ and later versions, but I'm not sure when it first appeared. It was not in the original ksh93 releases. And, like GNU date , ksh93 isn't generally available on some systems (Solaris 10, doesn't have GNU date or ksh93 by default).

This is why it is so crucial for the first post in each thread in these forums to tell us what operating system (including version) and shell (including version) is being used. In this thread, where we know that the OS is Red Hat release 6.2, GNU date should be readily available (and its use didn't give diagnostics about -d being an unrecognized option).

Portable, in ways as ksh93 could be easily installed anywhere.
While gnu date would require much more effort and disk space.

So if i had a date manipulation requirement in script across a multiverse of unixes, i would go with ksh.
It's the closest one can get regarding dates and shell at minimum requirements on modern systems.

Of course, not including full blown scripts or external utilities (awk, perl, python..) to handle date manipulations.

Regards
Peasant.

1 Like