nos*_*mus 3 shell bash directory filenames touch
我正在尝试从 2 个变量创建文件名。
这是错误消息:
touch: cannot touch `/root/tinstalls/2--06/06/15': No such file or directory
2--06/06/15 19:54
Run Code Online (Sandbox Code Playgroud)
这是代码:
tdate=$(date '+%D %R')
tfile=$(echo "${toadd}--${tdate}")
touch /root/tinstalls/${tfile}
echo $tfile
Run Code Online (Sandbox Code Playgroud)
目录在那里。
@Theophrastus 有正确的想法。根据POSIX “[t]组成[文件]名称的字符可以从所有字符值的集合中选择,不包括斜杠字符和空字节”(我的重点)。换句话说,两个斜杠之间的每个字符串(空字符串除外)都是另一个目录,您不能创建名称包含斜杠的文件。因此,当您尝试时touch /root/tinstalls/2--06/06/15
,系统会尝试15
在具有绝对路径的目录中创建文件/root/tinstalls/2--06/06
。
一个简单的修改方法是替换文件名中的所有斜线,例如用下划线:
touch "/root/tinstalls/${tfile//\//_}"
Run Code Online (Sandbox Code Playgroud)