chanchal118
chanchal118

Reputation: 3647

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

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?

Upvotes: 47

Views: 179906

Answers (13)

Michael Mantion
Michael Mantion

Reputation: 215

If you're willing to install a third-party app, Bonk is written in Rust and is a greatly improved version of touch.

To install from crates.io:

cargo install bonky

Create nested file inside nested directories:

bonk src/bonky/mod.rs

I normally prefer vanilla solutions, but bonk adds so many features that I feel it is superior to creating functions and other work arounds. It looks like a new program and the code appears safe to me.

Upvotes: 0

Ervan Kurniawan
Ervan Kurniawan

Reputation: 81

mkdir -p Python/Beginner/CH01 && touch $_/hello_world.py

Explanation:

-p -> create parent and child directories

$_ -> use it for current directory we work with inline

Upvotes: 8

Yibrah Amare
Yibrah Amare

Reputation: 1

sudo mkdir B && sudo vim B/myfile.txt

you can leave your file.txt empty.

Upvotes: 0

Robert Lowstetter
Robert Lowstetter

Reputation: 1

I know I am late to the show but, here is how I typically approach this in bash:

mkdir -p "$(dirname "/path/to/file.extension")" && touch "/path/to/file.extension"

dirname will grab the folder structure and create it for you. The touch command will create your actual file.

I hope this helps!

Upvotes: 0

Rezan Moh
Rezan Moh

Reputation: 365

1-to make your own command add this to ~/.bashrc:

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

save and then to make it available without a reboot or logout execute: $ source ~/.bashrc then you can use it: mkfile myfolder myfile.txt

2-or without new command you can just type:

$ mkdir folder && touch $_/file.txt

note that $_ = folder here

Upvotes: 4

chety
chety

Reputation: 156

Below commands I tried on Windows 10 machine with git-bash. And that worked for me. Let's say I want to create a directory named "Files" and under this directory I want to have "file1.txt,file2.txt,file3.txt" ..etc files

mkdir Files && touch $_/file1.txt $_/file2.txt $_/file3.txt

Upvotes: 0

Damon
Damon

Reputation: 1

you can install the script ;

pip3 install --user advance-touch

After installed, you can use ad command

ad airport/plane/captain.txt
airport/
├── plane/
│   ├── captain.txt

Upvotes: 0

Krzysztof Darasz
Krzysztof Darasz

Reputation: 91

You could create a function that parses argument with sed;

atouch() {
  mkdir -p $(sed 's/\(.*\)\/.*/\1/' <<< $1) && touch $1
}

and then, execute it with one argument:

atouch B/C/D/myfile.txt

Upvotes: 3

Rodrigo Andrade
Rodrigo Andrade

Reputation: 449

Just a simple command below is enough.

mkdir a && touch !$/file.txt

Thx

Upvotes: 3

chandan
chandan

Reputation: 1

This might work:

mkdir {{FOLDER NAME}}
cd {{FOLDER NAME}}
touch {{FOLDER NAME}}/file.txt

Upvotes: -5

Ilya Zarembsky
Ilya Zarembsky

Reputation: 151

devnull's answer provides a function:

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

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:

echo 'mkfile() { mkdir -p "$1" && touch "$1"/"$2" }' >> ~/.bash_profile
source ~/.bash_profile
mkfile sample/dir test.file

Upvotes: 0

Purkhalo Alex
Purkhalo Alex

Reputation: 3627

For this purpose, you can create your own function. For example:

$ echo 'mkfile() { mkdir -p "$(dirname "$1")" && touch "$1" ;  }' >> ~/.bashrc
$ source ~/.bashrc
$ mkfile ./fldr1/fldr2/file.txt

Explanation:

  • Insert the function to the end of ~/.bashrc file using the echo command
  • The -p flag is for creating the nested folders, such as fldr2
  • Update the ~/.bashrc file with the source command
  • Use the mkfile function to create the file

Upvotes: 9

devnull
devnull

Reputation: 123458

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.

Upvotes: 64

Related Questions