How to Truncate a file without deleting the file

Home Questions How to Truncate a file without deleting the file

This topic contains 1 reply, has 1 voice, and was last updated by  admin 1 year, 8 months ago.

  • Author
    Posts
  • #6701 Reply

    Kolan

    I want to write a shell script in which the file will be truncated if there is any unknown data found. Any option to truncate the UNIX file or variable instead of deleting the original file?

  • #6792 Reply

    Deepa SP

    Try this:

    cp /dev/null filename
    

    /dev/null is an empty file which is present in all the UNIX and LINUX machines.
    Copying this file will overwrite the content of the original file and make it empty.

    cat /dev/null > filename
    

    This command also does the same job.

Reply To: How to Truncate a file without deleting the file

You can use BBCodes to format your content.
Your account can't use Advanced BBCodes, they will be stripped before saving.

Your information: