Shell script creates test data for a specified size file _linux shell

Source: Internet
Author: User

When we are testing or commissioning, sometimes you need to generate a file of size, for example, when testing the storage system, you need to reduce the amount of disk space 5G, the easiest way is to copy a 5G file over, but where to get the size of files, perhaps you think of random find a file, non-stop copy, Finally, this is a way to do it, but with DD, you will be easier and more flexible to implement.
We'll take the case by introducing DD's usage. Let's see the first one.

Generate a file size 5G with no content required

Commands are as follows

Copy Code code as follows:

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

Explain the parameters used here.
Copy Code code as follows:

If=file: Specifies the input file, and reads from the callout input if unspecified. This specifies that/dev/zero is a pseudo file of Linux that can produce a continuous stream of NULL (binary 0)
Of=file: Specify output file, output to standard output if not specified
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 by IBS and OBS respectively, and if BS is used, it means that both IBS and OBS are using this parameter
Count=blocks: The number of blocks read, the size of the block is specified by IBS (for input parameters only)

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

Let's see one more question.

Copy Code code as follows:

Appends the top 1M of the file.in to the end of the File.out

Commands are as follows

Copy Code code 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 settings are set for different values, and only one seek parameter is more than the preceding command
Copy Code code as follows:

Seek=blocks: Before copying data, the size of the BLOCKS Block,block skipped from the beginning of the output file is specified by the 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 the file.out, but at the 1* $file _out_size byte offset (that is, at the end of the file)

Add a further requirement on this basis

Appends the file.in 3rd m to the end of the File.out

Copy Code code as follows:

$ 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

Copy Code code as follows:

Skip=blocks: The size of the BLOCKS Block,block skipped from the input file before copying the data is specified by IBS. This parameter is corresponding to the seek.

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

Such basic parameters are all introduced, but is to set the input and output files and their respective offsets, set read and write data block size and read the number of blocks, the following summary

Copy Code code as follows:

Input parameters:
If
Skip
Ibs
Count
Output parameters:
Of
Seek
Obs

Finally, a final question. The previous creation is a null stream, this time for another

Copy Code code as follows:

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

The problem seems pointless, but sometimes it does need to be used. For example, I created a 1G file through/dev/null, but I wanted to modify the intermediate 100M data for testing purposes, and I needed to create a 100M file to write to the specified location of the 1G file, and the 100M file could not be created from/dev/null. Otherwise, the purpose of the modification is not achieved, this time the need for such a function

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

Copy Code code as follows:

#!/bin/bash
If [$#-ne 3];then
echo "Usage: $ character out_file file_size (Byte)"
Exit 1
Fi

echo "$" | Grep-q "^[a-za-z]$"
If [$?-ne 0];then
echo "Arg1 must be character"
Exit 1
Fi

Character=$1
Out_file=$2
Target_size=$3

# echo output default is ' \ n ' character, so you need to specify the number of bytes entered via DD
echo "$character" | DD of= $out _file Ibs=1 count=1
While True
Todo
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 1
Done

With these tips, you can create files of a specified size, arbitrarily modify the number of bytes in a file, and make certain test situations very handy, without requiring the contents of the file.

Related Article

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.