在测试中需要创建需求大小的文件,学习了几种方式,罗列如下备忘:
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 才能使用;