Unix - 创建文件夹和文件的路径 [英] Unix - create path of folders and file
问题描述
我知道你可以用mkdir
来创建一个目录和touch
来创建一个文件,但是没有办法一次完成这两个操作吗?
I know you can do mkdir
to create a directory and touch
to create a file, but is there no way to do both operations in one go?
即如果我想在文件夹 other
不存在时执行以下操作:
i.e. if I want to do the below when the folder other
does not exist:
cp /my/long/path/here/thing.txt /my/other/path/here/cpedthing.txt
错误:
cp: cannot create regular file `/my/other/path/here/cpedthing.txt': No such file or directory
有没有人想出一个函数来解决这个问题?
Has anyone come up with a function as a workaround for this?
推荐答案
使用 &&
在一个 shell 行中组合两个命令:
Use &&
to combine two commands in one shell line:
COMMAND1 && COMMAND2
mkdir -p /my/other/path/here/ && touch /my/other/path/here/cpedthing.txt
注意:以前我建议使用 ;
来分隔这两个命令,但正如@trysis 指出的那样,在大多数情况下使用 &&
可能更好,因为如果 COMMAND1
失败 COMMAND2
也不会被执行.(否则这可能会导致您可能没有预料到的问题.)
Note: Previously I recommended usage of ;
to separate the two commands but as pointed out by @trysis it's probably better to use &&
in most situations because in case COMMAND1
fails COMMAND2
won't be executed either. (Otherwise this might lead to issues you might not have been expecting.)
这篇关于Unix - 创建文件夹和文件的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!