LoginSignup
16
15

More than 5 years have passed since last update.

連番ファイルをコマンド一発で生成する

Posted at

test_1.txt, test_2.txt...test_30.txtのような連番のファイルを作りたくなる時があります。
そんな時、1つ1つcpコマンドでコピーとかしてるようでは超絶だるいですよね。
そんな悩みをコマンド一発で解決する方法を紹介します。

  • 実践
[riki@hoge]$ touch test_{1..30}.txt
[riki@hoge]$ ls
test_1.txt   test_11.txt  test_13.txt  test_15.txt  test_17.txt  test_19.txt  test_20.txt  test_22.txt  test_24.txt  test_26.txt  test_28.txt  test_3.txt   test_4.txt  test_6.txt  test_8.txt
test_10.txt  test_12.txt  test_14.txt  test_16.txt  test_18.txt  test_2.txt   test_21.txt  test_23.txt  test_25.txt  test_27.txt  test_29.txt  test_30.txt  test_5.txt  test_7.txt  test_9.txt

出来ました。

書式

{開始番号..終了番号}
記号 意味
{ 開始を意味する
開始番号 連番の最初の数字(1~30で作りたいなら1)
終了番号 連番の最後の数字(1~30で作りたいなら30)
} 終了を意味する

応用してみる

アルファベットでも使ってみる

a~zとかも楽勝

[riki@hoge]$ touch test_{a..z}.txt
[riki@hoge]$ ls
test_a.txt  test_c.txt  test_e.txt  test_g.txt  test_i.txt  test_k.txt  test_m.txt  test_o.txt  test_q.txt  test_s.txt  test_u.txt  test_w.txt  test_y.txt
test_b.txt  test_d.txt  test_f.txt  test_h.txt  test_j.txt  test_l.txt  test_n.txt  test_p.txt  test_r.txt  test_t.txt  test_v.txt  test_x.txt  test_z.txt

桁を揃えてみる

ゼロを先頭に書くと、いい感じにゼロ埋めしてくれる

[riki@hoge]$ touch foo_{01..100}.txt
[riki@hoge]$ ls
foo_001.txt  foo_009.txt  foo_017.txt  foo_025.txt  foo_033.txt  foo_041.txt  foo_049.txt  foo_057.txt  foo_065.txt  foo_073.txt  foo_081.txt  foo_089.txt  foo_097.txt
foo_002.txt  foo_010.txt  foo_018.txt  foo_026.txt  foo_034.txt  foo_042.txt  foo_050.txt  foo_058.txt  foo_066.txt  foo_074.txt  foo_082.txt  foo_090.txt  foo_098.txt
foo_003.txt  foo_011.txt  foo_019.txt  foo_027.txt  foo_035.txt  foo_043.txt  foo_051.txt  foo_059.txt  foo_067.txt  foo_075.txt  foo_083.txt  foo_091.txt  foo_099.txt
foo_004.txt  foo_012.txt  foo_020.txt  foo_028.txt  foo_036.txt  foo_044.txt  foo_052.txt  foo_060.txt  foo_068.txt  foo_076.txt  foo_084.txt  foo_092.txt  foo_100.txt
foo_005.txt  foo_013.txt  foo_021.txt  foo_029.txt  foo_037.txt  foo_045.txt  foo_053.txt  foo_061.txt  foo_069.txt  foo_077.txt  foo_085.txt  foo_093.txt
foo_006.txt  foo_014.txt  foo_022.txt  foo_030.txt  foo_038.txt  foo_046.txt  foo_054.txt  foo_062.txt  foo_070.txt  foo_078.txt  foo_086.txt  foo_094.txt
foo_007.txt  foo_015.txt  foo_023.txt  foo_031.txt  foo_039.txt  foo_047.txt  foo_055.txt  foo_063.txt  foo_071.txt  foo_079.txt  foo_087.txt  foo_095.txt
foo_008.txt  foo_016.txt  foo_024.txt  foo_032.txt  foo_040.txt  foo_048.txt  foo_056.txt  foo_064.txt  foo_072.txt  foo_080.txt  foo_088.txt  foo_096.txt
16
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
15