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