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?
-
What are you trying to do? – quanta Sep 12 '12 at 09: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
-
1The question is posed as 'how to install truncate', but a workable answer might be `:> file` which works equally well on Ubuntu and macOS. (see alternate answer below) – karmakaze Nov 21 '21 at 20:56
4 Answers
Update:
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
.

- 301
- 2
- 4
-
2
-
2Looks like `truncate` is available in Homebrew again as its own formula. – Navin Jul 17 '18 at 22:40
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 https://raw.githubusercontent.com/Homebrew/install/master/install)"
To install homebrew, then when it's installed, run:
brew install truncate

- 1,213
- 4
- 15
- 22

- 2,307
- 13
- 23
-
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: http://www.finkproject.org/ and Macports: http://www.macports.org/ you could try, before doing so run brew uninstall truncate, though. – Alex Berry Sep 12 '12 at 10:56
-
2Also 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: http://www.magiclantern.fm/forum/index.php?topic=6407.0 – msmafra Nov 03 '14 at 15:04
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.

- 183
- 2
- 15
-
`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 03:20
-
1Not 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 09:38
-
One point of consideration is if the truncated file is the same file or a new file with the same name. Of importance for log files that are still being written-to, or being tail
ed.
:> file
Works on both *nix and macOS to truncate a file in-place.

- 111
- 3