0

I am new to unix and I am starting to get my head around it slowly. I would like to know how I can create multiple files within an folder in my directory. Untill now I was doing it manually one by one. I was using the following code:

echo 'random text here' > newfile1.txt

I would like to create a script that will do the following:

new file x n

newfile1.txt
newfile2.txt
newfile3.txt
newfilen.txt

I would like to have some dummy text inside the files to. I am learning vim and bash.

Thank You

2
  • stackoverflow.com/questions/17034941/… Commented Jan 8, 2016 at 13:45
  • 1
    What does Vim have to do with this question? Anything? Commented Jan 8, 2016 at 15:30

4 Answers 4

1

Here is another possibility:

for i in file{1..3}; do printf "random text\n" > $i; done

zsh also allows the more simpler:

printf "random text\n" > random_text{1..3}
Sign up to request clarification or add additional context in comments.

Comments

0

One way:

seq 3 | awk '{print "random text" > "newfile"i++".txt"}' i=1 

In place of 3, put the value of n.

Comments

0
#!/bin/bash

for n in `seq 1 10`; do
        echo "$3" > $1/$2$n.txt
done

This script takes in 3 command line arguments, the first is the already created folder you want the files created in, the second is the first part of the filename, the third is dummy text you want inserted. :)

./script.sh my_folder basename dummytext

(provided as a bash answer)

Comments

0

A classic for loop would be good. This is just for fun. (don't use in your script)

yes "foo text"|head -10|awk '{print >"newFile"NR".txt"}'

Sometimes we should use yes command, to show that we don't forget him, or he is gonna be sad. :-)

1 Comment

It's ok, yes is not going to be sad... @glenn-jackman found a brilliant use for him... stackoverflow.com/a/34556470/2836621 :-)

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.