Cons
Cons

Reputation: 1193

Linux/ make folder with date name

I try to create folder in my server with the current date.

So I wrote the next lines:

$ mkdir # date +”%d-%m-%Y”
cd # date +”%d-%m-%Y”

and save it as .sh, But for somme reasom it doesn't work. What can be the reason?

Upvotes: 6

Views: 19555

Answers (1)

Noctua
Noctua

Reputation: 5218

You should use

mkdir "$(date +"%d-%m-%Y")"
cd "$(date +"%d-%m-%Y")"

In the extreme case a day passes between the first and the second statement, that won't work. Change it to:

d="$(date +"%d-%m-%Y")"
mkdir "$d"
cd "$d"

Explanation: The $(...) returns the output from the subcommands as a string, which we store in the variable d.

(Quoted the variables as suggested by tripleee)

Upvotes: 18

Related Questions