Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there any way to install Ubuntu's truncate command to Mac OS X 10.7 in order to use the command in Terminal shell?

share|improve this question
What are you trying to do? – quanta Sep 12 '12 at 9:04
I have a shell script that makes sure of truncate function, which I want to try it in local Mac. – Raptor Sep 12 '12 at 10:47
up vote 4 down vote accepted

This shouldn't really be in Server Fault, probably Super User or Ask Different, but you can install a lot of binaries by using homebrew, found here:

Firstly you'll need to install command line tools for Xcode, which will probably require you to purchase it if you havn't already.

Once you've done this, run:

ruby -e "$(curl -fsSL"

To install homebrew, then when it's installed, run:

brew install truncate
share|improve this answer
I found that truncate in homebrew is different than Ubuntu version. It does not have option flag. – Raptor Sep 12 '12 at 10:47
There's also Fink: and Macports: you could try, before doing so run brew uninstall truncate, though. – Alex Berry Sep 12 '12 at 10:56
Also would appreciate an upvote as, regardless of if it worked for you, I did provide a valid answer to your question :) – Alex Berry Sep 12 '12 at 10:56
Now, using Yosemite the following solved for me (also using home brew: – tenshimsm Nov 3 '14 at 15:04


As of March 2015 truncate is no longer available in Homebrew as a standalone formula.

As truncate is part of GNU Coreutils you should install it on OS X with the following command:

> brew install coreutils

After installation truncate will be available under the name gtruncate. Note that all programs from Coreutils will be available with the prefix g.

share|improve this answer
Good catch. Thanks for your version note. – Raptor Mar 30 '15 at 2:27
This should be the accepted Answer. – fguillen Jan 5 at 13:58

When truncate is not available you can often use dd instead. Eg.

Make a file full of zeros:

dd if=/dev/zero of=/tmp/youroutputfile bs=1m count=50

Truncate an existing file:

dd if=yourinputfile of=/tmp/youroutputfile bs=1k count=1

Note: the OS X dd uses lower case unit letters where GNU dd uses upper case.

share|improve this answer
dd can almost achieve the goal. Can dd truncate from the end of file? As I read from the man file of dd, I know that it can only be read from file beginning. How about I delete last 100 bytes from the file ? – Raptor Oct 22 '12 at 3:20
Not as far as I know. You'd have to do the target size calculation yourself by reading the file size first and subtracting 100 bytes. – Ian Mackinnon Oct 22 '12 at 9:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.