创建需求任意大小的文件的多种方式

在测试中需要创建需求大小的文件,学习了几种方式,罗列如下备忘:

1. dd

Linux下的命令dd用于转换和复制文件,dd命令需要输入,所以创建指定大小(200K)的文件(不关心内容),可以使用以下命令:

$ dd if=/dev/urandom of=dd_testFile.txt bs=10K count=20

或者

$ dd if=/dev/zero of=dd_testFile.txt bs=10K count=20

详情见:

http://zh.wikipedia.org/zh-cn/Dd_(Unix)


2. truncate

Linux下的又一个命令行,用于截断和扩展文件的大小,如果文件不存在,可以创建指定任意大小的文件,truncate不需要输入;

$ truncate --size 100K truncate_test.txt


3. fallocate

Linux的另一个命令,用于为指定文件“预留”需求的大小,实际可以创建需求的任意大小的文件;manual里这么说的:

fallocate  is used to preallocate blocks to a file.  For filesystems which support the fallocate system call, this is done quickly by allocating blocks and marking them as uninitialized, requiring no IO to the data blocks.   This is much faster than creating a file by filling it with zeros.

由此可见,fallocate创建文件比较快;


$ fallocate --length 100K fallocate_test.txt


4. Windows 下的fsutil

fsutil 是Windows下提供的命令,可以创建需求大小的文件;

参考:http://www.cnblogs.com/jiangz/archive/2013/05/17/3084315.html

使用fsutil命令,在windows xp和win 7下应该都自带了这个命令。命令的格式是 fsutil file createnew 新文件名 文件大小。例如生成一个1K大小的文件,可以使用

 fsutil file createnew test.txt 1024


5. qemu-img

看网上也有使用Linux下命令qemu-img 来创建文件,Ubuntu需要安装qemu-kvm 才能使用;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值