Unix Shell - Truncate big binary file

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > December 2006 > Truncate big binary file





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Truncate big binary file
Max Schillinger

2006-12-14, 7:29 pm

Hi,

I would like to truncate a big binary file. And since my file is so big, I
don't want to create a second, temporary file (better said, I can't because
there isn't enough free space on my hard disc). I searched the internet but
I couldn't find a solution.

"head" and "split" create additional files.

Another idea I found doesn't work for such big files:
max@bcn:~> dd of=./openSUSE-10.2-GM-DVD-i386.iso obs=3858364800 seek=1
< /dev/null
dd: número inválido «3858364800»
max@bcn:~>

Maybe I can use:
sed '1,xd' file > file
But "sed" seems to be for text files. I don't want to risk to destroy the
file.

Any ideas?
Thanks in advance!

Max

Janis Papanagnou

2006-12-14, 7:29 pm

Max Schillinger wrote:
> Hi,
>
> I would like to truncate a big binary file. And since my file is so big, I
> don't want to create a second, temporary file (better said, I can't because
> there isn't enough free space on my hard disc). I searched the internet but
> I couldn't find a solution.
>
> "head" and "split" create additional files.
>
> Another idea I found doesn't work for such big files:
> max@bcn:~> dd of=./openSUSE-10.2-GM-DVD-i386.iso obs=3858364800 seek=1
> < /dev/null
> dd: número inválido «3858364800»
> max@bcn:~>
>
> Maybe I can use:
> sed '1,xd' file > file
> But "sed" seems to be for text files. I don't want to risk to destroy the
> file.
>
> Any ideas?


The newest versions of Kornshell allow specifying offsets with file
redirection; I think it might be possible to achieve what you ask for
if they do that in-place (but that's what I'd expect), though I haven't
yet used that feature myself so my hint may seem vague, but you may try
that. (The online manual unfortunately doesn't describe that feature.)

Janis

> Thanks in advance!
>
> Max
>

John W. Krahn

2006-12-14, 7:29 pm

Max Schillinger wrote:
>
> I would like to truncate a big binary file. And since my file is so big, I
> don't want to create a second, temporary file (better said, I can't because
> there isn't enough free space on my hard disc). I searched the internet but
> I couldn't find a solution.


perl -e'truncate "file", 1234567'


John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
Max Schillinger

2006-12-14, 7:29 pm

> PERL -e'truncate "file", 1234567'

It works! Thanks
Kenny McCormack

2006-12-17, 1:37 am

In article <4udsmtF17mqaqU1@mid.dfncis.de>,
Max Schillinger <maximilian.schillinger@rwth-aachen.de> wrote:
>
>It works! Thanks


You can also do it with "dd" and the "trunc" option. Details left to you...

You could also do it with any other scripting tool that, like Perl,
gives you more or less transparent access to Unix system calls.

Or you could write a C program to do it.

Janis Papanagnou

2006-12-17, 1:37 am

Kenny McCormack wrote:
> In article <4udsmtF17mqaqU1@mid.dfncis.de>,
> Max Schillinger <maximilian.schillinger@rwth-aachen.de> wrote:
>
>
>
> You can also do it with "dd" and the "trunc" option. Details left to you...


When the question had been asked I immediately thought about 'dd', found
the 'notrunc' conversion option (no 'trunc' specified, though) and tried
that without success. So the "left out details" would be the interesting
part if you know how to make that work.

Just note; it works well if you specify an output file different from the
input file, but seems not to work in place as requested by the OP.

Janis
Bo Yang

2006-12-17, 1:37 am

Max Schillinger :
> Hi,
>
> I would like to truncate a big binary file. And since my file is so big, I
> don't want to create a second, temporary file (better said, I can't because
> there isn't enough free space on my hard disc). I searched the internet but
> I couldn't find a solution.
>
> "head" and "split" create additional files.
>
> Another idea I found doesn't work for such big files:
> max@bcn:~> dd of=./openSUSE-10.2-GM-DVD-i386.iso obs=3858364800 seek=1
> < /dev/null
> dd: número inválido «3858364800»
> max@bcn:~>

You can use
dd of=./openSUSE-.. obs=38583648 seek=100 < /dev/null

It works for me very well!
>
> Maybe I can use:
> sed '1,xd' file > file
> But "sed" seems to be for text files. I don't want to risk to destroy the
> file.
>
> Any ideas?
> Thanks in advance!
>
> Max
>


Kenny McCormack

2006-12-17, 1:16 pm

In article <em2950$80v$1@online.de>,
Janis Papanagnou <Janis_Papanagnou@hotmail.com> wrote:
....
>When the question had been asked I immediately thought about 'dd', found
>the 'notrunc' conversion option (no 'trunc' specified, though) and tried
>that without success. So the "left out details" would be the interesting
>part if you know how to make that work.


Yes, it appears you are correct. I have a distant memory of having
done this at some point - used dd to truncate a file in place - but it
seems not to be there and not to work at the present time.
Dave Gibson

2006-12-17, 1:16 pm

Kenny McCormack <gazelle@xmission.xmission.com> wrote:
> In article <em2950$80v$1@online.de>,
> Janis Papanagnou <Janis_Papanagnou@hotmail.com> wrote:
> ...
>
> Yes, it appears you are correct. I have a distant memory of having
> done this at some point - used dd to truncate a file in place - but it
> seems not to be there and not to work at the present time.


dd if=/dev/null of=$file_name ibs=1 obs=1 seek=$size_in_bytes count=0
John DuBois

2006-12-17, 1:16 pm

In article <em2950$80v$1@online.de>,
Janis Papanagnou <Janis_Papanagnou@hotmail.com> wrote:
>When the question had been asked I immediately thought about 'dd', found
>the 'notrunc' conversion option (no 'trunc' specified, though) and tried
>that without success. So the "left out details" would be the interesting
>part if you know how to make that work.
>
>Just note; it works well if you specify an output file different from the
>input file, but seems not to work in place as requested by the OP.


It works with GNU dd:

dd bs=1 of=filename seek=nnn if=/dev/null

With some other dds, you need to write at least one character for it to do the
truncation:

echo | dd bs=1 of=filename seek=nnn

(and the file ends up having size nnn+1 of course)

John
--
John DuBois spcecdt@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/
Janis Papanagnou

2006-12-17, 7:32 pm

John DuBois wrote:
> In article <em2950$80v$1@online.de>,
> Janis Papanagnou <Janis_Papanagnou@hotmail.com> wrote:
>
>
>
> It works with GNU dd:
>
> dd bs=1 of=filename seek=nnn if=/dev/null
>
> With some other dds, you need to write at least one character for it to do the
> truncation:
>
> echo | dd bs=1 of=filename seek=nnn
>
> (and the file ends up having size nnn+1 of course)
>
> John


Helpful to know!

Janis
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com