Linux tips-use dd to generate a file of the specified size, linuxdd

Source: Internet
Author: User

Linux tips-use dd to generate a file of the specified size, linuxdd
During testing or debugging, we sometimes need to generate a file of a certain size. For example, when testing the storage system, we need to reduce the remaining disk space by 5 GB, the simplest way is to copy a 5g file, but where can we get a file of this size? Maybe you want to find a file, copy it continuously, and merge it at last, this is also a method, but with dd, you will be easier and more flexible to implement

Let's introduce the usage of dd in case by case. First

Generate a 5 GB file with no requirements on the Content

The command is as follows:

$ dd if=/dev/zero of=tmp.5G bs=1G count=5

The parameters used here are explained.

If = FILE: Specifies the input FILE. if this parameter is not set, the FILE is read from the annotation input. The/dev/zero parameter is a pseudo FILE in Linux. It can generate continuous null streams (Binary 0) of = FILE: Specify the output FILE, if this parameter is not specified, bs = BYTES is output to the standard output. The unit is K, M, and G. In addition, the input and output can be specified with ibs and obs respectively. If bs is used, it indicates that both ibs and obs use this parameter count = BLOCKS: number of BLOCKS read, the block size is specified by ibs (only for input parameters)

In this way, the command to generate 5g files is easy to understand, that is, read 1G data every time from/dev/null, read 5 times, write the tmp.5 g File

Let's look at the following question.

Append the first 1 m of file. in To the end of file. out.

The command is as follows:

$ file_out_size=`du -b file.out | awk '{print $1}'`$ dd if=./file.in ibs=1M count=1 of=./file.out seek=1 obs=$file_out_size

Here, ibs and obs are set to different values. Compared with the preceding command, only one seek parameter is added.

Seek = BLOCKS: before copying data, the BLOCKS are skipped from the beginning of the output file. The block size is specified by obs.

The command is from file. in reads 1 MB of data blocks and writes them to file. but the write location is not in file. starting with out, but at the 1 * $ file_out_size byte offset (that is, the end of the file)

Add another requirement on this basis

Append 3 m of file. in To the end of file. out.

$ file_out_size=`du -b file.out | awk '{print $1}'`$ dd if=./file.in skip=2 ibs=1M count=1 of=./file.out seek=1 obs=$file_out_size

Here, a parameter skip is added.

Skip = BLOCKS: before copying data, the BLOCKS are skipped from the input file. The block size is specified by ibs. This parameter corresponds to seek.

The preceding command skips 2*1 M from file. in, copies 1*1 M data, and writes the 1 * $ file_out_size offset of file. out.

In this way, the basic parameters are all described. It is nothing more than setting the input and output files and Their offsets, and setting the size of the read and write data blocks and the number of read data blocks. The following is a summary.

Input parameter: if skip ibs count output parameter: of seek obs

The final question. All the previously created streams are null.

Specify a character to create a file of the specified size that is all this character. For example, to create A file with A size of 123456 bytes, each byte is A character

This seems meaningless, but sometimes it is necessary. For example, I have created a 1g file through/dev/null, but I want to modify M data in the middle out of the test requirement. In this case, I need to create a M file, write the file to the specified location of the 1g file, and the M file cannot be created from/dev/null. Otherwise, the file cannot be modified, this function is required at this time.

I don't know much about it. I believe I can understand the script with the Foundation above.

#! /Bin/basffe [$ #-ne 3]; then echo "usage: $0 character out_file file_size (Byte) "exit 1 fiecho" $1 "| grep-q" ^ [a-zA-Z] $ "if [$? -Ne 0]; then echo "arg1 must be character" exit 1 ficharacter = $ 1out_file = $ 2target_size = $3 # echo output contains '\ n' characters by default, therefore, you need to specify the number of input bytes through dd echo "$ character" | dd of = $ out_file ibs = 1 count = 1 while truedo cur_size = 'du-B $ out_file | awk '{print $1} ''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 skills, you can create a file of the specified size and modify the specified number of bytes without any requirement on the file content, which makes it very convenient for some testing scenarios.

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.