Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6558971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:15:54+00:00 2026-05-25T13:15:54+00:00

In Linux, I have a rather large file with some extraneous information tacked on

  • 0

In Linux, I have a rather large file with some extraneous information tacked on to the end of it. Let’s say for example I know there are 314 bytes of extraneous data at the end of a 1.6GB file.

Of course it is very easy and efficient to add more data to the end of a file, but what can I do to remove it without having to copy the first portion of that file into another (or overwrite said file)?

Edit

I’m seeing some good advice on doing this in C. I was hoping to script it from the commandline, but failing that I would be more inclined to doing it in python than C.

I see that python has a truncate method on its file object but it seems to be demolishing my file no matter how i use it–I should be able to figure this one out, but of course answers are more than welcome still.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:15:55+00:00Added an answer on May 25, 2026 at 1:15 pm

    use the function truncate

    http://linux.die.net/man/2/truncate

    int truncate(const char *path, off_t length);
    int ftruncate(int fd, off_t length); 
    

    truncate takes the file name
    ftruncate takes an open file descriptor

    both of these set the file length to length so it either truncates or elongates (in the latter case, the rest of the file will be filled with NULL/ZERO)

    [edit]
    truncate (linux shell command) will work also

    **SYNTAX**
    
    truncate -s integer <filename>  
    **OPTIONS**
    
    -s number specify the new file length. If the new length is smaller than the current filelength data is lost. If the new length is greater the file is padded with 0. You can specify a magnitude character to ease large numbers:
    b or B size is bytes.
    k size is 1000 bytes.
    K size is 1024 bytes.
    m size is 10^6 bytes.
    M size is 1024^2 bytes.
    g size is 10^9 bytes.
    G size is 1024^3 bytes.
    
    
    **EXAMPLES**
    
    To shrink a file to 10 bytes:
    
    truncate -s 10 /tmp/foo
    
    To enlarge or shrink a file to 345 Megabytes:
    
    truncate -s 345M /tmp/foo
    

    [/edit]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to have Linux read ahead when cloning a disk? I
I have 3 Linux machines, and want some way to keep the dotfiles in
I have a number of rather large binary files (fixed length records, the layout
I have just joined a project with a rather large existing code base. We
I'm a longtime Windows and Linux user and have had some weird experiences writing
I have some python scripts that configure linux computers. One of the tasks is
I have a PHP script that I am using to generate some rather complicated
I have a file that's been modified recently. Rather than looking into each revision,
Let's say I have 10 programs (in terminals) working in tandem: {p1,p2,p3,...,p10}. It's hard
I'm new to Linux and have inherited keeping our single linux server running. It's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.