Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am trying to create a filename from 2 variables.

This is the error msg:

touch: cannot touch `/root/tinstalls/2--06/06/15': No such file or directory
2--06/06/15 19:54

This is the code:

tdate=$(date '+%D %R')
tfile=$(echo "${toadd}--${tdate}")
touch /root/tinstalls/${tfile}

 echo $tfile

The directory is there.

share|improve this question
    
To touch /root/tinstalls/2--06/06/15 19:54, you need to have directory /root/tinstalls/2--06/06 previously. Is it what you intended? And you need to quotes paths which include spaces. –  yaegashi yesterday

3 Answers 3

@Theophrastus has the right idea. According to POSIX "[t]he characters composing the [file] name may be selected from the set of all character values excluding the slash character and the null byte" (my emphasis). In other words, every string between two slashes (except the empty string) is another directory, and you cannot create a file with a name containing slashes. So when you try to touch /root/tinstalls/2--06/06/15, the system is trying to create the file 15 inside the directory with the absolute path /root/tinstalls/2--06/06.

A simple way to amend this would be to replace all slashes in the filename, for example with underscore:

touch "/root/tinstalls/${tfile//\//_}"
share|improve this answer

touch can't make directories. For instance see here

I see you have "The directory is there." but you do realize that your date format includes "/" characters which would require more directories, yes?

share|improve this answer

As others have already explained, the problem is that the slashes in your date make touch try to create a directory. Since it can't, it complains. The simplest solution is to change your date format. Instead of this:

$ date '+%D %R'
06/07/15 13:47

Use this:

 $ date '+%F %R'
 2015-06-07 13:52

Or, even better, avoid having to deal with spaces and use this:

$ date '+%F-%R'
2015-06-07-13:52

Finally, if you insist on having spaces, you must quote the name when passing it to touch

touch /root/tinstalls/"${toadd}--$(date '+%F %R')"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.