LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Create a file of a specified size (https://www.linuxquestions.org/questions/linux-newbie-8/create-a-file-of-a-specified-size-614068/)

Rettahdam 01-16-2008 03:07 PM

Create a file of a specified size
 
Hey all,

Can anyone tell me if it is possible to create a file that is a certain size. I need to create a 50k and 200k file for testing and I can not for the life of me find a way to do it.

Thank ya-

Tinkster 01-16-2008 03:14 PM

Hi,

and welcome to LQ!


Like so?
Code:

dd if=/dev/zero of=50k_file bs=1024 count=50


Cheers,
Tink

Rettahdam 01-16-2008 03:20 PM

Exactly like that.. Many thanks

Lsatenstein 12-10-2011 05:56 PM

Quote:

Originally Posted by Rettahdam (Post 3024945)
Hey all,

Can anyone tell me if it is possible to create a file that is a certain size. I need to create a 50k and 200k file for testing and I can not for the life of me find a way to do it.

Thank ya-

Here it is in 2011.

truncate -s the_size_you_want_to_the_very_byte filename

SIZE may be (or may be an integer optionally followed by) one of following:
KB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on
for G, T, P, E, Z, Y.

Do man truncate.

Dark_Helmet 12-10-2011 10:44 PM

Quote:

Originally Posted by Rettahdam
I need to create a 50k and 200k file for testing

Tinkster's command is fine, but I would suggest an only slightly-modified version:
Code:

dd if=/dev/random of=50k_file bs=1024 count=50
The only difference is the use of /dev/random in place of /dev/zero. And the only reason I suggest that is because you want the file for testing.

Depending on the test, an all-zeroes file may not provide accurate results. For instance, compression algorithm tests would have an absolute field day "compressing" all zeroes.

So a random data file might be more to your liking.

And to be thorough, I don't know how truncate handles the data when a file is grown. The man page does not mention whether the new space is all-zeroes, randomized, or "whatever was written at those particular sectors last time they were used."

TobiSGD 12-11-2011 03:57 AM

Quote:

Originally Posted by Dark_Helmet (Post 4546919)
Tinkster's command is fine, but I would suggest an only slightly-modified version:
Code:

dd if=/dev/random of=50k_file bs=1024 count=50
The only difference is the use of /dev/random in place of /dev/zero. And the only reason I suggest that is because you want the file for testing.

May I suggest to use /dev/urandom instead, /dev/random is pretty slow, especially when you want to create larger files.


All times are GMT -5. The time now is 02:18 PM.