Linux touch command
On Unix-like operating systems, the touch command modifies file timestamps. If the file doesn't exist, an empty file with that name is created.
This page covers the GNU/Linux version of touch.
Description
A timestamp is information associated with a file that identifies an important time in the file's history. A file can have multiple timestamps, and some of them can be "forged" by setting them manually. Internally, the operating system stores these times as time elapsed since an arbitrary date called the epoch. For Unix like operating systems, the epoch is 00:00:00 UTC (Coordinated Universal Time), Thursday, 1 January 1970.
In Linux, there are three timestamps associated with a file:
Timestamp type | Description | Abbreviation |
---|---|---|
Access time | The last time the file was read. | atime |
Modification time | The last time the file's contents were modified. | mtime |
Change time | The last time the file's metadata, called the Status, was changed. Status information includes a file's permissions and its timestamps. Every time anything happens to a file, at least one element of its status changes, and its ctime is set to the current system time. |
ctime |
The atime and mtime are part of a file's status metadata. Therefore, when you change the atime (-a) or mtime (-m) of a file, its ctime is automatically set to the current time.
There is no way to manually set the ctime.
A file's atime or mtime can be set to the future or the past if the user owns the file.
Syntax
touch [[-a] [-m] | [--time=timetype] [...]] [[-d datestring] | [-t timestamp]] [-c] [-f] [-h] [-r reffile] file [file ...]
touch --version
touch --help
The only required argument to touch is a file name:
file | A file whose times should be changed. If file does not exist, it is created, unless the -c or -h options are used. Multiple files may be specified as file0 file1 file2... etc. (If file is a dash "-", touch modifies the special file descriptor standard output. See examples.) |
With no options, touch changes the atime, mtime, and ctime of file to the current system time.
Options
Option | Description | ||||
---|---|---|---|---|---|
-a | Set the access time only. | ||||
-c, --no-create |
Do not create files. | ||||
-d datestring, --date=datestring |
Parse the date string datestring, and use it instead of current time. Strings valid to the date command are accepted by the -d option. See examples. In addition to having write access, the user must also own a file to set its times to the past or future. |
||||
-f | This option does nothing, but is accepted to provide compatibility with BSD versions of touch. | ||||
-h, --no-dereference |
If file is a symbolic link and this option is specified, touch modifies the timestamp of the symlink, rather than its referenced file. If this option is not specified, touch will dereference symlinks before making modifications. This option implies -c: nothing is created if file does not exist. |
||||
-m | Set modification time only. | ||||
-r=reffile, --reference=reffile |
Set the times of file to the times of file reffile instead of the current time. In addition to having write access, the user must also own a file to set its times to the past or future. |
||||
-t timestamp | Use the numeric timestamp instead of the current time. The format of timestamp is [[CC]YY]MMDDhhmm[.ss]. See examples. In addition to having write access, the user must also own a file to set its times to the past or future. |
||||
--time=timetype | An alternate way to specify what type of time to set (as with -a and -m). The value of timetype must be one of the following:
|
||||
--help | Display a help message, and exit. | ||||
--version | Display version information, and exit. |
Notes
The -d option takes a human-readable date string. For example, "July 4", "4 Jul", "0:00", or "Jul 4 2017 00:00:00". If year, month, or day are omitted, the current system time values are used. If the time is omitted, midnight is used. The day may be specified before or after the month in the string. Single digit numbers may be prefixed with a zero, or not, according to preference. If seconds are specified, they are to be preceded in the time by a colon ( : ).
-t takes a numeric timestamp, which expresses the month, date, hour, and minute as MMDDHHMM. For example, 07040000 would be midnight on the fourth of July. Century, years, and seconds are optional, and may be specified as CCYYMMDDHHMM.SS. If seconds are specified, they are to be preceded with a period ( . ).
Specifying times in the future is okay. For example, -d "Jan 1 2029". In addition to having write access, the user must also own a file to set its times to the past or future.
Because there is no way to manually set ctimes, the -r, -d, and -t options can modify only atimes and mtimes. When a file is touched, the ctime is always set to the current system time.
Time zones
If the value of environment variable TZ is set, all operations use that time zone. Otherwise, the system default time zone is used.
To set the TZ environment variable, use the command tzselect.
Exit status
The exit status of touch is zero if all operations were successful. Any nonzero value indicates failure.
Examples
touch file.txt
If file.txt exists, set its access, modification, and change times (atime, mtime, and ctime) to the current system time. If file.txt doesn't exist, create an empty file with that name.
touch -c file.txt
If file.txt exists, set its times to the current system time. If it does not exist, do nothing.
touch -a file.txt
Change the atime of file.txt. The mtime is not changed. The ctime is set to the current system time. If file.txt does not exist, it is created.
touch -h mysym
Change the times of file mysym. If it's a symbolic link, change the times of the symlink, not the times of the referenced file.
touch -cr a.txt b.txt
Change the access and modification times of b.txt to match the times of a.txt. The ctime will be set to the current system time. If file.txt does not exist, it is not created.
touch -ahmcr a.txt b.txt
Change the atime and mtime of b.txt to match the atime and mtime of a.txt. If file1.txt doesn't exist, do nothing. If b.txt is a symlink, set the times of the symlink. Do not touch the referenced file.
touch --time=atime --no-dereference --time=mtime --no-create --reference=a.txt b.txt
Same as the previous command.
touch -d "October 31" - > boo.txt
Set the atime and mtime of standard output to midnight, October 31 of the current year. Redirect (>) standard output to boo.txt.
touch cannot overwrite the contents of an existing file, but the redirect will. If boot.txt does not exist, it is created. If boot.txt exists, it will be overwritten.
touch -t "10310000" - > boo.txt
Same as the previous command.
Examples
touch -d "1 Feb" file1.txt
Set the atime and mtime of file1.txt to February 1st of the current year. The ctime is set to the current system time.
touch -d "February 1" file1.txt
Same as the previous command.
touch --date="1 February" file1.txt
Same as the previous command.
touch -d "01:02" file1.txt
Set the atime and mtime of file1.txt to 1:02 AM, today.
touch -d "1:2" file1.txt
Same as the previous command.
touch -md "Sep 1 1927 23:58:59" file1.txt
Set the mtime of file1.txt to September 1, 1927, 11:58 PM and 59 seconds. The ctime is set to the current system time. The atime is not changed.
Examples
touch --time=01020304 file1.txt
Set the atime and mtime of file1.txt to January 2, 3:04 AM of the current year. The ctime is set to the current system time.
touch -t 01020304 file1.txt
Same as the previous command.
touch -t 5006070405 file1.txt
Set the atime and mtime of file1.txt to June 7, 2050, 4:05 AM. The ctime is set to the current system time.
touch -t 205007080405 file1.txt
Same as the previous command, but explicitly specifying the century (20).
touch -at 195003040506.59 file1.txt
Set the atime of file1.txt to March 4, 1950, 5:06 AM and 59 seconds. The ctime is set to the current system time. The mtime is not changed.
Checking file times with stat
You can check the times of a file using the stat command:
stat file.txt
stat: cannot stat 'file.txt': No such file or directory
touch file.txt; stat file.txt
File: 'file.txt' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 802h/2050d Inode: 668116 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1001/ hope) Gid: ( 1002/ hope) Access: 2017-10-25 21:35:17.368254343 -0400 Modify: 2017-10-25 21:35:17.368254343 -0400 Change: 2017-10-25 21:35:17.368254343 -0400 Birth: -
touch -ad "July 12 1895" file.txt; stat file.txt
File: 'file.txt' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 802h/2050d Inode: 668116 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1001/ hope) Gid: ( 1002/ hope) Access: 1895-07-12 00:00:00.000000000 -0500 Modify: 2017-10-25 21:35:17.368254343 -0400 Change: 2017-10-25 21:35:55.487636366 -0400 Birth: -
touch -mt 198307010000 file.txt; stat file.txt
File: 'file.txt' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 802h/2050d Inode: 668116 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1001/ hope) Gid: ( 1002/ hope) Access: 1895-07-12 00:00:00.000000000 -0500 Modify: 1983-07-01 00:00:00.000000000 -0400 Change: 2017-10-25 21:36:59.654589018 -0400 Birth: -
for formatspec in 'file name:\t%n' 'accessed:\t%x' 'modified:\t%y' 'changed:\t%z'; do stat --printf="$formatspec\n" file.txt; done
file name: file.txt accessed: 1895-07-12 00:00:00.000000000 -0500 modified: 1983-07-01 00:00:00.000000000 -0400 changed: 2017-10-25 21:36:59.654589018 -0400
You may notice in this set of examples that when the atime was changed to July 12, 1895, the time zone changed. That's because DST (Daylight Savings Time) was not enacted in North America until April 1, 1918.
Related commands
date — View or set the current date and time.
stat — Display the status of a file or filesystem.