Making the title of a directory the date in bash?

Christian picture Christian · Aug 20, 2011 · Viewed 11k times · Source

I want to make the current date into the title of a directory in /home/chris/Downloads by using mkdir and date -I

I tried mkdir "date -I" that gets me a folder named "date -I" Without the quotes it gives the error

mkdir: invalid option -- 'I'

Trying to make it a variable next

date= date -I
mkdir -p $date

with the -p option, it looked good, but upon inspection, the folder wasn't created. removing -p gets me the error

mkdir: cannot create directory `/home/chris/Downloads/': File exists

and even pointing it to the entire path

date= date -I
mkdir "/home/chris/Downloads/$date"

gets me the same error as before

It's not that the variable is empty, I echo'd it and the value is what I should expect, it seems to be that the value isn't substituted before the directory is created. What would be the way to get around this problem? I'm running Ubuntu 11.04 (Natty Narwhal) if that gives you any more info.

Answer

Thomas Berger picture Thomas Berger · Aug 20, 2011

Your syntax is wrong:

mkdir -p /home/chris/downloads/$(date -I)

or

mkdir -p /home/chris/downloads/`date -I`

will work