Unix的 - 创建文件夹和文件的路径 [英] Unix - create path of folders and file

查看:175
本文介绍了Unix的 - 创建文件夹和文件的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道你能行的mkdir 以创建一个目录,触摸来创建一个文件,但有没有一气呵成的方式做这两个操作?

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?

即。如果我想要做的时候下面的文件夹不存在:

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?

推荐答案

使用&放大器;&安培; 来两个命令在一个外壳线结合:

Use && to combine two commands in one shell line:

COMMAND1 && COMMAND2
mkdir -p /my/other/path/here/ && touch /my/other/path/here/cpedthing.txt

请注意:previously我推荐的使用; 来的两个命令分开,但正如@trysis指出,它可能会更好使用 &功放;&安培; 在大多数情况下,因为万一 COMMAND1 失败命令2 将不无论是执行。 (否则,这可能会导致你可能不会一直期待的问题。)

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