How to empty (truncate) the file without deleting the file

Home Questions How to empty (truncate) the file without deleting the file

This topic contains 1 reply, has 2 voices, and was last updated by   2 years ago.

  • Author
    Posts
  • #6691 Reply

    admin
    Keymaster
    Offline
    Topics: 65
    Replies: 65

    I want to delete only the content of the log file in UNIX without deleting the complete file. Instead of deleting and recreating the file, is there any command to truncate the file content alone?

  • #6779 Reply

    admin
    Keymaster
    Offline
    Topics: 65
    Replies: 65

    I would recommend to delete and recreate a fresh file since truncating the file is not a good idea. If you want to truncate then try this,

    cat /dev/null > file_name

    /dev/null is an empty file which is present in all the machines. Here I used cat command to amend that empty file to the filename. So the content of the file will get truncated. cheers…

Reply To: How to empty (truncate) the 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: