We help IT Professionals succeed at work.

date command

lolaferrari
lolaferrari asked
on
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
Comment
Watch Question

Keith BrownAWS System Administrator
CERTIFIED EXPERT

Commented:
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.
CERTIFIED EXPERT
Most Valuable Expert 2013
Top Expert 2013
Commented:
Hi,

that's due to the slashes ( / ) in the "date" format you've chosen and which cannot be part of a filename.

Consider using a different format, like

time=$(date "+%m-%d-%y.%H")
touch file"$time"


wmp
Kent WSr. Network / Systems Admin
CERTIFIED EXPERT

Commented:
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.
CERTIFIED EXPERT
Top Expert 2007

Commented:
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
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
does the directory file11/01/11/ exist?
Kamaraj SubramanianApplication Support Analyst

Commented:
#!/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

Author

Commented:
many thanks

Explore More ContentExplore courses, solutions, and other research materials related to this topic.