Create File of Size X
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Create File of Size X




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Create File of Size X  
Fao, Sean


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-18-05 11:03 PM

Is there any easy way to create a file with size X bytes?  In other
words, if I want to create a file that's exactly 1024 bytes long, what's
the best way to go about this?  Is a simple loop the best way?

Thank you in advance,

--
Sean





[ Post a follow-up to this message ]



    Re: Create File of Size X  
Alex Fraser


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-18-05 11:03 PM

"Fao, Sean" <enceladus311@yahoo.comI-WANT-NO-SPAM> wrote in message
news:K7Mie.8$NP5.1113@news.abs.net...
> Is there any easy way to create a file with size X bytes?  In other
> words, if I want to create a file that's exactly 1024 bytes long, what's
> the best way to go about this?  Is a simple loop the best way?

ftruncate(), or truncate() if you don't need the file open.

Alex







[ Post a follow-up to this message ]



    Re: Create File of Size X  
Fao, Sean


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-18-05 11:03 PM

Alex Fraser wrote:
> "Fao, Sean" <enceladus311@yahoo.comI-WANT-NO-SPAM> wrote in message
> news:K7Mie.8$NP5.1113@news.abs.net...
> 
>
> ftruncate(), or truncate() if you don't need the file open.

Exactly what I was looking for.  Thank you, Alex.

--
Sean





[ Post a follow-up to this message ]



    Re: Create File of Size X  
Bill Marcum


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-18-05 11:03 PM

On Wed, 18 May 2005 14:49:32 -0400, Fao, Sean
<enceladus311@yahoo.comI-WANT-NO-SPAM> wrote:
> Is there any easy way to create a file with size X bytes?  In other
> words, if I want to create a file that's exactly 1024 bytes long, what's
> the best way to go about this?  Is a simple loop the best way?
>
dd if=/dev/zero of=yourfile bs=1024 count=1


--
Test-tube babies shouldn't throw stones.





[ Post a follow-up to this message ]



    Re: Create File of Size X  
SM Ryan


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-19-05 01:50 AM

"Fao, Sean" <enceladus311@yahoo.comI-WANT-NO-SPAM> wrote:
# Alex Fraser wrote:
# > "Fao, Sean" <enceladus311@yahoo.comI-WANT-NO-SPAM> wrote in message
# > news:K7Mie.8$NP5.1113@news.abs.net...
# >
# >>Is there any easy way to create a file with size X bytes?  In other
# >>words, if I want to create a file that's exactly 1024 bytes long, what's
# >>the best way to go about this?  Is a simple loop the best way?
# >
# > ftruncate(), or truncate() if you don't need the file open.
#
# Exactly what I was looking for.  Thank you, Alex.

On some file systems, that can create a promise of 1024 zero bytes, a hole,
without actually creating that many. It matters if you're concerned about
disk capacity, for example if you're pre-allocating to ensure the entire fil
e
can be written without a track limit.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
OOOOOOOOOO! NAVY SEALS!





[ Post a follow-up to this message ]



    Re: Create File of Size X  
Lew Pitcher


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-19-05 07:48 AM

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Fao, Sean wrote:
> Is there any easy way to create a file with size X bytes?  In other
> words, if I want to create a file that's exactly 1024 bytes long, what's
> the best way to go about this?  Is a simple loop the best way?

See dd(1)

I.e.
dd if=/dev/zero of=/my/file bs=1024 count=1

Change the bs and count values to get the size you want
final size = bs x count


- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFCi/51agVFX4UWr64RAtYTAKDKwwL0mqu/D2evwb5hKVeBrnGQngCggWNX
TYxFgwmj5X3Dn5+Iaq/nUek=
=hKJD
-----END PGP SIGNATURE-----





[ Post a follow-up to this message ]



    Re: Create File of Size X  
Rich Teer


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-19-05 10:52 PM

On Wed, 18 May 2005, Lew Pitcher wrote:

> See dd(1)

Also mkfile (on Solaris at least).

--
Rich Teer, SCNA, SCSA, OpenSolaris CAB member

President,
Rite Online Inc.

Voice: +1 (250) 979-1638
URL: http://www.rite-group.com/rich





[ Post a follow-up to this message ]



    Re: Create File of Size X  
Måns Rullgård


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-19-05 10:52 PM

Lew Pitcher <lpitcher@sympatico.ca> writes:

> Fao, Sean wrote: 
>
> See dd(1)
>
> I.e.
>   dd if=/dev/zero of=/my/file bs=1024 count=1
>
> Change the bs and count values to get the size you want
>   final size = bs x count

If the size if very large, something like

dd if=/dev/null of=/my/file bs=1024 seek=1

will save you some time.

--
Måns Rullgård
mru@inprovide.com





[ Post a follow-up to this message ]



    Re: Create File of Size X  
Geoff Clare


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-20-05 12:55 PM

Måns Rullgård <mru@inprovide.com> wrote, on Thu, 19 May 2005:
 
>
> If the size if very large, something like
>
>   dd if=/dev/null of=/my/file bs=1024 seek=1
>
> will save you some time.

That only works with some versions of dd.  For example it doesn't
work on HP-UX.  To be sure of setting the size you need to give
dd something to write after it has done the seek.  E.g.:

echo | dd of=/my/file bs=1024 seek=1

will create a 1025 byte file.

The reason is that seeking past EOF does not itself extend the file,
you need to write something at the new offset.  Versions of dd
that extend the file when nothing is written do it by calling
ftruncate() to set the size explicitly.

--
Geoff Clare <netnews@gclare.org.uk>






[ Post a follow-up to this message ]



    Re: Create File of Size X  
Nils Weller


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-21-05 10:49 PM

In article <118nq99cminij95@corp.supernews.com>, SM Ryan wrote:
> "Fao, Sean" <enceladus311@yahoo.comI-WANT-NO-SPAM> wrote:
> # Alex Fraser wrote:
> # > "Fao, Sean" <enceladus311@yahoo.comI-WANT-NO-SPAM> wrote in message
> # > news:K7Mie.8$NP5.1113@news.abs.net...
> # >
> # >>Is there any easy way to create a file with size X bytes?  In other
> # >>words, if I want to create a file that's exactly 1024 bytes long, what
's
> # >>the best way to go about this?  Is a simple loop the best way?
> # >
> # > ftruncate(), or truncate() if you don't need the file open.
> #
> # Exactly what I was looking for.  Thank you, Alex.
>
> On some file systems, that can create a promise of 1024 zero bytes, a hole
,
> without actually creating that many. It matters if you're concerned about
> disk capacity, for example if you're pre-allocating to ensure the entire f
ile
> can be written without a track limit.

SUSv3 provides a more reliable function for pre-allocating file space:
posix_fallocate(). You may wish to use it in combination with
posix_fadvise() in order to optimize the block allocation strategy. Of
course, this may not be an option if portability is among your main
concerns, as it is a new function (though you can always use conditional
compilation to pick a portable implementation on systems that do not
provide posix_fallocate().)

--
Nils R. Weller, Bremen / Germany
My real email address is ``nils<at>gnulinux<dot>nl''
... but I'm not speaking for the Software Libre Foundation!





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 11:18 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register