Score:3

How to create multiple files (with different names) in a specific directory all at once in a Linux terminal

mg flag

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?

Score:11
fr flag

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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.