touch (修改文件时间戳)

rose1 发表于 2020-07-29 17:04
浏览次数:
在手机上阅读

在类似Unix的操作系统上,touch命令会修改文件时间戳。如果该文件不存在,则会创建一个具有该名称的空文件。 本文档介绍了GNU / Linux touch版本。

查看英文版

目录

1 touch 运行系统环境

2 touch 说明

3 touch 语法

4 touch 例子

touch 运行系统环境

Linux

touch 说明

时间戳是与文件相关联的信息,用于标识文件历史记录中的重要时间。一个文件可以具有多个时间戳,可以通过手动设置其中一些来“伪造”它们。在内部,操作系统将这些时间存储为自称为epoch的任意日期以来经过的时间。对于类似Unix的操作系统,纪元为1970 年1月1日(星期四)00:00:00协调世界时(UTC)。

在Linux中,与文件关联的三个时间戳记:

时间戳类型 描述 缩写
访问时间 上次读取文件的时间。 atime
修改时间 上次修改文件内容的时间。 mtime
变更时间 上次更改文件的元数据(称为状态)。状态信息包括文件的权限及其时间戳。

每次文件发生任何事情,其状态至少都会改变一个元素,并且其ctime会设置为当前系统时间。
ctime

atime和mtime是文件状态元数据的一部分。因此,当您更改文件的atime(-a)或mtime(-m)时,其ctime会自动设置为当前时间。

无法手动设置ctime。

如果用户拥有文件,则可以将文件的atime或mtime设置为将来或过去。

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 Coordinated Universal Time (UTC), 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 contents of the file 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.

查看英文版

查看中文版

touch 语法

touch [[-a] [-m] | [--time=timetype] [...]] [[-d datestring] | [-t timestamp]]
      [-c] [-f] [-h] [-r reffile] file [file ...]
touch --version
touch --help

唯一需要的参数,以触摸为一个文件名:

文件 时间应更改的文件。如果文件不存在,除非使用-c或-h选项,否则将创建该文件。

可以将多个文件指定为file0 file1 file2 ...等。

(如果文件是破折号“ - ”,请触摸以修改特殊文件描述符标准输出。请参见示例。)

如果没有任何选项,touch会将文件的atime,mtime和ctime 更改为当前系统时间。

选件

选项 描述
-a 仅设置访问时间。
-c,
--no-create
不要创建文件。
-d datestring,
--date=datestring
解析日期字符串 datestring,并使用它代替当前时间。-d选项接受对date命令有效的字符串。

参见示例。

除了具有写访问权之外,用户还必须拥有文件才能将其时间设置为过去或将来。
-f 该选项不执行任何操作,但可以接受以提供与BSD版本的touch的兼容性。
-h,
--no-dereference
如果file是符号链接,并且指定了此选项,那么touch将修改符号链接的时间戳,而不是其引用文件的时间戳。如果未指定此选项,则在进行修改之前,touch将取消引用符号链接。

此选项表示-c:如果文件不存在,则不会创建任何内容。
-m 仅设置修改时间。
-r=reffile,
--reference=reffile
将文件时间设置为文件reffile的时间,而不是当前时间。

除了具有写访问权之外,用户还必须拥有文件才能将其时间设置为过去或将来。
-t timestamp 使用数字时间戳而不是当前时间。时间戳的格式为[[ CC ] YY ] MMDDhhmm [ .ss ]。

参见示例。

除了具有写访问权之外,用户还必须拥有文件才能将其时间设置为过去或将来。
--time=timetype 指定设置时间类型的另一种方法(与-a和-m一样)。timetype

值必须是以下之一:

atime,
access,
use
设置访问时间。等效于-a。
mtime,
modify
设置修改时间。等效于-m。
可以无损地指定两次该选项。例如,-- time = atime --time = mtime与-am相同。
--help 显示帮助消息,然后退出。
--version 显示版本信息,然后退出。

笔记

该-d选项接受一个人类可读的日期字符串。例如,“ 7月4日”,“ 7月4日”,“ 0:00”或“ 7月4日00:00:00”。如果省略年,月或日,则使用当前系统时间值。如果省略时间,则使用午夜。可以在字符串中的月份之前或之后指定日期。根据喜好,一位数字可以加零或不加零。如果指定秒,它们是在由结肠的时间的前面(:)。

-t采用数字时间戳,该时间戳将月份,日期,小时和分钟表示为MMDDHHMM。例如,07040000将在7月4日午夜。世纪,年和秒是可选的,可以指定为CCYYMMDDHHMM.SS。如果指定了秒数,则必须在它们前面加上句点(。)。

可以指定将来的时间。例如,-d“ Jan 1 2029”。除了具有写访问权之外,用户还必须拥有文件才能将其时间设置为过去或将来。

由于无法手动设置ctimes,因此-r,-d和-t选项只能修改atimes和mtimes。触摸文件时,ctime将始终设置为当前系统时间。

时区

如果设置了环境变量 TZ的值,则所有操作都将使用该时区。否则,将使用系统默认时区。

要设置TZ环境变量,请使用命令tzselect。

退出状态

如果所有操作成功,触摸的退出状态为零。任何非零值表示失败。

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 will change 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 will modify 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:

atime,
access,
use
Set access time. Equivalent to -a.
mtime,
modify
Set modification time. Equivalent to -m.
This option can be specified twice nondestructively. For example, --time=atime --time=mtime is the same as -am.
--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 will always be set to the current system time.

