Home > Linux > mkfile/dd to create a file with specific size

mkfile/dd to create a file with specific size

June 21st, 2011


Now assume you want to create a file with 10M space:
Under Solaris:
root@beisoltest02 / # mkfile 10m disk1.img
root@beisoltest02 / # ls -lh disk1.img -rw——T   1 root     root         10M Jun 22 00:41 disk1.img

Under Linux:
[root@beivcs02 downloads]#  dd if=/dev/zero of=disk1.img bs=1024k count=10
10+0 records in
10+0 records out
10485760 bytes (10 MB) copied, 0.01096 seconds, 957 MB/s
[root@beivcs02 downloads]# ls -lh disk1.img
-rw-r–r– 1 root root 10M Jun 22 00:37 disk1.img


support_me
Amounts
Categories: Linux Tags:
  1. Nikkkor
    September 27th, 2011 at 17:47 | #1

    Cool, thanks! it works for me :)

  2. kekkonj
    March 31st, 2013 at 11:02 | #2

    You can also use the following script with almost all different Linux distros to emulate “mkfile”. Put the script somewhere within the search path and give it a executable rights with the command chmod 755 mkfile. The syntax of the mkfile is then “mkfile

    #!/bin/bash
    #
    # This script makes the Solaris mkfile command to live in Linux
    #
    # USAGE: mkfile with an example # mkfile 10m filename
    opt1=`echo $1`
    value=`echo $1|sed -e “s/^.*(.)$/1/”`
    size=`echo “${opt1::${#opt1}-1}”`
    gi=`echo “$size * 1000″|bc -l`
    count=`echo $opt1|grep ‘.’|wc -m`
    echo $opt1|grep ‘.’ 1> /dev/null 2>&1
    if [ $? = 0 ]; then
    if [ "$count" -ge 6 ]; then
    echo -e “Please, do not give more decimals than one after . (period).”
    echo -e “Use m for Mega Bytes instead of g for Giga Bytes in exact cases.”
    exit 0
    fi
    giga=`echo “${gi::${#gi}-2}”`
    else
    giga=`echo $gi`
    fi
    #
    case “$value” in
    k|K)
    dd if=/dev/zero of=$2 bs=1k count=$size 1> /dev/null 2>&1
    ;;
    m|M)
    dd if=/dev/zero of=$2 bs=1024k count=$size 1> /dev/null 2>&1
    ;;
    g|G)
    dd if=/dev/zero of=$2 bs=1024k count=$giga 1> /dev/null 2>&1
    ;;
    *)
    ;;
    esac
    exit 0

    • kekkonj
      March 31st, 2013 at 11:08 | #3

      Remove from the script all the html/xml tags after the last “exit 0″. They were not supposed to appear there, but copying + pasting onto the website did cause this kind garbled contents to appear there.

  1. June 21st, 2011 at 23:54 | #1
  2. December 22nd, 2012 at 09:23 | #2