LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-11-2009, 02:02 AM   #1
Narcopolypse
LQ Newbie
 
Registered: Aug 2009
Posts: 3

Rep: Reputation: 1
Trim a file in place without copying.


I have a large file that needs to be trimmed by a shell script periodically. Normally I would just use the head command or dd but this is running on an embedded system and won't always have enough free space to make a copy of the file. Does anyone know of a way I can trim off part of the end of a file without making a copy of it?

Last edited by Narcopolypse; 08-18-2009 at 11:57 PM.
 
Old 08-11-2009, 02:19 AM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Have a look at the 'truncate' and 'ftruncate' posix calls.

There are some tools in the embutils package that may help (including 'truncate'), although I have not tried these.

Last edited by neonsignal; 08-11-2009 at 02:28 AM.
 
Old 08-11-2009, 02:21 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
If you don't mind using a more powerful lang like Perl or C, you can use the seek() fn to move to your new endpoint and write an EOF there.

http://perldoc.perl.org/functions/seek.html
http://linux.die.net/man/3/fseek
 
Old 08-12-2009, 01:58 AM   #4
Narcopolypse
LQ Newbie
 
Registered: Aug 2009
Posts: 3

Original Poster
Rep: Reputation: 1
Truncate seems to be as close to a perfect solution as I can get. Unfortunately I must use the device's SDK to compile it. Oh well, now I know what I'm doing on my day off.
 
Old 08-18-2009, 11:54 PM   #5
Narcopolypse
LQ Newbie
 
Registered: Aug 2009
Posts: 3

Original Poster
Rep: Reputation: 1
Never mind, I actually did find THE perfect solution!
Code:
dd if=/dev/null bs=1k seek=new_file_size_in_KB of=filename
 
Old 08-19-2009, 06:41 AM   #6
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
Hey that is a neat solution!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Trim first 10 lines out of a file hattori.hanzo Linux - Newbie 7 11-12-2008 08:40 AM
Need to trim some bytes off the end of a file onedingo Linux - Software 5 07-04-2007 06:13 PM
trim lines based on other file. hepburnenthorpe Linux - Newbie 1 04-02-2007 10:07 PM
Will my XF86Config file work in place of xorg.conf file BillyGalbreath Linux - General 2 03-08-2006 06:18 AM
How To Trim A File fpfernando Programming 11 01-05-2006 08:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration