Create files with certain size

What it does:  The script will create certain number of files with certain size. It takes two arguments – first is the number of files and the second is the size of each file ( in kilobytes ).

Usage:  ./script.sh 100 1
(this will create 100 files 1MB each)

Code:

#!/bin/bash
filecount=$1;
blocksize=$2;
counter=1;
while [[ $counter -le $filecount ]];
do echo Copying $counter;
dd if=/dev/zero of=filename$counter bs=$2k count=1024
let "counter +=1";
done
Advertisements

2 thoughts on “Create files with certain size

  1. livibetter

    I’d prefer using ARITHMETIC EVALUATION:

    while ((counter <= filecount));
    

    and prefixing 0 to the filenames:

     printf -v filename '%04d' $counter
    

    and maybe using for for clearer code:

    for ((counter = 1; counter <= filecount; counter++)); do
     do echo Copying $counter
     printf -v filename 'filename%04d' $counter
     dd if=/dev/zero of=$filename bs=${blocksize}k count=1024
    done
    
    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s