Linux Tips--Generate files of a specified size with DD

Source: Internet
Author: User

when we are testing or debugging, sometimes we will need to generate a size file, such as testing the storage system, you need to reduce the amount of disk space to 5G, the simplest way is to copy a 5G file, but from where to get files of such size, perhaps you think of a random file, It's a way to keep copying and finally merging, but with DD, you'll be easier and more flexible to implement .

Let's introduce the use of DD in case-by-case. Let's see the first one.

Generate a file with a size of 5G, the content does not require

The command is as follows

$ dd If=/dev/zero of=tmp.5g bs=1g count=5

Explain the parameters used here.

If=file      : Specifies the input file, which is read from the callout input if not specified. The designation here as/dev/zero is a pseudo-file of Linux, which can generate a continuous null stream (binary 0) of=file      : Specifies the output file, if not specified, outputs to the standard output bs=bytes     : The number of bytes per read and write, You can use units K, M, G, and so on. In addition, the input and output can be specified in IBS, OBS, if using BS, it means that both IBS and OBS are using this parameter count=blocks: number of blocks read, block size is specified by IBS (for input parameters only)

So the command to generate 5G files above is very well understood, that is, every time from/dev/null reading 1G data, read 5 times, write tmp.5g this file

Let's look at one more question.

Append the top 1M of File.In to the end of File.out

The command is as follows

$ File_out_size= ' Du-b file.out | awk ' {print $} ' $ dd if=./file.in ibs=1m count=1 of=./file.out seek=1 obs= $file _out_size

Here the IBS and OBS are set to different values, compared to the previous command, only one more seek parameter

Seek=blocks: Skips the size of BLOCKS Block,block from the beginning of the output file before copying the data is specified by Obs

The command means to read 1 1M blocks of data from file.in to File.out, but the write location is not at the beginning of File.out, but at 1* $file _out_size byte offset (that is, the end of the file)

Add one more requirement on this basis

Append the 3rd m of File.In to the end of File.out

$ File_out_size= ' Du-b file.out | awk ' {print $} ' $ dd if=./file.in skip=2 ibs=1m count=1 of=./file.out seek=1 obs= $file _out_size

Here's one more parameter skip

Skip=blocks: The size of the BLOCKS Block,block is skipped from the input file before the data is copied by the IBS designation. This parameter and seek are the corresponding

The above command means to skip 2*1m from file file.in, copy 1*1m data, write file file.out 1* $file _out_size offset

Such basic parameters are all introduced, nothing more than to set the input and output files and their respective offsets, set the read and write data block size and read the number of data blocks, summarized below

Input parameters:    If    skip    IBS    count output parameter:    of    seek    Obs

Finally come to a final question. The previous create is a null stream, this time for a

Specifies a character that creates a file of a specified size that is all of this character. For example, create a file, size 123456 bytes, each byte is a character a

The problem seems pointless, but sometimes it needs to be used. For example, I created a 1G file through/dev/null, but for testing needs I want to modify the intermediate 100M data, when I need to create a 100M file, the file is written to the 1G file in the specified location, and this 100M file can not be created from/dev/null, Otherwise, the purpose of the modification is not achieved, this is the need for such a function

Words do not say, directly on the script, with the basis of the previous, I believe you can read

#!/bin/bashif [$#-ne 3];then    echo "Usage: $ character out_file file_size (Byte)"    exit 1fiecho "$" | grep-q " ^[a-za-z]$ "If [$?-ne 0];then    echo" Arg1 must be character "    exit 1ficharacter=$1out_file=$2target_size=$3# Echo The output defaults to the ' \ n ' character, so you need to specify the number of input bytes via DD echo "$character" | DD of= $out _file ibs=1 count=1while truedo    cur_size= ' du-b $out _file | awk ' {print '} '    if [$cur _size-ge $target _size];then        break    fi    remain_size=$ ((target_size-$cur _size))    if [$remain _size-ge $cur _size];then        input_size= $cur _size    else        input_size= $remain _size    fi    dd if= $out _file ibs= $input _size Count=1 of= $out _file seek=1 obs= $cur _size | | Exit 1done

With these techniques, you can create a file of the specified size without any requirement on the contents of the file, and modify the file to specify the number of bytes, which makes some test situations very convenient.

Linux Tips--Generate files of a specified size with DD

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.