y0ga
y0ga

Reputation: 31

Create multiple files with same names in linux

I need to create 8 txt files wiht name text[x], where X is number from 1 to 8. Is there any simple construction to do that? I thought to use iteration. The simple method like:

touch text1.txt text2.txt text3.txt text4.txt text5.txt text6.txt text7.txt text8.txt

is not acceptable.

Upvotes: 3

Views: 4519

Answers (2)

Barton Chittenden
Barton Chittenden

Reputation: 4416

You can do this without the loop by using brace expansion in the file name:

touch text{1..8}.txt

See brace expansion in the bash man pages. Unlike wildcard expansion, the file names do not have to exist for brace expansion.

Upvotes: 3

William Cates
William Cates

Reputation: 113

Basic for loop construction with echoing the iteration 1 - 8 as variable 'i', which you use as a piece of the file name created by touch.

for i in `echo {1..8}`
do touch text$i.txt
done

As a one liner:

for i in `echo {1..8}`; do touch text$i.txt; done

But, I think you just had the answer in a simpler format in your comments.

Upvotes: 1

Related Questions