我有一个 500mb 的文件。我只想保留前 5mb。如何在 osx 上截断它,因为 'truncate' 命令不可用(并且无法通过 homebrew 安装)?
答案1
安装 truncate
brew install truncate
将文件截断为 0 大小:
truncate -s 0 your_filename
答案2
您可能应该尝试使用这个 - 空设备 /dev/null 充当黑洞,在类 Unix 操作系统下丢弃写入它的所有数据。您可以尝试按如下方式使用它:
cp /dev/null largefile.txt 或 cat /dev/null > largefile.txt
无论哪件事做对了,那它未来要做的就一定是正确的事情。
答案3
我知道这是一个老问题,但截至 2018 年 10 月,您可以通过 homebrew 安装它:
> brew info truncate
truncate: stable 0.9 (bottled), HEAD
Truncates a file to a given size
https://www.vanheusden.com/truncate/
/usr/local/Cellar/truncate/0.9 (4 files, 49.3KB) *
Poured from bottle on 2018-10-31 at 14:54:21
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/truncate.rb
==> Options
--HEAD
Install HEAD version
==> Analytics
install: 281 (30 days), 861 (90 days), 3,138 (365 days)
install_on_request: 283 (30 days), 862 (90 days), 3,124 (365 days)
build_error: 0 (30 days)
2022 年 9 月更新:
╰─ brew info coreutils
==> coreutils: stable 9.1 (bottled), HEAD
GNU File, Shell, and Text utilities
https://www.gnu.org/software/coreutils
Conflicts with:
aardvark_shell_utils (because both install `realpath` binaries)
b2sum (because both install `b2sum` binaries)
ganglia (because both install `gstat` binaries)
gdu (because both install `gdu` binaries)
idutils (because both install `gid` and `gid.1`)
md5sha1sum (because both install `md5sum` and `sha1sum` binaries)
truncate (because both install `truncate` binaries)
uutils-coreutils (because coreutils and uutils-coreutils install the same binaries)
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/coreutils.rb
License: GPL-3.0-or-later
==> Dependencies
Required: gmp ✘
==> Options
--HEAD
Install HEAD version
==> Caveats
Commands also provided by macOS and the commands dir, dircolors, vdir have been installed with the prefix "g".
If you need to use these commands with their normal names, you can add a "gnubin" directory to your PATH with:
PATH="/opt/homebrew/opt/coreutils/libexec/gnubin:$PATH"
==> Analytics
install: 86,771 (30 days), 265,204 (90 days), 1,257,918 (365 days)
install-on-request: 62,729 (30 days), 190,281 (90 days), 885,118 (365 days)
build-error: 132 (30 days)
答案4
: > filename
冒号表示真且不产生任何输出,因此没有任何内容被重定向到文件 - 从而覆盖其所有内容。