3

My current directory structure

├── index.html
├── src
│   └── App.js
└── README.md

Now, I wanted to create files App.js, Body.js and Footer.js in a new directory named components. So, my final structure will be:

├── index.html
├── src
│   ├── App.js
│   └── components
│       ├── Header.js
│       ├── Body.js
│       └── Footer.js
└── README.md

I tried something like this:

mkdir -p src/components && touch $_/Header.js
touch src/components/Body.js  src/components/Footer.js

From reference, I know to create multiple files at once, we can use

touch newfilename{1..N}

But, my filenames are having different names.

Is there a way to create all these files in a new directory using a single command with terminal?

1 Answer 1

11

Yes, you can create multiple files with different names in a single command using terminal. Here's an example command that does exactly what you want:

mkdir -p src/components && touch src/components/{Header,Body,Footer}.js

This command first creates the components directory inside the src directory using mkdir -p src/components. Then, it uses the touch command to create the Header.js, Body.js, and Footer.js files inside the components directory. The {Header,Body,Footer} is brace expansion, which allows you to create multiple file names with a single pattern.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.