Time zones

If the value of environment variable TZ is set, all operations will 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.

查看英文版

查看中文版

touch 例子

touch file.txt

如果存在file.txt,请将其访问,修改和更改时间(atime,mtime和ctime)设置为当前系统时间。如果file.txt不存在,请使用该名称创建一个空文件。

touch -c file.txt

如果存在file.txt,请将其时间设置为当前系统时间。如果不存在,则什么也不做。

touch -a file.txt

更改file.txt的时间。mtime不变。ctime设置为当前系统时间。如果file.txt不存在,则会创建它。

touch -h mysym

更改文件mysym的时间。如果是符号链接,请更改符号链接的时间,而不是引用文件的时间。

touch -cr a.txt b.txt

更改b.txt的访问和修改时间以匹配a.txt的时间。ctime将被设置为当前系统时间。如果file.txt不存在,将不会创建。

touch -ahmcr a.txt b.txt

更改b.txt的atime和mtime 以匹配a.txt的atime和mtime 。如果file1.txt不存在,则不执行任何操作。如果b.txt是符号链接,请设置符号链接的时间。不要触摸引用的文件。

touch --time=atime --no-dereference --time=mtime --no-create --reference=a.txt b.txt

与上一个命令相同。

touch -d "October 31" - > boo.txt

将标准输出的atime和mtime设置为当年10月31日午夜。将标准输出重定向(>)到boo.txt。

touch不能覆盖现有文件的内容,但重定向会。如果boot.txt不存在,将创建它。如果存在boot.txt,它将被覆盖。

touch -t "10310000" - > boo.txt

与上一个命令相同。

例子

touch -d "1 Feb" file1.txt

将file1.txt的atime和mtime设置为当年的2月1日。ctime设置为当前系统时间。

touch -d "February 1" file1.txt

与上一个命令相同。

touch --date="1 February" file1.txt

与上一个命令相同。

touch -d "01:02" file1.txt

今天将file1.txt的atime和mtime设置为1:02 AM。

touch -d "1:2" file1.txt

与上一个命令相同。

touch -md "Sep 1 1927 23:58:59" file1.txt

将file1.txt的mtime设置为1927年9月1日下午11:58和59秒。ctime设置为当前系统时间。时间没有改变。

例子

touch --time=01020304 file1.txt

将file1.txt的atime和mtime设置为当年的1月2日,凌晨3:04。ctime设置为当前系统时间。

touch -t 01020304 file1.txt

与上一个命令相同。

touch -t 5006070405 file1.txt

将file1.txt的atime和mtime设置为2050年6月7日上午4:05。ctime设置为当前系统时间。

touch -t 205007080405 file1.txt

与上一个命令相同,但是明确指定了世纪(20)。

touch -at 195003040506.59 file1.txt

将file1.txt的时间设置为1950年3月4日,5:06 AM和59秒。ctime设置为当前系统时间。mtime不变。

使用统计信息检查文件时间

您可以使用stat命令检查文件的时间:

stat file.txt
stat:不能stat'file.txt':没有这样的文件或目录
touch file.txt; stat file.txt
  文件:'file.txt' 
  大小: 0           Blocks: 0          IO Block: 4096   常规空文件
设备: 802h/2050d  索引节点: 668116      链接: 1
访问: (0664/-rw-rw-r--)  Uid: ( 1001/    hope)   Gid: ( 1002/    hope)
访问: 2017-10-25 21:35:17.368254343 -0400
修改: 2017-10-25 21:35:17.368254343 -0400
更改: 2017-10-25 21:35:17.368254343 -0400
 Birth: -
touch -ad "July 12 1895" file.txt; stat file.txt
  文件:'file.txt' 
  大小: 0           Blocks: 0          IO Block: 4096   常规空文件
设备: 802h/2050d  索引节点: 668116      链接: 1
访问: (0664/-rw-rw-r--)  Uid: ( 1001/    hope)   Gid: ( 1002/    hope)
访问: 1895-07-12 00:00:00.000000000 -0500
修改: 2017-10-25 21:35:17.368254343 -0400
更改: 2017-10-25 21:35:55.487636366 -0400
 Birth: -
touch -mt 198307010000 file.txt; stat file.txt
  文件:'file.txt' 
  大小: 0           Blocks: 0          IO Block: 4096   常规空文件
设备: 802h/2050d  索引节点: 668116      链接: 1
访问: (0664/-rw-rw-r--)  Uid: ( 1001/    hope)   Gid: ( 1002/    hope)
访问: 1895-07-12 00:00:00.000000000 -0500
修改: 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.txt 
访问:   1895-07-12 00:00:00.000000000 -0500
修改:   1983-07-01 00:00:00.000000000 -0400
更改:    2017-10-25 21:36:59.654589018 -0400

注意:您可能会在这组示例中注意到,当atime更改为1895年7月12日时,时区也发生了变化。这是因为直到1918年4月1日,北美才颁布了夏令时(DST)。

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 will not be 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 will be 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

Note: 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 Daylight Savings Time (DST) was not enacted in North America until April 1, 1918.

查看英文版

查看中文版

其他命令行

tabs | tac | talk | tail | tcopy | tty | tar | tbl | tcpdump | time | tee | timex | telinit | telnet | test | top | tput | tr | troff | traceroute |

如此好文,分享给朋友