The truncate
command is a useful utility in Ubuntu/Linux systems for shrinking or extending files to a specified size. While macOS has similar functionality through other commands, developers familiar with Linux often prefer the straightforward syntax of truncate
.
Before attempting to install the Linux version, consider these native macOS alternatives:
# Using dd to truncate (destructive)
dd if=/dev/null of=target_file bs=1 seek=100K # Sets file to 100KB
# Using Perl for non-destructive truncation
perl -e 'truncate "filename", 100000' # Truncates to 100KB
The most reliable way to get truncate
on macOS is through GNU Coreutils:
# Install Homebrew if you don't have it
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
# Install coreutils
brew install coreutils
# Create symbolic link (optional but recommended)
ln -s /usr/local/bin/gtruncate /usr/local/bin/truncate
Note that the command will be available as gtruncate
unless you create the symlink.
For developers who prefer building from source:
# Download coreutils source
curl -O http://ftp.gnu.org/gnu/coreutils/coreutils-8.32.tar.xz
tar -xf coreutils-8.32.tar.xz
cd coreutils-8.32
# Configure and make
./configure --prefix=/usr/local
make
sudo make install
Once installed, you can use truncate
as you would on Ubuntu:
# Set file to exactly 1MB
truncate -s 1M logfile.txt
# Reduce file by 500KB
truncate -s -500K large_file.dat
# Empty a file completely
truncate -s 0 /var/log/app.log
If you encounter permission problems when installing, try:
sudo chown -R $(whoami) /usr/local/share/man/man8
brew link --overwrite coreutils
For Lion (10.7) specifically, you might need Xcode Command Line Tools installed:
xcode-select --install
Coming from Ubuntu to macOS 10.7 (Lion), you'll quickly notice the absence of the handy truncate
command in Terminal. This core utility in Linux systems lets you:
- Set exact file sizes (-s option)
- Extend or shrink files efficiently
- Create sparse files instantly
Before installing anything, consider these built-in options:
# Create empty file (equivalent to truncate -s 0)
touch filename
# Set file to specific size (bytes)
mkfile -n 1g largefile.img # 1GB file
dd if=/dev/zero of=file bs=1 count=0 seek=1M # 1MB sparse file
The most robust solution is to install GNU coreutils:
# Install Homebrew if missing
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
# Install coreutils (includes truncate)
brew install coreutils
# Access via g-prefix
gtruncate -s 10M testfile
For a lightweight solution, add this to your ~/.bash_profile
:
truncate() {
if [ "$1" = "-s" ]; then
size=$2
file=$3
dd if=/dev/null of="$file" bs=1 seek="$size" &>/dev/null
else
echo "Usage: truncate -s SIZE FILE"
fi
}
# Truncate log file to zero bytes
gtruncate -s 0 /var/log/system.log
# Create 500MB test file
gtruncate -s 500M benchmark.data
# Reduce database dump size
gtruncate -s 1G largedump.sql
Remember macOS's SIP protection may restrict operations on system files. Use sudo
when needed:
sudo gtruncate -s 0 /private/var/log/install.log
The GNU version outperforms shell workarounds for large files due to direct syscall usage. Benchmark with:
time gtruncate -s 10G sparse.file
time dd if=/dev/null of=dd.file bs=1 seek=10G