Jump to content
Sign in to follow this  
theYinYeti

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

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
Guest senyahnoj

If you are using bash you can simply do:

 

> filename

 

not sure about other shells...

Edited by senyahnoj

Share this post


Link to post
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!

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...