Warm tip: This article is reproduced from serverfault.com, please click

One command to create a directory and file inside it linux command

发布于 2013-08-01 06:25:56

Suppose my current directory is A. I want to create a directory B and a file "myfile.txt" inside B.

How to do that in one command from Terminal?

Edit:

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 

and

mkdir -p B/C/D && touch B/C/D/myfile.txt

will create the directory and the file. But I do not want to repeat the directory part after the touch command. Is that possible?

Questioner
chanchal118
Viewed
0
devnull 2014-05-29 01:55:36
mkdir B && touch B/myfile.txt

Alternatively, create a function:

mkfile() { mkdir -p -- "$1" && touch -- "$1"/"$2" }

Execute it with 2 arguments: path to create and filename. Saying:

mkfile B/C/D myfile.txt

would create the file myfile.txt in the directory B/C/D.