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?
This might work:
For this purpose, you can create your own function. For example:
Explanation:
~/.bashrc
file using theecho
command-p
flag is for creating the nested folders, such asfldr2
~/.bashrc
file with thesource
commandmkfile
function to create the fileAlternatively, create a function:
Execute it with 2 arguments: path to create and filename. Saying:
would create the file
myfile.txt
in the directoryB/C/D
.Just a simple command below is enough.
Thx
devnull's answer provides a function:
This function did not work for me as is (I'm running bash 4.3.48 on WSL Ubuntu), but did work once I removed the double dashes. So, this worked for me: