E1Tips Qucik Tip

Quick Tip: 3 Ways To Truncate a File in Linux

Read Time:30 Sec

The following are 3 quick ways to delete the contents of  (truncate) a file in Linux:

  1. Truncate
    bash$ truncate -s 0 filename.txt
  2. I/O Redirection
    bash$ > filename.txt
  3. /dev/null
    bash$ cp /dev/null filename.txt

If you get an error it may be because the noclobber feature is enabled on your file. The noclobber feature of Linux is used to prevent accidental overwriting of files. It can be overridden by using either of the following depending on the shell you are using:

  • For csh – bash$ >! filename.txt
  • For ksh – bash$ >| filename.txt

If you have any other tricks to truncate files, let me know.

Author

Stewart Schatz

Career: Principal CNC Consultant for Syntax Systems Limited specializing Oracle JD Edwards EnterpriseOne and the technology that supports it. Side Hustle: Owner/Operator of E1Tips.com Location: Lancaster, PA USA  What I like to do: Invest in Family, Explore Technology, Lead Teams, Share Knowledge/Experience, Hunt, Hike, etc.

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

E1Tips.com - SQL Ninja Previous post How To Disable Client Package Deployment
Next post Needle: IBM iSeries (AS400) Power Outage JDE Recovery