linux目录怎么自动生成,情景linux--如何快速生成大文件?
情景
在寫情景l(fā)inux--如何解決服務(wù)器日志過大導(dǎo)致的磁盤空間不足?(實(shí)踐篇)時(shí),因?yàn)橐獙?shí)際演示,所以需要快速創(chuàng)建一個(gè)大文件。其實(shí),在實(shí)際工作過程中,有些時(shí)候是有這種需求的。今天就將其作為一個(gè)話題探討下。
創(chuàng)建文件有很多種途徑,如常見的命令有:touch命令、vi(m)命令、tee命令、>或>>、cp命令,或者它們的組合,等等。如果要?jiǎng)?chuàng)建一個(gè)大文件(比如>10GB),使用上述命令,結(jié)合循環(huán)語句也不難實(shí)現(xiàn),但效率就差強(qiáng)人意了。
方案
BTW:此處舉例統(tǒng)一用100MB的文件。
dd命令
dd命令,可以從標(biāo)準(zhǔn)輸入或指定的文件中讀取數(shù)據(jù),拷貝至新文件。
dd if=/dev/zero of=my_new_file count=102400 bs=1024
102400+0 records in
102400+0 records out
104857600 bytes (105 MB) copied, 0.642327 s, 163 MB/s
參數(shù)if為輸入文件名,of為輸出文件名,bs為單次讀取和寫入的字節(jié)數(shù),count為拷貝的次數(shù),因而總文件大小為bs*count。最后一行括號(hào)中的105MB顯然是把104857600 bytes按1000的量級(jí)換算出來的,因而不是精確值,不必細(xì)究。
wc my_new_file
0 0 104857600 my_new_file
od -tc my_new_file
0000000 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0
*
620000000
由wc的結(jié)果可知,生成的文件my_new_file的行數(shù)是0(不理解為什么是0的,請(qǐng)參見文章情景l(fā)inux--wc -l是用來統(tǒng)計(jì)文件行數(shù)的嗎?),單詞數(shù)為0,字節(jié)數(shù)為104857600。
由od命令的結(jié)果可知,文件中的內(nèi)容全部為\0空字符。這是由于我們的輸入文件是/dev/zero——一個(gè)特殊的文件,提供無限個(gè)空字符(NULL、0x00)。
如果希望文件有多行信息,可以將if設(shè)置為/dev/urandom——提供不為空字符的隨機(jī)字節(jié)數(shù)據(jù)流。
dd if=/dev/urandom of=detail_1 count=102400 bs=1024
102400+0 records in
102400+0 records out
104857600 bytes (105 MB) copied, 13.3349 s, 7.9 MB/s
wc detail_1
409483 2323021 104857600 detail_1
od -tc detail_1 | head -n 2
0000000 276 313 R * 274 273 372 N 266 034 346 i K 316 b p
0000020 302 | 314 243 [ 323 x 021 016 ? 016 275 270 266 ; #
fallocate命令
fallocate命令可以為文件預(yù)分配物理空間。-l后接空間大小,默認(rèn)單位為字節(jié)。也可后跟k、m、g、t、p、e來指定單位,分別代表KB、MB、GB、TB、PB、EB。
fallocate -l 100m new_file_2
wc new_file_2
0 0 104857600 new_file_2
od -tc new_file_2
0000000 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0
*
620000000
可見,fallocate命令分配出的內(nèi)容也是空字符。
truncate命令
truncate命令可以將文件縮減或擴(kuò)展為指定大小,使用-s參數(shù)設(shè)置大小。
truncate -s 100M new_file_3
wc new_file_3
0 0 104857600 new_file_3
od -tc new_file_3
0000000 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0
*
620000000
可見,truncate命令分配出的內(nèi)容也是空字符。
性能比較
既然我們希望快速得到大文件,那性能就是一個(gè)關(guān)鍵因素了。
BTW:此處舉例統(tǒng)一用20GB的文件。
dd命令的性能
sync ; time (dd if=/dev/zero of=20G_dd bs=2048 count=10485760 ; sync)
10485760+0 records in
10485760+0 records out
21474836480 bytes (21 GB) copied, 280.953 s, 76.4 MB/s
( dd if=/dev/zero of=20G_dd bs=2048 count=10485760; sync; ) 1.11s user 28.16s system 9% cpu 4:57.19 total
dd命令所產(chǎn)生的文件大小是由count和bs的乘積,所以我總覺得dd命令的執(zhí)行時(shí)間應(yīng)該是關(guān)于count或bs的函數(shù),究竟是不是這樣的呢?于是,調(diào)整參數(shù):
sync ; time (dd if=/dev/zero of=20G_dd_2 bs=10485760 count=2048 ; sync)
2048+0 records in
2048+0 records out
21474836480 bytes (21 GB) copied, 280.843 s, 76.5 MB/s
( dd if=/dev/zero of=20G_dd_2 bs=10485760 count=2048; sync; ) 0.01s user 19.89s system 6% cpu 4:57.08 total
sync ; time (dd if=/dev/zero of=20G_dd_3 bs=1048576 count=20480 ; sync)
20480+0 records in
20480+0 records out
21474836480 bytes (21 GB) copied, 280.614 s, 76.5 MB/s
( dd if=/dev/zero of=20G_dd_3 bs=1048576 count=20480; sync; ) 0.03s user 18.21s system 6% cpu 4:49.55 total
由上數(shù)據(jù),無論是bs=2048 count=10485760,bs=10485760 count=2048還是bs=1048576 count=20480,時(shí)間差異都很小,對(duì)應(yīng)地,拷貝速度也基本上是定值。
這是因?yàn)?#xff0c;dd命令的原理是從一個(gè)文件中復(fù)制數(shù)據(jù)并拷貝到另一個(gè)文件中,所以與設(shè)置不同的參數(shù)相比,dd的性能更依賴于硬盤的讀取和寫入速度,這也是為什么平均速度只有約76 MB/s的原因。
其實(shí),dd命令自身提供了可以更快的參數(shù)seek=BLOCKS:在輸出文件的開始處跳過BLOCKS個(gè)塊后再開始復(fù)制。
sync ; time (dd if=/dev/zero of=20G_dd_4 bs=1 count=0 seek=20G; sync)
0+0 records in
0+0 records out
0 bytes (0 B) copied, 6.971e-06 s, 0.0 kB/s
( dd if=/dev/zero of=20G_dd_4 bs=1 count=0 seek=20G; sync; ) 0.00s user 0.00s system 0% cpu 0.227 total
因?yàn)樘^的部分并沒有進(jìn)行硬盤讀寫,所以執(zhí)行時(shí)間極大地縮短。
od -tc 20G_dd_4
0000000 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0
*
240000000000
而文件開頭跳過的部分,也都是\0字符。
fallocate命令的性能
sync ; time (fallocate -l 20G 20G_fallocate ; sync)
( fallocate -l 20G 20G_fallocate; sync; ) 0.00s user 0.02s system 0% cpu 7.775 total
truncate命令
sync ; time (truncate -s 20G 20G_truncate ; sync)
( truncate -s 20G 20G_truncate; sync; ) 0.00s user 0.00s system 1% cpu 0.257 total
總結(jié)
從上面命令的執(zhí)行結(jié)果,可以大致得出這樣的結(jié)論:
不使用seek參數(shù)的dd命令在生成20G的文件時(shí),速度并不算太快,瓶頸是硬盤讀寫速度。
使用seek參數(shù)的dd命令的執(zhí)行速度則非常快,時(shí)間小于1s。
truncate命令的執(zhí)行速度也非常快,時(shí)間小于1s。
fallocate命令的執(zhí)行速度略慢,但創(chuàng)建20G的文件時(shí)間不到10s,也并不算慢。
PS:上述命令得出的速度數(shù)據(jù)跟操作系統(tǒng)的軟硬件配置有關(guān)。
參考鏈接
相關(guān)命令
dd
truncate
fallocate
od
sync
time
總結(jié)
以上是生活随笔為你收集整理的linux目录怎么自动生成,情景linux--如何快速生成大文件?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: oracle协议适配器错误00530,o
- 下一篇: linux多进程网络实例,Linux下一
