xref: /netbsd-src/usr.bin/touch/touch.1 (revision bb2336b065cfbaa4746521693d41dc0fa2ec4637)
1.\"	$NetBSD: touch.1,v 1.30 2024/03/17 21:37:53 andvar Exp $
2.\"
3.\" Copyright (c) 1991, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" the Institute of Electrical and Electronics Engineers, Inc.
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.\" 3. Neither the name of the University nor the names of its contributors
18.\"    may be used to endorse or promote products derived from this software
19.\"    without specific prior written permission.
20.\"
21.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31.\" SUCH DAMAGE.
32.\"
33.\"     @(#)touch.1	8.3 (Berkeley) 4/28/95
34.\"
35.Dd February 10, 2024
36.Dt TOUCH 1
37.Os
38.Sh NAME
39.Nm touch
40.Nd change file access and modification times
41.Sh SYNOPSIS
42.Nm
43.Op Fl acDfhm
44.Op Fl d Ar posix-datetime|human-datetime
45.Op Fl Fl \|date Ar posix-datetime|human-datetime
46.Op Fl R Ar ref-file
47.Op Fl r Ar ref-file
48.Op Fl Fl \|reference Ar ref-file
49.Op Fl t Ar datetime
50.Ar file ...
51.Sh DESCRIPTION
52The
53.Nm
54utility changes either or both of the access and modification times of the
55.Ar file Ns s
56to the time specified by the options, described below,
57or to the current time of day, if none of those options is present.
58If the file doesn't exist, it is first created with default permissions.
59.Pp
60The following options are available:
61.Bl -tag -compact -width Fl
62.Pp
63.It Fl a
64Change the access time of the
65.Ar file .
66The modification time of the
67.Ar file
68is not changed unless the
69.Fl m
70flag is also specified.
71.Pp
72.It Fl c
73Do not create the
74.Ar file
75if it does not exist.
76The
77.Nm
78utility does not treat this as an error.
79No error messages are displayed and the exit value is not affected.
80.Pp
81.It Fl D
82Do not attempt to adjust a
83.Ar file Ns 's
84times if they are already set to the values specified.
85.Pp
86.It Fl d Ar posix-datetime
87.It Fl d Ar human-datetime
88.It Fl Fl \|date Ar posix-datetime
89.It Fl Fl \|date Ar human-datetime
90Attempt to parse the arg
91.Ar posix-datetime
92as a POSIX time string
93.Dq CCYY\-MM\-DDThh:mm:ss[.frac][Z]
94where the minus (or hyphen)
95.Pq Sq \&\-
96and colon
97.Pq Sq \&:
98characters are literals, and:
99.Bl -bullet -compact
100.It
101.Cm CCYY
102represents a 4 (or more) digit year number,
103.It
104.Cm MM
105represents a 2 digit month number (1\-12),
106.It
107.Cm DD
108represents a 2 digit day of the month (1\-31),
109.It
110.Cm T
111represents either the character
112.Sq T
113or a single space character (in which case the
114space, at least, may need to be quoted to the shell to
115avoid the arg being split into two words),
116.It
117.Cm hh
118represents a 2 digit hour of the day (00\-23),
119.It
120.Cm mm
121represents a 2 digit minute of the hour (00\-59),
122.It
123.Cm ss
124represents a 2 digit second of the minute (00\-60)
125where 60 indicates the occurrence of a leap second,
126which POSIX systems ignore, resulting in the following
127second being generated instead (:00 of the next minute),
128.It
129.Cm .frac
130represents optional factional seconds, where the
131.Sq \&.
132can be a period
133.Pq Sq \&.
134or a comma
135.Pq Sq \&,
136and
137.Cm frac
138gives one or more digits, interpreted as if
139in a floating-point representation of the seconds,
140so
141.Dq \&.3
142represents three tenths of a second, and
143.Dq \&,17
144represents seventeen hundredths of a second, etc.
145Note that if the period or comma is given, there
146must be at least one following digit.
147If no fraction of a second is to be specified,
148also omit the period (or comma).
149If omitted, the fractional seconds are set to 0,
150so specifying
151.Dq \&.0
152or
153.Dq \&,0
154is identical to omitting the
155.Cm \&.frac
156field entirely,
157.It
158.Cm Z
159represents an optional literal
160.Sq Z
161character, indicating the the time given is to
162be considered as a Co-ordinated Universal Time (UTC) value.
163If omitted, the time is considered as being in the local
164timezone, as specified by the
165.Ev TZ
166environment variable.
167.El
168.Pp
169Note that parsing of this string is quite strict.
170If successfully parsed, the time to set will be that
171specified by this string.
172.Pp
173If the attempt to parse the string as a
174.Ar posix-datetime
175fails, then
176.Nm
177will attempt to parse it as a
178.Ar human-datetime
179using the human datetime parser
180.Xr parsedate 3 ,
181and use the result as the time to set.
182.Pp
183.It Fl f
184This flag has no effect; it is accepted for compatibility reasons.
185.Pp
186.It Fl h
187If a
188.Ar file
189is a symbolic link, the access and/or modification time of the link is changed.
190This option implies
191.Fl c .
192.Pp
193.It Fl m
194Change the modification time of the
195.Ar file .
196The access time of the
197.Ar file
198is not changed unless the
199.Fl a
200flag is also specified.
201.Pp
202.It Fl R Ar ref-file
203.It Fl r Ar ref-file
204.It Fl Fl \|reference Ar ref-file
205Use the access and modification times,
206as appropriate for the operation being performed,
207from
208.Ar ref-file
209instead of the current time of day.
210If the
211.Ar ref-file
212is a symbolic link,
213then if the
214.Fl R
215form of this option was used,
216the times are taken from the symbolic link itself,
217otherwise
218the times are taken from the file referenced by it.
219If
220.Ar ref-file
221is not a symbolic link, all three forms are identical.
222.Pp
223.It Fl t Ar datetime
224Change the access and modification times of the
225.Ar file Ns Pq s
226to the specified
227.Ar datetime.
228The argument
229.Ar datetime
230should be in the form
231.Dq [[CC]YY]MMDDhhmm[.ss]
232where each pair of letters represents exactly 2 decimal digits,
233with the following interpretations:
234.Pp
235.Bl -tag -width Ds -compact -offset indent
236.It Ar CC
237The first two digits of the year (the century).
238.It Ar YY
239The second two digits of the year.
240If
241.Dq YY
242is specified, but
243.Dq CC
244is not, a value for
245.Dq YY
246between 69 and 99 (inclusive) results in a
247.Dq CC
248value of 19.
249Otherwise, a
250.Dq CC
251value of 20 is used.
252.It Ar MM
253The month of the year, from 1 to 12.
254.It Ar DD
255The day of the month, from 1 to 31.
256.It Ar hh
257The hour of the day, from 0 to 23.
258.It Ar mm
259The minute of the hour, from 0 to 59.
260.It Ar ss
261The second of the minute, from 0 to 60 (permitting leap seconds).
262If
263.Ar ss
264is 60 and the resulting time,
265as affected by the
266.Ev TZ
267environment variable,
268does not refer to a leap second,
269the resulting time is one second after a time where
270.Ar ss
271is 59.
272If
273.Ar ss
274is not given a value, it is assumed to be zero, and the
275preceding period
276.Pq Sq \&.
277must be omitted.
278.El
279.Pp
280If the
281.Dq CC
282and
283.Dq YY
284letter pairs are not specified, the values default to the current
285year.
286If the
287.Dq ss
288letter pair
289.Pq and the preceding period
290is not specified, the value defaults to 0.
291As an extension to the standard, any of the
292.Dq MM ,
293.Dq DD ,
294and
295.Dq hh
296fields may also be omitted, defaulting to the current
297time of day,
298but once any one of these letter pairs is given, all
299the following ones
300.Pq except Dq \&.ss
301are required.
302Fields must always be specified as 2 digits, even when
303the value is less than 10.
304Leading zeroes do not cause the value to be treated as octal,
305all conversions use decimal numbers.
306.El
307.Pp
308The
309.Fl d ,
310.Fl R ,
311.Fl r ,
312and
313.Fl t
314options are (nominally) mutually exclusive.
315If more than one of these options is present,
316each will be evaluated, and may cause an error,
317then the result from the last one specified is used.
318.Pp
319The options which specify any part of the time
320.Pq Fl d , Fl R, Fl r , Fl t
321apply to both the access and modification times
322(with
323.Fl r
324and
325.Fl R
326obtaining those values independently from the
327.Ar ref-file ) ,
328though which is actually applied depends upon
329the
330.Fl a
331and
332.Fl m
333options.
334.Sh ENVIRONMENT
335.Bl -tag -width -iTZ
336.It Ev TZ
337The time zone to be used for interpreting the
338.Ar datetime
339argument of the
340.Fl t
341option, and the default zone for the
342.Ar posix-datetime
343or
344.Ar human-datetime
345argument of the
346.Fl d
347option.
348.El
349.Sh EXAMPLES
350.Dl touch -h -r path path
351.Pp
352If
353.Ar path
354is a symbolic link, this will set the symbolic link's
355access and modify timestamps identical to those of the
356file to which it refers.
357If
358.Ar path
359is not a symbolic link,
360this will simply update the
361.Dq inode changed
362time
363.Pq Dq ctime
364of the
365.Ar path
366file to the current time of day.
367.Pp
368.Dl touch -Dh -d human-datetime -t CCYYMMDDhhmm.ss -R file file
369.Pp
370Provided
371.Ar file
372exists, this parses the
373.Ar human-datetime
374and
375.Ar CCYYMMDDhhmm.ss
376arguments,
377verifying that they would be suitable for use with
378.Nm ,
379then does nothing, as the final time specification
380.Pq Fl R
381specifies to take the times from
382.Ar file
383and apply them to
384.Ar file
385itself, changing nothing, which the
386.Fl D
387option then prevents from actually occurring.
388That is, this merely tests that the
389.Ar human-datetime
390and
391.Ar datetime
392arguments to
393.Fl d
394and
395.Fl t
396respectively are valid, and could be used to specify a time.
397Use of both
398.Fl h
399and
400.Fl R
401means this works if
402.Ar file
403is a symbolic link,
404even one which does not reference an existing file,
405as well as if it is some other file type.
406Use of
407.Fl R
408requires that
409.Ar file
410exists,
411though if it does not, and an error is generated for that reason,
412the
413.Fl d
414and
415.Fl t
416arguments would have already been successfully processed.
417.Pp
418.Dl touch -m -d '-1 day' somefile
419.Pp
420Set the modify time for
421.Ar somefile
422to one day (24 hours) earlier than the current time.
423.Sh EXIT STATUS
424.Ex -std
425.Sh COMPATIBILITY
426The obsolescent form of
427.Nm ,
428where a time format is specified as the first argument, is supported.
429When none of the time setting options is specified,
430there are at least two arguments,
431and the first argument is a string of digits
432which is either eight or ten characters in length,
433the first argument is interpreted as a time specification of the form
434.Dq MMDDhhmm[YY]
435and applied to the remaining arguments interpreted as path names.
436.Pp
437The
438.Dq MM ,
439.Dq DD ,
440.Dq hh
441and
442.Dq mm
443letter pairs are treated as their counterparts specified to the
444.Fl t
445option, except that none of these are optional.
446If the
447.Dq YY
448letter pair is present,
449it is interpret the same as
450.Dq YY
451in the
452.Fl t
453option with no
454.Dq CC
455specified, however here it appears last, rather than first.
456There are no equivalents to the
457.Dq CC
458or
459.Dq ss
460fields of
461.Fl t
462and the fractional seconds field is always set to zero.
463.Sh SEE ALSO
464.Xr utimes 2 ,
465.Xr parsedate 3
466.Sh "FUTURE PLANNING"
467Sometime in the middle of the 21st century, the default
468.Dq CC
469used in formats where that information is not present, or
470where those digits are not given, will be altered to
471make low year numbers represent the 22nd century, and high
472years the 21st century.
473The boundary between low and high is also expected to change.
474To avoid issues, always use formats which include the
475.Dq CC
476field, and always use it when
477.Dq YY
478is given.
479.Sh STANDARDS
480The
481.Nm
482utility is expected to be a superset of the
483.St -p1003.2
484and
485.St -p1003.1-2008
486specifications.
487.Sh HISTORY
488A
489.Nm
490utility appeared in
491.At v7 .
492