Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create multiple files within (sub)directory

How can I create multiple files within the same directory/sub directory using one command in the terminal?

I know its possible with

touch ./path/to/the/directory/file1.txt  ./path/to/the/directory/file2.txt

but looking to see if I can skip entering the fullpath for every file.

I thought touch ./path/to/the/directory/{file1.txt file2.txt} would do the trick, but seems no.

like image 300
ltdev Avatar asked Oct 23 '25 05:10

ltdev


1 Answers

Yes, you can create multiple files within the same directory/sub-directory using one command in the terminal.

we can use touch command and {} wildcard to create multiple files in a single command. Here's an example command,

  • An example command that does exactly what you want:

    touch ./path/to/the/directory/file{1..3}.txt
    

    This command creates file1.txt, file2.txt and file3.txt files inside the sub-directory.

  • Creating multiple files having completely different names:

    touch ./path/to/the/directory/{readme,utils}.txt
    

    This command creates readme.txt, utils.txt files inside the sub-directory.

  • Creating multiple files having different file extensions:

    touch ./path/to/the/directory/{index.html,style.css}
    

    This command creates index.html, style.css files inside the sub-directory.

PS: Above commands are using brace expansion. A brace expansion is contained between a pair of braces “{}”. It can be a list of comma-separated items or a range specifier.

like image 84
Yogesh Yadav Avatar answered Oct 25 '25 23:10

Yogesh Yadav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!