0
0

More than 5 years have passed since last update.

指定したサイズと数量でテスト用ファイルを生成

Posted at

はじめに

以前にファイルサーバーの移行を担当した時に、移行(ファイルコピー)に要する時間を推測するため、移行先ファイルサーバーの書き込み性能をテストしたことがありました。
その時は、テストのために「適当な容量とファイル数のサンプル」を用意するのが少々面倒でした。

そこで、「ファイルサイズ」「ファイル数」の2つを引数で指定して、ファイルを自動生成してくれるスクリプトを作ってみました。
あまりにもニッチ過ぎるスクリプトですが、どなたかのお役に立てるかもしれません...

スクリプト

スクリプトのオプションは以下の通りです。

  • -h
    • ヘルプの表示。
  • -s
    • 生成するファイルのサイズ。
    • 1KBであれば"1K"、1MBであれば"1M"、1GBであれば"1G"を指定する。
    • デフォルト値は1K。
  • -c
    • 生成するファイル数。
    • デフォルト値は1。

なお、スクリプトの末尾のecho -n -e "\n"は、プログレスバーの代わりに出力している「...」を改行するために入れています。

mkfile.sh
#!/bin/bash

function usage {
    cat <<EOS
$(basename ${0}) is a file-creation tool for test.
Usage:
    $(basename ${0}) [<options>]
Options:
    -s    print file size in human readable format(e.g., 10K, 5M, 1G). default is 1K.
    -c    print file counts. default is 1.
EOS
}

FILE_SIZE="1K"
COUNT=1

while getopts s:c:h OPT
do
  case $OPT in
    "s" ) FILE_SIZE="$OPTARG"
          ;;
    "c" ) COUNT="$OPTARG"
          ;;
    "h" ) usage;
          exit 0
          ;;
  esac
done

for i in $( seq 1 $((COUNT)) ); do
  dd if=/dev/zero of=${FILE_SIZE}_${i}.txt bs=$FILE_SIZE count=1 2> /dev/null
  echo -n "."
done

echo -n -e "\n"

使い方

  • $ ./mkfiles.sh -s 100M -c 20で、「100MBのファイルを20個生成する」ことができます。
    • 当たり前ですが、このスクリプトに対して実行権限を付与してから実行してください。
  • 作成されるファイル名は、{ファイルサイズ}_{連番}.txtとなります。
    • 以下の例では、100M_1.txtから100M_20.txtまで、100MBのファイルを20個生成しています。
[nkojima@akagi ~]$ ls
mkfiles.sh
[nkojima@akagi ~]$ ./mkfiles.sh -h
mkfiles.sh is a file-creation tool for test.

Usage:
    mkfiles.sh [<options>]

Options:
    -s    print file size in human readable format(e.g., 10K, 5M, 1G). default is 1K.
    -c    print file counts. default is 1.
[nkojima@akagi ~]$ ./mkfiles.sh -s 100M -c 20
....................
[nkojima@akagi ~]$ ls -lh
合計 2.0G
-rw-rw-r--. 1 nkojima nkojima 100M  7月  6 20:05 2019 100M_1.txt
-rw-rw-r--. 1 nkojima nkojima 100M  7月  6 20:05 2019 100M_10.txt
-rw-rw-r--. 1 nkojima nkojima 100M  7月  6 20:05 2019 100M_11.txt
-rw-rw-r--. 1 nkojima nkojima 100M  7月  6 20:05 2019 100M_12.txt
-rw-rw-r--. 1 nkojima nkojima 100M  7月  6 20:05 2019 100M_13.txt
-rw-rw-r--. 1 nkojima nkojima 100M  7月  6 20:05 2019 100M_14.txt
-rw-rw-r--. 1 nkojima nkojima 100M  7月  6 20:05 2019 100M_15.txt
-rw-rw-r--. 1 nkojima nkojima 100M  7月  6 20:05 2019 100M_16.txt
-rw-rw-r--. 1 nkojima nkojima 100M  7月  6 20:05 2019 100M_17.txt
-rw-rw-r--. 1 nkojima nkojima 100M  7月  6 20:05 2019 100M_18.txt
-rw-rw-r--. 1 nkojima nkojima 100M  7月  6 20:05 2019 100M_19.txt
-rw-rw-r--. 1 nkojima nkojima 100M  7月  6 20:05 2019 100M_2.txt
-rw-rw-r--. 1 nkojima nkojima 100M  7月  6 20:05 2019 100M_20.txt
-rw-rw-r--. 1 nkojima nkojima 100M  7月  6 20:05 2019 100M_3.txt
-rw-rw-r--. 1 nkojima nkojima 100M  7月  6 20:05 2019 100M_4.txt
-rw-rw-r--. 1 nkojima nkojima 100M  7月  6 20:05 2019 100M_5.txt
-rw-rw-r--. 1 nkojima nkojima 100M  7月  6 20:05 2019 100M_6.txt
-rw-rw-r--. 1 nkojima nkojima 100M  7月  6 20:05 2019 100M_7.txt
-rw-rw-r--. 1 nkojima nkojima 100M  7月  6 20:05 2019 100M_8.txt
-rw-rw-r--. 1 nkojima nkojima 100M  7月  6 20:05 2019 100M_9.txt
-rwx------. 1 nkojima nkojima  639  7月  6 20:04 2019 mkfiles.sh
0
0
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
0
0