3

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.

2
  • 4
    In Bash, using brace expansion: touch text{1..8}.txt Commented Feb 4, 2015 at 12:27
  • 1
    @gniourf_gniourf that's the answer, so shouldn't be a comment! Commented Feb 4, 2015 at 12:52

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

As you've suggested, you don't need to use a loop anyway but if you're going to, you certainly don't need to use process substitution as well. for i in {1..8} would be sufficient.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.