How to truncate large file on osx?

I have a 500mb file. I want to keep only the first 5 mb. How can I truncate it on osx where the 'truncate' command isn't available (and can't be installed by homebrew)?


Solution 1:

  1. Install truncate

    brew install truncate

  2. Truncate a file to 0 size:

    truncate -s 0 your_filename

Solution 2:

I know this is an old question, but as of Oct 2018 you can install it via homebrew:

> brew info truncate
truncate: stable 0.9 (bottled), HEAD
Truncates a file to a given size
https://www.vanheusden.com/truncate/
/usr/local/Cellar/truncate/0.9 (4 files, 49.3KB) *
  Poured from bottle on 2018-10-31 at 14:54:21
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/truncate.rb
==> Options
--HEAD
    Install HEAD version
==> Analytics
install: 281 (30 days), 861 (90 days), 3,138 (365 days)
install_on_request: 283 (30 days), 862 (90 days), 3,124 (365 days)
build_error: 0 (30 days)

Solution 3:

You should probably try to use this - The null device /dev/null act as the black hole that discards all data written to it under Unix like operating system. You can try to use it as follows :

cp /dev/null largefile.txt OR cat /dev/null > largefile.txt

whichever goes right, must be the right thing to do in future.