Help - Search - Members - Calendar
Full Version: Shell command to truncate a file [solved]
MandrivaUsers.org > Advanced Topics > Command Line, Kernel and Programming
theYinYeti
I forgot to stop a TV recording, which resulted in a giant file taking up all remaining free space on disk.
Now I want to keep only the first 12GB from this file.

The disk is so full I can't free 12GB on it, to be able to use the "head" command.
I know I can (and will) use something like "head --bytes=12000m FILE | ssh remoteuser@remotehost 'cat >TAIL-FILE'". But…

I'd like to know how I can truncate a file to its first bytes when neither the network nor extra disk space is available.

Yves.
ramfree17
a quick search for linux and truncate revealed this links: http://linux.about.com/library/cmd/blcmdl2_truncate.htm

which according to pbone.net is either in the ltp package: http://rpm.pbone.net/index.php3/stat/4/idp...0.i386.rpm.html

or in the embutils package: http://rpm.pbone.net/index.php3/stat/4/idp...1.i586.rpm.html

based on the description it does not move the file so it seems like what you are looking for.

ciao!
theYinYeti
QUOTE (ramfree17 @ Jun 3 2008, 05:25 AM) *
a quick search for linux and truncate revealed this links: http://linux.about.com/library/cmd/blcmdl2_truncate.htm

This is a C system call. I cannot use it directly.

QUOTE
which according to pbone.net is either in the ltp package: http://rpm.pbone.net/index.php3/stat/4/idp...0.i386.rpm.html

And those are just test cases for the kernel function that is called by the above system call.

QUOTE
or in the embutils package: http://rpm.pbone.net/index.php3/stat/4/idp...1.i586.rpm.html

based on the description it does not move the file so it seems like what you are looking for.

I didn't notice this one before. It may indeed be what I'm looking for, even though it's deep under /usr/lib… Thanks, I'll try.

Yves.
theYinYeti
Excellent! smile.gif It works:
CODE
[yves@localhost ~]$ truncate
usage: truncate size filename [filename...]
[yves@localhost ~]$ echo "hello hello" >testfile
[yves@localhost ~]$ echo "bye bye" >>testfile
[yves@localhost ~]$ ls -li testfile
711918 -rw-r--r-- 1 yves yves 20 2008-06-03 10:27 testfile
[yves@localhost ~]$ cat testfile
hello hello
bye bye
[yves@localhost ~]$ truncate 10 testfile
[yves@localhost ~]$ ls -li testfile
711918 -rw-r--r-- 1 yves yves 10 2008-06-03 10:27 testfile
[yves@localhost ~]$ cat testfile
hello hell[yves@localhost ~]$

Thanks again.

Yves.
ramfree17
cool! smile.gif

ciao!
senyahnoj
If you are using bash you can simply do:

> filename

not sure about other shells...
ramfree17
QUOTE (senyahnoj @ Oct 23 2008, 03:23 PM) *
If you are using bash you can simply do:

> filename


wouldnt that overwrite the whole file (aka redirection)? the original problem is to truncate a file up to a certain point so that the file contents is still usable and space is freed up.

ciao!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.