zl程序教程

您现在的位置是:首页 >  系统

当前栏目

Linux下快速创建大文件的方法(linux创建大文件)

Linux文件方法 快速 创建
2023-06-13 09:15:05 时间

在Linux下创建大文件(大小在100M以上)是任何系统管理员必须面对的情况之一,一般由于测试服务器磁盘性能,模拟大文件来测试磁盘性能,或者用来作为磁盘映像文件等等。下面介绍一种使用`/dev/zero`快速创建大文件的方法。

首先我们需要使用Linux系统内置的`/dev/zero`文件,它会持续输出`0x00`的字节信息,所以可以用它来创建指定大小的文件:

$ dd if=/dev/zero of=bigfile.img bs=20M count=20
20+0 records in20+0 records out
419430400 bytes (419 MB, 400 MiB) copied, 2.59893 s, 161 MB/s

上面的语句中,`if`参数指定的是输入的文件,这里是`/dev/zero`,`of`参数指定的是我们要创建的大文件名称,这里是`bigfile.img`,bs参数指定的是每一次拷贝的大小,大小是bytes最小单位,这里是20M,count参数代表循环拷贝次数,这里填写20,所以最终创建的文件大小是20M x 20 = 400M。

通过上面的方法,我们很方便的就创建了一个大文件,如果想要创建更大的文件,只需要把`bs`和`count`参数调大就可以了。

有些情况下,我们不想一次性创建一个大文件,而是分别创建几个小文件,这也很easy:

$ for i in $(seq 10); do dd if=/dev/zero of=file$i bs=5m count=5; done
5+0 records in5+0 records out
5242880 bytes (5.2 MB, 5.0 MiB) copied, 0.0193245 s, 270 MB/s5+0 records in
5+0 records out5242880 bytes (5.2 MB, 5.0 MiB) copied, 0.0195468 s, 267 MB/s
5+0 records in5+0 records out
5242880 bytes (5.2 MB, 5.0 MiB) copied, 0.0192924 s, 271 MB/s5+0 records in
5+0 records out5242880 bytes (5.2 MB, 5.0 MiB) copied, 0.0193203 s, 270 MB/s
5+0 records in5+0 records out
5242880 bytes (5.2 MB, 5.0 MiB) copied, 0.022063 s, 237 MB/s5+0 records in
5+0 records out5242880 bytes (5.2 MB, 5.0 MiB) copied, 0.0192579 s, 271 MB/s
5+0 records in5+0 records out
5242880 bytes (5.2 MB, 5.0 MiB) copied, 0.0189577 s, 274 MB/s5+0 records in
5+0 records out5242880 bytes (5.2 MB, 5.0 MiB) copied, 0.020131 s, 259 MB/s
5+0 records in5+0 records out
5242880 bytes (5.2 MB, 5.0 MiB) copied, 0.0202127 s, 258 MB/s5+0 records in
5+0 records out5242880 bytes (5.2 MB, 5.0 MiB) copied, 0.0194629 s, 269 MB/s
5+0 records in5+0 records out
5242880 bytes (5.2 MB, 5.0 MiB) copied, 0.0190501 s, 273 MB/s

这样就成功创建10个5M大小的文件,并且在短时间内完成,它们都是以file1,file2,file3等格式开头的。

总而言之,通过使用Linux下的`/dev/zero`,不论是想要创建一个大文件,还是想要创建多个小文件,都可以用这种快速方法来实现,非常实用。


我想要获取技术服务或软件
服务范围:MySQL、ORACLE、SQLSERVER、MongoDB、PostgreSQL 、程序问题
服务方式:远程服务、电话支持、现场服务,沟通指定方式服务
技术标签:数据恢复、安装配置、数据迁移、集群容灾、异常处理、其它问题

本站部分文章参考或来源于网络,如有侵权请联系站长。
数据库远程运维 Linux下快速创建大文件的方法(linux创建大文件)