xref: /freebsd/usr.bin/truncate/truncate.1 (revision 4b15965daa99044daf184221b7c283bf7f2d7e66)
1.\"
2.\" SPDX-License-Identifier: BSD-2-Clause
3.\"
4.\" Copyright (c) 2000 Sheldon Hearn <sheldonh@FreeBSD.org>.
5.\" All rights reserved.
6.\" Copyright (c) 2021 The FreeBSD Foundation
7.\"
8.\" Portions of this manual page were written by Ka Ho Ng <khng@FreeBSD.org>
9.\" under sponsorship from the FreeBSD Foundation.
10.\"
11.\" Redistribution and use in source and binary forms, with or without
12.\" modification, are permitted provided that the following conditions
13.\" are met:
14.\" 1. Redistributions of source code must retain the above copyright
15.\"    notice, this list of conditions and the following disclaimer.
16.\" 2. Redistributions in binary form must reproduce the above copyright
17.\"    notice, this list of conditions and the following disclaimer in the
18.\"    documentation and/or other materials provided with the distribution.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.Dd July 9, 2025
33.Dt TRUNCATE 1
34.Os
35.Sh NAME
36.Nm truncate
37.Nd resize files or manage file space
38.Sh SYNOPSIS
39.Nm
40.Op Fl c
41.Bk -words
42.Fl s Xo
43.Sm off
44.Op Cm + | - | % | /
45.Ar size
46.Op Cm SUFFIX
47.Sm on
48.Xc
49.Ek
50.Ar
51.Nm
52.Op Fl c
53.Bk -words
54.Fl r Ar rfile
55.Ek
56.Ar
57.Nm
58.Op Fl c
59.Bk -words
60.Fl d
61.Oo
62.Fl o Xo
63.Sm off
64.Ar offset
65.Op Cm SUFFIX
66.Sm on
67.Xc
68.Oc
69.Fl l Xo
70.Sm off
71.Ar length
72.Op Cm SUFFIX
73.Sm on
74.Xc
75.Ek
76.Ar
77.Sh DESCRIPTION
78The
79.Nm
80utility adjusts the length of each regular file given on the command-line, or
81performs space management with the given offset and the length over a regular
82file given on the command-line.
83.Pp
84The following options are available:
85.Bl -tag -width indent
86.It Fl c
87Do not create files if they do not exist.
88The
89.Nm
90utility does not treat this as an error.
91No error messages are displayed
92and the exit value is not affected.
93.It Fl r Ar rfile
94Truncate or extend files to the length of the file
95.Ar rfile .
96.It Fl s Xo
97.Sm off
98.Op Cm + | - | % | /
99.Ar size
100.Op Cm SUFFIX
101.Sm on
102.Xc
103If the
104.Ar size
105argument is preceded by a plus sign
106.Pq Cm + ,
107files will be extended by this number of bytes.
108If the
109.Ar size
110argument is preceded by a dash
111.Pq Cm - ,
112file lengths will be reduced by no more than this number of bytes,
113to a minimum length of zero bytes.
114If the
115.Ar size
116argument is preceded by a percent sign
117.Pq Cm % ,
118files will be round up to a multiple of this number of bytes.
119If the
120.Ar size
121argument is preceded by a slash sign
122.Pq Cm / ,
123files will be round down to a multiple of this number of bytes,
124to a minimum length of zero bytes.
125Otherwise, the
126.Ar size
127argument specifies an absolute length to which all files
128should be extended or reduced as appropriate.
129.It Fl d
130Zero a region in the specified file.
131If the underlying file system of the given file supports hole-punching,
132file system space deallocation may be performed in the operation region.
133.It Fl o Ar offset
134The space management operation is performed at the given
135.Ar offset
136bytes in the file.
137If this option is not specified,
138the operation is performed at the beginning of the file.
139.It Fl l Ar length
140The length of the operation range in bytes.
141This option must always be specified if option
142.Fl d
143is specified, and must be greater than 0.
144.El
145.Pp
146The
147.Ar size ,
148.Ar offset
149and
150.Ar length
151arguments may be suffixed with one of
152.Cm K ,
153.Cm M ,
154.Cm G
155or
156.Cm T
157(either upper or lower case) to indicate a multiple of
158Kilobytes, Megabytes, Gigabytes or Terabytes
159respectively.
160.Pp
161Exactly one of the
162.Fl r ,
163.Fl s
164and
165.Fl d
166options must be specified.
167.Pp
168If a file is made smaller, its extra data is lost.
169If a file is made larger,
170it will be extended as if by writing bytes with the value zero.
171If the file does not exist,
172it is created unless the
173.Fl c
174option is specified.
175.Pp
176Note that,
177while truncating a file causes space on disk to be freed,
178extending a file does not cause space to be allocated.
179To extend a file and actually allocate the space,
180it is necessary to explicitly write data to it,
181using (for example) the shell's
182.Ql >>
183redirection syntax, or
184.Xr dd 1 .
185.Sh EXIT STATUS
186.Ex -std
187If the operation fails for an argument,
188.Nm
189will issue a diagnostic
190and continue processing the remaining arguments.
191.Sh EXAMPLES
192Adjust the size of the file
193.Pa test_file
194to 10 megabytes but do not create it if it does not exist:
195.Bd -literal -offset indent
196truncate -c -s 10M test_file
197.Ed
198.Pp
199Same as above but create the file if it does not exist:
200.Bd -literal -offset indent
201truncate -s +10M test_file
202ls -lh test_file
203-rw-r--r--  1 root  wheel    10M Jul 22 18:48 test_file
204.Ed
205.Pp
206Adjust the size of
207.Pa test_file
208to the size of the kernel and create another file
209.Pa test_file2
210with the same size:
211.Bd -literal -offset indent
212truncate -r /boot/kernel/kernel test_file test_file2
213ls -lh /boot/kernel/kernel test_file*
214-r--r--r--  1 root  wheel    30M May 15 14:18 /boot/kernel/kernel
215-rw-r--r--  1 root  wheel    30M Jul 22 19:15 test_file
216-rw-r--r--  1 root  wheel    30M Jul 22 19:15 test_file2
217.Ed
218.Pp
219Increase the size of the file
220.Pa test_file
221by 5 megabytes but do not create it if it does not exist:
222.Bd -literal -offset indent
223truncate -s +5M test_file
224ls -l test_file*
225-rw-r--r--  1 root  wheel    36595432 Sep 20 19:17 test_file
226-rw-r--r--  1 root  wheel    31352552 Sep 20 19:15 test_file2
227.Ed
228.Pp
229Reduce the size of the file
230.Pa test_file
231by 5 megabytes:
232.Bd -literal -offset indent
233truncate -s -5M test_file
234ls -lh test_file*
235-rw-r--r--  1 root  wheel    25M Jul 22 19:17 test_file
236-rw-r--r--  1 root  wheel    30M Jul 22 19:15 test_file2
237.Ed
238.Sh SEE ALSO
239.Xr dd 1 ,
240.Xr touch 1 ,
241.Xr fspacectl 2 ,
242.Xr truncate 2
243.Sh STANDARDS
244The
245.Nm
246utility conforms to no known standards.
247.Sh HISTORY
248The
249.Nm
250utility first appeared in
251.Fx 4.2 .
252.Sh AUTHORS
253.An -nosplit
254The
255.Nm
256utility was written by
257.An Sheldon Hearn Aq Mt sheldonh@starjuice.net .
258Hole-punching support of this
259utility was developed by
260.An Ka Ho Ng Aq Mt khng@FreeBSD.org .
261