一个在其中创建目录和文件的命令linux命令 [英] One command to create a directory and file inside it linux command
问题描述
假设我的当前目录为 A .我想在 B 中创建目录 B 和文件"myfile.txt".
Suppose my current directory is A. I want to create a directory B and a file "myfile.txt" inside B.
如何在Terminal的一个命令中做到这一点?
How to do that in one command from Terminal?
目录可以嵌套多次.就像我可能要创建 B/C/D ,然后在其中创建"myfile.txt".我也不想重复目录部分.
Directory can be nested multiple times. Like I may want to create B/C/D and then "myfile.txt" inside that. I do not also want to repeat the directory part.
以下命令将在任何级别创建目录.
Following command will create directory at any level.
mkdir -p B/C/D
和
mkdir -p B/C/D && touch B/C/D/myfile.txt
将创建目录和文件.但是我不想在touch
命令之后重复目录部分.有可能吗?
will create the directory and the file. But I do not want to repeat the directory part after the touch
command. Is that possible?
推荐答案
mkdir B && touch B/myfile.txt
或者,创建一个函数:
mkfile() { mkdir -p -- "$1" && touch -- "$1"/"$2" }
使用2个参数执行它:创建路径和文件名.说:
Execute it with 2 arguments: path to create and filename. Saying:
mkfile B/C/D myfile.txt
将在目录B/C/D
中创建文件myfile.txt
.
这篇关于一个在其中创建目录和文件的命令linux命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!