tags:

views:

192

answers:

6

I need to make files called from File1, File2, ... to File99.

I tried the following unsuccessfully

cat test > {File1 .. File99}

The command without the word File did not work.

+4  A: 

This will depend on the shell you are using. I assume you are using Bash.

According to http://www.gnu.org/software/bash/manual/bashref.html#Brace-Expansion, Bash will expand digits and single characters. So your brace expression should be:

File{1..99}

But I don't think the redirection operator ">" can output to multiple files. You might need to use a loop:

for output in File{1..99}
do
    cat test > $output
done

Or as a one-liner:

for output in File{1..99}; do cat test > $output; done
Joe Koberg
I tried your command as a one-liner unsuccessfully: $ for output in File{1..99}; do; cat test > $output; done
Masi
I think you have an extra semicolon. The following works for me: for output in File{1..99}; do cat test > $output; done
Joe Koberg
It works with $ for output in File{1..99}; do cat test > $output; done
Masi
I like also a combination of your and Bryan's codes like $ for i in {1..99}; do cat test > File$i; done
Masi
I wonder how you could make the same code in Python as a one-liner. It could be even shorter.
Masi
I dont know. here's a shot: """t=open("test","rb").read(); [open("File%d"%(i,),"wb").write(t) for i in range(1,100)]"""
Joe Koberg
"""import shutil; [shutil.copy("test", "File%d"%(i,)) for i in range(1,100)]"""
Joe Koberg
@Joe: I did not get the Python code to work. Could you please dpaste the code.
Masi
+5  A: 
$ for i in {1..100}; do touch "File$i"; done
BryanH
+4  A: 

If you prefer a non looping version then you can use tee

cat test | tee File{1..99} > /dev/null
phyrex1an
Your code also works without /dev/null. Why do you use it?
Masi
@Masi: Mostly to be equivalent with the other answers. Another possible reason, although not mine, is to prevent a long output that isn't needed.
phyrex1an
'tee' would otherwise copy the output to the terminal as well as the files.
Joe Koberg
+3  A: 

With zsh (with its *mult_ios*) you can :)

% zsh -c 'print test > file{1..3}' 
% head file*                      
==> file1 <==
test

==> file2 <==
test

==> file3 <==
test
radoulov
+4  A: 

Just one command:

touch File{1..99}
FJRA
Concise. I presumed you WANTED the contents of file "test" in the new files.
Joe Koberg
+1  A: 

If you want the files to sort properly (file01, file02 ... file10, etc.), do this:

for i in {0..10}; do i="0"$i; touch file${i: -2}; done

Which is the same as:

for i in {0..10}
do
    i="0"$i
    touch file${i: -2} # or cat file > file${i: -2}
done

There must be be a space between the colon and the dash in the substring expansion clause. You can start the ranges above with 1 if you want to start with "file01".

Or, a much more succinct way to have leading zeros:

touch file{0..9}{0..9}

and to use phyrex1an's technique with this one:

cat test | tee File{0..9}{0..9} > /dev/null

This does make one extra file "file00". You can also do "{2..3}{0..9}" for "file20" through "file39", but changing the ones digit (the second range) would cause parts of the sequence to be skipped.

Dennis Williamson