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?

2 Answers 2

12

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.

2

Brace expansion is done by the shell, nothing to do with touch itself. So the fact that your files have different names doesn't matter. ie This will also work:

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

But obviously it makes sense to use the brace expansion when you can. My point is that this will still work even if the files have totally different names:

touch a b c d
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .