PDA

View Full Version : Short HOWTO: Create several files with or w/o the same extension



kyonides
May 26th 2010, 02:09 AM
In order to be able to create several files at the same time you can type this in konsole or xterm.

touch somefile{1,2,3,4,5}.txt

# Results

somefile1.txt, somefile2.txt and so on

touch somefile.{cpp,h,pro,ui}

# Results

somefile.cpp, somefile.h, somefile.pro, somefile.ui

It's up to you where you place the {} parentheses but remember that the results are not the same.

Telengard
Jul 1st 2010, 12:58 AM
touch somefile{1,2,3,4,5}.txt


Pretty cool, but it can be done even simpler:


$touch somefile{1..10}.txt
$ ls -1 somefile*
somefile10.txt
somefile1.txt
somefile2.txt
somefile3.txt
somefile4.txt
somefile5.txt
somefile6.txt
somefile7.txt
somefile8.txt
somefile9.txt

The ".." syntax inside brace expressions causes BASH to iterate from the first character to the second character.


A sequence expression takes the form `{X..Y}', where X and Y are either integers or single characters. When integers are supplied, the expression expands to each number between X and Y, inclusive. When characters are supplied, the expression expands to each character lexicographically between X and Y, inclusive. Note that both X and Y must be of the same type.

So you don't have to necessarily use integers in the expression. This also works:


$ touch somefile_{a..z}.txt
$ ls -w 80 somefile_*
somefile_a.txt somefile_g.txt somefile_m.txt somefile_s.txt somefile_y.txt
somefile_b.txt somefile_h.txt somefile_n.txt somefile_t.txt somefile_z.txt
somefile_c.txt somefile_i.txt somefile_o.txt somefile_u.txt
somefile_d.txt somefile_j.txt somefile_p.txt somefile_v.txt
somefile_e.txt somefile_k.txt somefile_q.txt somefile_w.txt
somefile_f.txt somefile_l.txt somefile_r.txt somefile_x.txt