Help us understand the problem. What is going on with this article?

連番の空ファイルを生成するワンライナー

More than 5 years have passed since last update.
$ for count in `seq -w 100`;do :> file_$count;done

生成結果

$ for count in `seq -w 100`;do :> file_$count;done
$ ls
file_001 file_021 file_041 file_061 file_081
file_002 file_022 file_042 file_062 file_082
file_003 file_023 file_043 file_063 file_083
file_004 file_024 file_044 file_064 file_084
file_005 file_025 file_045 file_065 file_085
file_006 file_026 file_046 file_066 file_086
file_007 file_027 file_047 file_067 file_087
file_008 file_028 file_048 file_068 file_088
file_009 file_029 file_049 file_069 file_089
file_010 file_030 file_050 file_070 file_090
file_011 file_031 file_051 file_071 file_091
file_012 file_032 file_052 file_072 file_092
file_013 file_033 file_053 file_073 file_093
file_014 file_034 file_054 file_074 file_094
file_015 file_035 file_055 file_075 file_095
file_016 file_036 file_056 file_076 file_096
file_017 file_037 file_057 file_077 file_097
file_018 file_038 file_058 file_078 file_098
file_019 file_039 file_059 file_079 file_099
file_020 file_040 file_060 file_080 file_100

  • seq -wオプションを使ってゼロパディング対応
  • touchよりも:>を使ったほうが空ファイル生成が早かった

※13/10/1追記

もっと早い方法があった

$ seq -f "file_%05g" 100 | xargs touch
  • 作成するファイル名をxargsに渡して作成コマンド実行回数を減らした
  • ゼロパディングは作成個数に応じてseq -fオプションのフォーマットを調整

10万ファイル作成時間を比較

$ time for count in `seq -w 100000`;do :> file_$count;done
real 0m1.607s
user 0m0.092s
sys 0m1.512s

$ time seq -f "file_%06g" 100000 | xargs touch
real 0m0.269s
user 0m0.052s
sys 0m0.248s

早くなりました。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away