Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux/ make folder with date name

Tags:

linux

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?

like image 799
Cons Avatar asked May 27 '13 15:05

Cons


People also ask

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

mkdir $(date +%Y%m%d) or I personally use mkdir $(date +%Y%m%d_%H%M%S) for hh:mm:ss. date --help will give you the different formats if you need something more.

How do I create a folder for today's date?

For a Windows batch-file, you'd just use mkdir "%DATE%" or mkdir "%datestamp%" -- whichever variable you want to use.

How do I create a file from a specific date in Linux?

The touch command also allows us to update or create a file with a specific time other than the current time. Use the -d ( --date= ) option to specify a date string and use it instead of the current time. The date string needs to be enclosed in single quotes.


1 Answers

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)

like image 67
Noctua Avatar answered Oct 07 '22 16:10

Noctua