Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a few files sized > 1 GB each. I need to remove last few bytes from the files. How can I do it? I prefer to edit file in place to save disk space.

I am on HP-UX.

share|improve this question
    
I think vim has a 4G limit (could be wrong) but I don't think it does it in place (could be wrong). –  xenoterracide Aug 26 '10 at 11:59
1  
@xenoterracide: I don't have vim here :-( and vi gives "Tmp file too large" Error. –  Hemant Aug 26 '10 at 17:39
    
@Hernant: That message tells you all you need to know about editing in place: vi is trying to copy it over to /tmp to work on. I think vim will do the same thing, and it does like to create a backup in the same directory. –  David Thornley Aug 27 '10 at 21:50
add comment

5 Answers

up vote 9 down vote accepted

Try using hexedit I haven't tried it on HP-UX but it should work. It allows you to move to a location in a file and truncate. I'm pretty sure that it does not read the whole file in but just seeks to the appropriate location for display.

Usage is fairly simple once you have launched it the arrow keys allow you to move around. F1 gives help. Ctrl-G moves to a location in the file (hint: to move to end use the size of the file from the bottom row of the display). Position the cursor on the first byte that you want to truncate and then press Escape T once you confirm the truncate will have been done. Ctrl-x exits.

share|improve this answer
    
wow ... this is what I am looking for. Will be usful in future as well :-). Thanks. –  Hemant Aug 26 '10 at 17:07
add comment

Cut 2 kilobytes from end of file:

truncate -s-2K file
share|improve this answer
    
I don't have truncate installed :(. –  Hemant Aug 26 '10 at 13:56
    
It is in coreutils (at least 8.5). –  Maciej Piechotka Aug 26 '10 at 14:02
    
Also according to packages.ubuntu.com it is present in ubuntu 9.10 and 10.04 –  Maciej Piechotka Aug 26 '10 at 14:08
5  
He says he's using HP-UX, not Ubuntu. –  frabjous Aug 26 '10 at 15:38
2  
GNU Coreutils for HP-UX hpux.connect.org.uk/hppd/hpux/Gnu/coreutils-8.5 –  unhillbilly Aug 27 '10 at 13:20
add comment

You can use dd for example:

dd if=yourfile of=outname bs=4k count=thefirstX4kb
share|improve this answer
1  
Thanks for your response. I prefer to edit file in place to save disk space. If nothing helps I will use dd :-). –  Hemant Aug 26 '10 at 10:28
add comment

Use a tool that gives you access to the truncate system call. You can do it with only POSIX tools. Warning, typed into a browser; be especially careful as dd is even more unforgiving of errors than the usual unix command. 123456 is the number of bytes to keep.

dd if=/dev/null of=/file/to/truncate seek=1 bs=123456

A Perl version is much more readable:

perl -e 'truncate "$ARGV[0]", 123456 or die $!' /file/to/truncate
share|improve this answer
add comment

You can use split or ed, awk or any programming language.

share|improve this answer
    
Thanks for your response. I prefer to edit file in place to save disk space and memory. –  Hemant Aug 26 '10 at 17:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.