14

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?

HopelessN00b
  • 53,795
  • 33
  • 135
  • 209
Raptor
  • 1,001
  • 4
  • 19
  • 38
  • 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
  • 1
    The 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 Answers4

20

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.

Alex Yursha
  • 301
  • 2
  • 4
17

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:

Link

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
Glorfindel
  • 1,213
  • 4
  • 15
  • 22
Alex Berry
  • 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
  • 2
    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: http://www.magiclantern.fm/forum/index.php?topic=6407.0 – msmafra Nov 03 '14 at 15:04
5

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.

Ian Mackinnon
  • 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
  • 1
    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 09:38
  • I like this answer as it doesn't require me to install anything new. – Demitri Oct 26 '21 at 06:36
1

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 tailed.

:> file

Works on both *nix and macOS to truncate a file in-place.

karmakaze
  • 111
  • 3