Jump to content

Shell command to truncate a file [solved]


Recommended Posts

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.

Edited by theYinYeti
Link to comment
Share on other sites

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!

Edited by ramfree17
Link to comment
Share on other sites

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.

 

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.

 

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.

Link to comment
Share on other sites

Excellent! :) It works:

[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.

Edited by theYinYeti
Link to comment
Share on other sites

  • 4 months later...
Guest senyahnoj

If you are using bash you can simply do:

 

> filename

 

not sure about other shells...

Edited by senyahnoj
Link to comment
Share on other sites

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!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...