8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2013-09-26
$ 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

早くなりました。

8
8
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
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?