Create 1Gb file in seconds ?

Hi Folks,
One of my friend asked is it possible to create a 1Gb file instantaneously. Well, I have never thought of it ,as typically what I normally do is, open a vim editor jolt down some lines and do a yank on it and suggest the number of time you need to paste the copied content , and thus your job would be done.

But in that case i knew what I would have typed. 🙂 But my friend requirement was to just create a 1Gb file and he is least bothered as what the content are in it. He was just concerned on the size rather than the “data” itself 🙂 ..

Now again our own Linux comes into rescue 🙂 With just one line we will be able to create a 1Gb file in 1 sec with random data .
The command is as follows
dd if=/dev/zero of=file.txt count=1024 bs=1048576

When You Don’t Care About The Contents But Want Some Lines you can use the command
dd if=/dev/urandom of=file.txt bs=2048 count=10
This will create a file with bs*count random bytes, in our case 2048*10 = 20Kb.

But if u want some contents now to be seen but least worried about repetition then you can use the following command.
Write some lines using cat to file.txt

cat – > file.txt

This commands redirects STDIN to a file, so you will need to enter two lines and then press Ctrl+D. Then you will need to run the following command:

for i in {1..n}; do cat file.txt file.txt > file2.txt && mv file2.txt file.txt; done

Where n is an integer. This will create a file with 2^(n+1) lines in it, by duplicating your original two lines. So to create a file with 16 lines you would do:

for i in {1..3}; do cat file.txt file.txt > file2.txt && mv file2.txt file.txt; done

There is also a command by name fallocate but I haven’t explored much 🙂

I learned from here http://www.skorks.com/2010/03/how-to-quickly-generate-a-large-file-on-the-command-line-with-linux/ .. So when u have time do visit 🙂

Advertisements

ನಿಮ್ಮದೊಂದು ಉತ್ತರ

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s