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?
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.
For a Windows batch-file, you'd just use mkdir "%DATE%" or mkdir "%datestamp%" -- whichever variable you want to use.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With