linux生成指定大小文件常用的几个命令:
- fallocate
- truncate
- dd
- head
- tail
fallocate
$ fallocate -l 1M fallocate.tmp $ ls -l -rw-rw-r-- 1 garlic garlic 1048576 Aug 19 00:02 fallocate.tmp
-l 参数以字节为单位支持 k/m/g
truncate
$ truncate -s 1M truncate.tmp $ ls -l -rw-rw-r-- 1 garlic garlic 1048576 Aug 19 00:07 truncate.tmp
-s 参数类似上面-l
以上两个命令, 重复运行生成同样名称文件,如果存在文件大,生成的文件大小不变, 如果指定文件大,文件会变成指定大小。
dd
$ dd if=/dev/zero of=dd.tmp bs=1M count=1 1+0 records in 1+0 records out 1048576 bytes (1.0 MB, 1.0 MiB) copied, 0.00501926 s, 209 MB/s $ ls -l -rw-rw-r-- 1 garlic garlic 1048576 Aug 19 00:17 dd.tmp
- if 输入文件
- of 输出文件
- bs 生成一块大小
- count 生成块数
head & tail
$ head --bytes 1M /dev/zero > head.tmp $ ls -l -rw-rw-r-- 1 garlic garlic 1048576 Aug 19 00:19 head.tmp
$ tail --bytes 1M /dev/zero > tail.tmp $ ls -l -rw-rw-r-- 1 garlic garlic 1048576 Aug 19 00:19 tail.tmp
另外几个
yes
$ yes >> yes.tmp ^C $ ls -l -rw-rw-r-- 1 garlic garlic 322355200 Aug 19 00:23 yes.tmp
yes不断输出y直到被中断。使用ctrl+c中断
seq
$ seq 100 >> seq.tmp $ ls -l -rw-rw-r-- 1 garlic garlic 292 Aug 19 00:26 seq.tmp
seq是生成1..100的数字
参考及引用:
https://www.baeldung.com/linux/create-file-of-given-size
https://stackoverflow.com/questions/257844/quickly-create-a-large-file-on-a-linux-system (yes)
https://www.cyberithub.com/how-to-create-a-large-file-in-linux-simple-easy-methods/#Method_4_Using_seq_command(seq)
图片from廖素珠
Comments are closed.