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?

3
  • What are you trying to do?
    – quanta
    Sep 12, 2012 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, 2012 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, 2021 at 20:56

4 Answers 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
4
  • I found that truncate in homebrew is different than Ubuntu version. It does not have option flag.
    – Raptor
    Sep 12, 2012 at 10:47
  • There's also Fink: finkproject.org and Macports: macports.org you could try, before doing so run brew uninstall truncate, though.
    – Alex Berry
    Sep 12, 2012 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, 2012 at 10:56
  • Now, using Yosemite the following solved for me (also using home brew: magiclantern.fm/forum/index.php?topic=6407.0
    – msmafra
    Nov 3, 2014 at 15:04
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.

3
  • 2
    This should be the accepted Answer.
    – fguillen
    Jan 5, 2016 at 13:58
  • 2
    Looks like truncate is available in Homebrew again as its own formula.
    – Navin
    Jul 17, 2018 at 22:40
  • ...and it's gone again. Dec 2, 2023 at 12: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.

3
  • 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, 2012 at 3: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. Oct 22, 2012 at 9:38
  • I like this answer as it doesn't require me to install anything new.
    – Demitri
    Oct 26, 2021 at 6: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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .