Truncate Large Text File in UNIX / Linux

How do I truncate or shrink large text file under UNIX / Linux operating systems?

There are various tools and methods to truncate

Tutorial details
Difficulty level Easy
Root privileges No
Requirements None
Est. reading time 1 minutes
large text files under UNIX / Linux operating systems as follows.
Advertisement

Options #1: Shell Output Redirction

Your can truncate text file and make the size to zero using redirection:

> {filename}
ls -l largefile.txt
> largefile.txt
ls -l largefile.txt

Please note that largefile.txt file is created if it doesn’t exist. And largefile.txt file is overwritten if it exists.

Option #2: truncate Command

Use the truncate command to shrink or extend the size of each FILE to the specified size:

truncate -s 0 {filename.txt}
ls -lh filename.txt
truncate -s 0 filename.txt
ls -lh filename.txt

The -s option is used to set SIZE to zero. See truncate command man page for more details:
man truncate

Option #3: logrotate Utility

logrotate command is designed to ease administration of systems that generate large numbers of log files. It allows automatic rotation, compression, removal, and mailing of log files. Each log file may be handled daily, weekly, monthly, or when it grows too large. See how to use logrotate command to rotates, compresses, and mails system logs stored in /var/log and other locations under UNIX / Linux oses.

Option #4: /dev/null

The null device /dev/null act as the black hole that discards all data written to it under Unix like operating system. You can use it as follows (hat tip to Philippe Petrinko):

cp /dev/null largefile.txt

OR

cat /dev/null > largefile.txt

πŸ₯Ί Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🀠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

23 comments… add one
  • Jay Jul 31, 2011 @ 0:40

    mentioning the ‘truncate’ command (as opposed to the library call), makes this Linux only (or other Unix-like without its own file utils, relying on GNU).

  • Rama Akella Sep 27, 2012 @ 7:14

    Please execute the below command in shell

    > filename

    it will truncate the file..

    Thanks,,
    Rama

  • Philippe Petrinko Sep 27, 2012 @ 20:49

    BTW Vivek, when will you add my options, as they are functional and relevant?

    And yes, please modify “exits” into “exists” as Shoaibi pointed out, don’t you think?

    • πŸ›‘οΈ Vivek Gite (Author and Admin) nixCraft Oct 2, 2012 @ 9:54

      Done. The faq has been updated.

      I appreciate your feedback :)

  • Vex Oct 22, 2012 @ 23:51

    Rama Akella points out the useless use of cat . The end goal is that you don’t want to change the inode of the file you wish to truncate to zero bytes. It’s functionally equivalent to run `> file` to accomplish this.

    http://partmaps.org/era/unix/award.html#cat

    The truncate option that truncates the file in place saves me from the problem of having to dd the file to a temp file and write it back as I’m dealing with files in the hundreds of gigabytes. My outdated install of Gentoo doesn’t provide truncate in coreutils. I instead found the source from a FreeBSD machine and compiled it manually, not knowing at the time that the newer coreutils includes it.

    Locate the truncate.c file in the FreeBSD world source tree. I found it here:
    /usr/src/usr.bin/truncate/truncate.c
    Copy this to your *nix system and compile it
    gcc -o truncate truncate.c
    Test it out in case the C libraries are too divergent before you use it.

    For Gentoo I created an ebuild for those who are not able to update coreutils. I offer absolutely no warranties of my work or the files that I am using. I am redistributing the code without modification.

  • Arun Bagul Nov 27, 2012 @ 3:29

    Good Article and simple topic.

  • roger Feb 28, 2013 @ 18:17

    Is there a yum package I’m missing?

    bash: truncate: command not found

    • Vex Mar 1, 2013 @ 7:52

      Roger, I made a post about this issue, http://www.devunmounted.com/2012/10/22/truncate-your-proble/ , I hope it helps you with your problem. The jest is that the truncate ships with a newer glibc, but if you don’t have a newer glibc you can either compile truncate.c from the newer glibc or from a libc bsd source. I believe my write up on it should cover both. Good luck.

  • jangyman Sep 8, 2014 @ 20:28

    I believe ‘truncate’ means to shorten, not (necessarily) to empty.
    I would like to shorten a very large log file to, say, 1MB.
    I wonder if Philippe P’s method demonstrates this? Thx!

    • Philippe Petrinko Sep 9, 2014 @ 6:38

      @jangyman

      Why would you do that?
      What part of the original file would you keep? The first 1Mbytes, or the last 1MB.

      Assuming you do not care whatever is still in your log file, (otherwise you would not consider loosing data by truncating it), your could :

      #Β first empty it
      cp /dev/null /somedir/mylogfile

      # enlarge your log file to 1 MB
      dd if=/dev/zero of=/somedir/mylogfile bs=1M count=1

      What do you think?

      • Robert Nov 25, 2014 @ 17:57

        You’d be better off typing
        truncate -s 1000000 /var/log/*log

        i usually do something like
        truncate -s 24000 /var/log/*log

        but my router is the linux machine, so 8 gigs is all i have of data. and some people only have 2-4 gigs.

  • David Sep 26, 2016 @ 9:08

    even shorter:
    :> toobigfile

Leave a Reply

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

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.