Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making the title of a directory the date in bash?

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.

like image 935
Christian Avatar asked Aug 19 '11 23:08

Christian


People also ask

How do I create a directory using the date in Linux?

The command is date +%Y%m%d so that a directory for today would be 20060811.

What's do `` $() ${} commands do in bash?

Save this answer. Show activity on this post. $() means: "first evaluate this, and then evaluate the rest of the line". On the other hand ${} expands a variable.

How do you set a date variable in bash?

To get the date in bash shell and add a date, you can use the GNU date command as date -d 'Feb 12 +1 day' +%F which will output 2020-02-13 . See the above section on GNU date for more examples on the -d option.

What does %% mean in bash?

So as far as I can tell, %% doesn't have any special meaning in a bash function name. It would be just like using XX instead. This is despite the definition of a name in the manpage: name A word consisting only of alphanumeric characters and under- scores, and beginning with an alphabetic character or an under- score.


2 Answers

Your syntax is wrong:

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

or

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

will work

like image 166
Thomas Berger Avatar answered Sep 20 '22 23:09

Thomas Berger


Use this: backticks run the command instead of printing it out.

mkdir `date -I`
like image 27
Femi Avatar answered Sep 22 '22 23:09

Femi