Generate files

来源: BlogBus 原始链接: http://lamp.blogbus.com:80/logs/2005/03/1049400.html 存档链接: https://web.archive.org/web/20060206190559id_/http://lamp.blogbus.com:80/logs/2005/03/1049400.html


Generate files For the optimizing php article (and elsewhere) we need a few files to play around with. These files have to be of definite size so that the results can be reproduced. On unix like operating systems there exists a tool named dd which can easily generate such files for you. The programs accepts several parameters but we need to concern ourselves with just the onces that interests are are: if - input filename of - output filename bs - block size count - number of blocks. Thus for example if we wish to make a 1MB file and fill it with zeros we might use dd this way: dd if=/dev/zero of=megabyte bs=1k count=1k However for some of our work it may be inconvinient to have nothing but zeros as the contents of the file. It would not make a good sample either. In such cases we could make use of /dev/random as the input file. However if you reran the previous command merely replacing /dev/zero with /dev/random the script will take what seems an eternity to run. dd if=/dev/zero of=megabyte bs=1k count=1k That's because producing random numbers is a 'difficult' problem and producing randmom number blocks of 1024 width is for want of a better term, 'very difficult'. We should there for use slighly different parameters. dd if=/dev/zero of=kilobyte bs=1 count=1k If you look closely you will see that we are generating a 1K file and not a 1MB file as we did before. If you ran the command you would have noticed that generating even a 1Kb file is no easy task. However once we have this 1kb file we could use it for other tasks like this: yes cat kilobyte | dd of=megabyte bs=1k count=1k That command as you might have noticed finishes seconds. Now you can continue to make files of various sizes for use with the Optimizing PHP article or other projects.