Link to home
Create AccountLog in
Avatar of lolaferrari
lolaferrariFlag for United Kingdom of Great Britain and Northern Ireland

asked on

date command

Can someone tell me what is wrong with this?

#!/bin/bash

time="`date "+%m/%d/%y/%H"`"
touch file"$time"

if I run it i get the following

touch: cannot touch `file11/01/11/22': No such file or directory
Avatar of Keith Brown
Keith Brown
Flag of United States of America image

What are you wanting to get?

as you have it set, it gets the month, followed by the day, then the 2 digit year, and then is the hour, with it being on a 24 hour clock.

As you have it set, it is trying to naviate to ./file11/01/11/ and change the last modified timestamp of a file named 22 in your example. Remember using / like that says divides things up as if it is a directory.
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
change your /'s to -'s and it works,  However, if you are trying to make nested directories based on the date, you'll need another approach.
It is not advisable to use / in file name since it is a separator for directories and sub directories and if possible at all to create a file with / in its name, then it will cause a lot of issues while trying to use the file with such file name. You may use other characters such as - or _  in file name
does the directory file11/01/11/ exist?
#!/bin/bash

time=`date "+%m/%d/%y/%H"` #make sure you have a directory with 11/02/11 - otherwise it will fail
mkdir -p $time
cd $time
touch file

Open in new window

Avatar of lolaferrari

ASKER

many thanks