Linux文件时间戳说明:atime,mtime和ctime

A laptop displaying a terminal window with additional text-filled terminal windows in the background.
Fatmawati Achmad Zaenuri/Shutterstock Fatmawati Achmad Zaenuri / Shutterstock

When does “changed” not mean “modified”? When we’re talking about Linux file timestamps. In this guide, we’ll explain how the system updates them, and how to alter them yourself.

什么时候“更改”不表示“修改”? 当我们谈论Linux文件时间戳时。 在本指南中,我们将说明系统如何更新它们以及如何自行更改它们。

atime,mtime和ctime之间的区别 (The Difference Between atime, mtime, and ctime)

Every Linux file has three timestamps: the access timestamp (atime), the modified timestamp (mtime), and the changed timestamp (ctime).

每个Linux文件都有三个时间戳:访问时间戳(atime),修改后的时间戳(mtime)和更改后的时间戳(ctime)。

The access timestamp is the last time a file was read. This means someone used a program to display the contents of the file or read some values from it. Nothing was edited or added to the file. The data was referenced but unchanged.

访问时间戳是最后一次读取文件的时间。 这意味着有人使用程序来显示文件的内容或从中读取一些值。 没有任何编辑或添加到文件。 引用了数据,但未更改。

A modified timestamp signifies the last time the contents of a file were modified. A program or process either edited or manipulated the file. “Modified” means something inside the file was amended or deleted, or new data was added.

修改后的时间戳表示上次修改文件内容的时间。 程序或过程编辑或操纵了文件。 “已修改”表示文件中的某些内容已被修改或删除,或者添加了新数据。

Changed timestamps aren’t referring to changes made to the contents of a file. Rather, it’s the time at which the metadata related to the file was changed. File permission changes, for example, will update the changed timestamp.

更改的时间戳不是指对文件内容所做的更改。 相反,这是与文件相关的元数据被更改的时间。 例如,文件许可权更改将更新更改的时间戳。

The standard ext4 Linux file system also allocates space for a file-creation timestamp in its internal file system structures, but this hasn’t been implemented yet. Sometimes, this timestamp is populated, but you can’t depend on the values in it.

标准ext4 Linux文件系统还在其内部文件系统结构中为文件创建时间戳分配了空间,但这尚未实现。 有时,会填充此时间戳记,但是您不能依赖其中的值。

时间戳的剖析 (The Anatomy of a Timestamp)

Linux timestamps hold a number rather than a date and time. This number is the number of seconds since the Unix epoch, which was midnight (00:00:00) on January 1, 1970, in Coordinated Universal Time (UTC). Leap seconds are ignored in Linux timestamps, so they aren’t analogous to real time.

Linux时间戳记包含数字而不是日期和时间。 此数字是自Unix纪元 (1970年1月1日午夜(00:00:00)的世界协调时间(UTC))以来的秒数。 在Linux时间戳中,秒将被忽略,因此它们与实时不相似。

When Linux needs to display a timestamp, it translates the number of seconds into a date and time. This makes it easier for humans to understand. The location and time zone the computer viewing the file is in guides the conversion of the number of seconds to a date and time. It also ensures the month is in the correct language.

当Linux需要显示时间戳时,它将秒数转换为日期和时间。 这使人类更容易理解。 查看文件的计算机所在的位置和时区指导将秒数转换为日期和时间。 它还可以确保以正确的语言显示月份。

So, how many seconds can be stored in a timestamp? A lot—2,147,483,647, to be precise. That’s a big number, but is it enough? If you add that to the Unix epoch, and then translate it to a date and time, you get Tuesday, January 19, 2038, at 03:14:07 a.m. We’ll need a different scheme for timestamps before then, though.

那么,时间戳可以存储多少秒? 很多-准确地说是2,147,483,647。 这是一个很大的数字,但是足够了吗? 如果将其添加到Unix时代,然后将其转换为日期和时间,则会得到2038年1月19日,星期二,03:14:07 am。在此之前,我们需要一个不同的时间戳方案。

查看时间戳 (Viewing Timestamps)

When you use the -l (long listing) option with ls, as shown below, you can see the modified timestamp:

当对ls使用-l (长列表)选项时,如下所示, 您可以看到修改后的时间戳

ls -l dp.c

If you want to see the access timestamp, use the -lu (access time) option like so:

如果要查看访问时间戳,请使用-lu (访问时间)选项,如下所示:

ls -lu dp.c

And finally, to see the change timestamp, you can use the -lc (change time) option; type the following:

最后,要查看更改时间戳,可以使用-lc (更改时间)选项; 输入以下内容:

ls -lc dp.c
The "ls -l dp.c" commands in a terminal window.

The timestamps above show the file’s contents were last modified on April 21, 2019. The access and changed timestamps are identical because the file was copied from another computer to this one on January 20, 2020, and both timestamps were updated at that time.

上面的时间戳显示了文件的内容最后一次修改是在2019年4月21日。访问和更改的时间戳是相同的,因为文件是在2020年1月20日从另一台计算机复制到这台计算机的,并且两个时间戳都在那时进行了更新。

To see all timestamps simultaneously, use the stat command as follows:

同时查看所有时间戳 ,请使用stat命令,如下所示:

stat dp.c
The "stat dp.c" command in a terminal window.

The time zones are listed at the bottom of the display. As you can see, they have a very accurate, fractional seconds component. At the end of each timestamp, you also see a -0500 or -0400 .

时区在显示屏底部列出。 如您所见,它们具有非常精确的小数秒部分。 在每个时间戳记的末尾,您还会看到-0500-0400

These are time zone offsets. The file system records the timestamps in UTC and converts them to the local time zone when displayed by stat. The computer we used to research this article is configured as if it were in the Eastern Standard Time (EST) zone of the U.S.

这些是时区偏移量 。 文件系统以UTC记录时间戳,并在通过stat显示时将其转换为本地时区。 我们用来研究本文的计算机的配置就好像它位于美国的东部标准时间(EST)区域一样

That time zone is five hours behind UTC when EST is in force. However, it’s four hours behind UTC when Eastern Daylight Time (EDT) is in force. In April 2019, when the modified timestamp was changed, EDT was in effect. That’s why two of the timestamps have a five-hour offset, but the modified has a four-hour offset.

EST生效时,该时区比UTC落后五个小时。 但是,实施美国东部夏令时间(EDT)的时间比UTC晚四个小时。 2019年4月,当修改的时间戳更改时,EDT生效了。 这就是为什么两个时间戳都有五个小时的偏移量,而修改后的时间戳有四个小时的偏移量的原因。

The offsets and time zones aren’t stored anywhere. There’s neither an inode nor a file system space devoted to holding these values. You have to calculate these on the fly using the timestamp (which is always in UTC time), the local time zone of the computer displaying the file, and whether DST was in effect.

偏移量和时区不会存储在任何地方。 没有一个索引节点或一个文件系统空间专门用于保存这些值。 您必须使用时间戳(始终使用UTC时间),计算机在显示文件的本地时区以及DST是否生效的情况下即时计算这些值。

You also see a “Birth” timestamp, which is reserved for the creation date of the file. This is not implemented, and you see a hyphen “-” instead of a timestamp.

您还会看到“出生”时间戳,该时间戳保留用于文件的创建日期。 这没有实现,您会看到连字符“ - ”而不是时间戳。

更改时间戳 (Changing Timestamps)

If you want, you can change the timestamps on a file. You can use the touch command to change the access or modified timestamps, or both:

如果需要,可以更改文件的时间戳。 您可以使用touch命令来更改访问或修改的时间戳, 或同时更改两者:

touch -a dp.c

To set a new access timestamp, you would use the -a (access time) option. This command sets the access timestamp to the computer’s current time:

要设置新的访问时间戳,可以使用-a (访问时间)选项。 此命令将访问时间戳设置为计算机的当前时间:

stat dp.c
The "touch -a dp.c" command in a terminal window.

The access timestamp changed, as expected. However, the changed timestamp was updated as well; this is normal.

访问时间戳已按预期更改。 但是,更改后的时间戳也已更新。 这个是正常的。

To change the modified timestamp, you can use the -m (modified time) option:

要更改修改后的时间戳,可以使用-m (修改后的时间)选项:

touch -m dp.c
stat dp.c
The "touch -m dp.c" command in a terminal window.

This time, the modified and changed timestamps were updated.

这次,修改和更改的时间戳记已更新。

You can use the -d (date) option if you want to change both the access and modified timestamps simultaneously. You can also specify a time and date—you’re not restricted to changing the timestamps to the present.

如果要同时更改访问和修改的时间戳记,则可以使用-d (日期)选项。 您还可以指定时间和日期-您不限于将时间戳记更改为现在。

We’ll use the following command to set the access and modified timestamps to 10:30:45 on January 15, 2020:

我们将使用以下命令在2020年1月15日将访问和修改的时间戳设置为10:30:45:

touch -d "2020-01-15 10:30:45" dp.c
stat dp.c
The "touch -d '2020-01-15 10:30:45' dp.c" command in a terminal window.

We’ve now set the access and modified timestamps to a date in the past. The changed timestamp was also updated to the current time of the computer.

现在,我们将访问权限和修改的时间戳设置为过去的日期。 更改后的时间戳也已更新为计算机的当前时间。

You can also use the -r (reference) option, as shown below, if you want to set the timestamps of one file to the timestamp values of another:

如果要将一个文件的时间戳设置为另一个文件的时间戳值,也可以使用-r (引用)选项,如下所示。

touch dp.c -r dice_words.sl3
stat dp.c
The "touch dp.c -r dice_words.sl3" command in a terminal window.

And then, we’re pretty much back where we started, with a mixture of -0400 and -0500 timestamps.

然后,我们几乎回到了起点,混合了-0400-0500时间戳。

Let’s do something that only affects the changed timestamp. We’ll use the chmod command to give an executable file execute permissions for all users:

让我们做一些只影响更改后的时间戳的操作。 我们将使用chmod命令为所有用户赋予可执行文件执行权限

chmod +x dp
stat dp
The "chmod +x dp" and "stat dp" commands in a terminal window.

The changed timestamp was the only one that updated. This is because the file itself wasn’t changed—it was neither accessed nor modified. However, the metadata about the file was changed.

更改后的时间戳是唯一更新的时间戳。 这是因为文件本身未更改-既未访问也未修改。 但是,有关文件的元数据更改。

文件系统如何更新时间戳 (How the File System Updates Timestamps)

When a file system is mounted, there are options you can use to specify how that file system should operate or be treated. These are stored in the /etc/fstab file, which is read and processed at boot time. You can also set options to dictate the scheme they should use to update the access timestamp.

挂载文件系统时,可以使用一些选项来指定该文件系统应如何操作或处理。 这些文件存储在/etc/fstab文件中,该文件在引导时进行读取和处理。 您还可以设置选项,以规定它们应用来更新访问时间戳的方案。

The following are some of the most common options:

以下是一些最常见的选项:

  • strictatime (strict atime): This option updates the access timestamp of files every time they’re accessed. There’s an overhead attached with this approach, but some servers can benefit from this scheme. It has little merit on a desktop or laptop computer.

    strictatime (strict atime) :每次访问文件时,此选项都会更新文件的访问时间戳。 这种方法带来了额外的开销,但是某些服务器可以从该方案中受益。 它在台式机或笔记本电脑上没有什么价值。

  • noatime (no atime): This option fully disables the access timestamps for files and directories from updating. The modified timestamps, however, will still update.

    noatime (no atime):此选项完全禁止文件和目录的访问时间戳更新。 但是,修改后的时间戳仍将更新。

  • nodiratime (no dir atime): This option enables access timestamps for files to update, but disables it for directories.

    nodiratime(no dir atime):此选项启用文件更新的访问时间戳,但禁用目录。

  • relatime (relative atime): This option updates the access timestamp only if it was more than 24-hours old, or the previous one was older than the current modified or changed timestamps. This strikes a good balance between access timestamps updating too frequently or not updating at all.

    relatime(相对时间):仅当访问时间戳早于24小时或以前的时间戳早于当前修改或更改的时间戳时,此选项才会更新。 这在访问时间戳更新过于频繁或根本不更新之间取得了良好的平衡。

Let’s look at the /etc/fstab file for this computer and see which options are set:

让我们看一下这台计算机/etc/fstab文件 ,看看设置了哪些选项:

less /etc/fstab
The "less /etc/fstab" in a terminal window.

The /etc/fstab file is displayed for us, as shown below.

为我们显示了/etc/fstab文件,如下所示。

The "/etc/fstab" file in less in a terminal window.

Here’s the content of the file without the wrap-around:

这是不包含换行符的文件内容:

# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/sda1 during installation
UUID=4a143d08-8695-475b-8243-b13b56050fc2 / ext4 errors=remount-ro 0 1
/swapfile none swap sw 0 0

There are only two entries, and one of them is a swap file, which we can ignore. The other is being mounted at the root of the filesystem ( / ) and was on device /dev/sda1 at the time of installation. That’s the first partition on the first hard drive, and it happens to contain an ext4 file system.

只有两个条目,其中一个是交换文件,我们可以忽略。 另一个正在安装在文件系统( / )的根目录中,并在安装时位于设备/dev/sda1上。 那是第一个硬盘驱动器上的第一个分区,它恰好包含一个ext4文件系统。

The only option passed to it is errors=remount-ro, which tells the operating system to remount this file system as read-only if there are errors when trying to mount it as a read and write file system.

传递给它的唯一选项是errors=remount-ro ,如果尝试将其挂载为读写文件系统时发生错误,它会告诉操作系统将该文件系统重新挂载为只读。

So, there’s no mention of how the access timestamp will be handled. Let’s dig deeper and check out what /proc/mounts can tell us. We’ll pipe the output from /proc/mounts through grep. Our search string will be “sda”, the hard drive identifier.

因此,没有提及如何处理访问时间戳。 让我们更深入地研究一下/proc/mounts可以告诉我们什么。 我们将通过grep/proc/mounts输出输出。 我们的搜索字符串将为硬盘驱动器标识符“ sda”

We type the following:

我们输入以下内容:

cat /proc/mounts | grep "sda"
The "cat /proc/mounts | grep 'sda'" command in a terminal window.

Now we see the following options:

现在我们看到以下选项:

  • rw: The file system will be mounted as a read and write file system.

    rw :文件系统将作为读写文件系统挂载。

  • relatime: The file system will use the “relative atime” scheme to update the access timestamps.

    relatime :文件系统将使用“相对时间”方案来更新访问时间戳。

Where did that come from? Well, the relatime scheme is used in the following situations:

那个是从哪里来的? 好吧,在以下情况下会使用relatime方案:

  • When the defaults /etc/fstab option is used.

    使用默认的 /etc/fstab选项时。

  • When the relatime /etc/fstab option is used.

    使用relatime /etc/fstab选项时。

  • When no access timestamp options are used in /etc/fstab, and you’re using Linux kernel 2.6.30 or newer.

    如果/etc/fstab没有使用访问时间戳选项,并且您使用的是Linux内核2.6.30或更高版本。

Our /etc/fstab entry for the ext4 file system didn’t specify any access timestamp update options, so Linux made the sensible choice and used relatime.

我们针对ext4文件系统的/etc/fstab条目未指定任何访问时间戳更新选项,因此Linux做出了明智的选择,并使用了relatime

时间戳很重要 (Timestamps Are Important)

Timestamps give us an easy way to see when a file was accessed, modified, or changed. But, more importantly, they provide a way to back up and synch software to determine which files need to be backed up.

时间戳使我们可以轻松地查看文件何时被访问,修改或更改。 但是,更重要的是,它们提供了一种备份和同步软件以确定需要备份哪些文件的方法。

The ability to manipulate timestamps will prove useful whenever you need to forcibly convince a program to include or ignore a file, or set of files.

每当您需要强行说服程序包括或忽略一个文件或一组文件时,操纵时间戳的能力将被证明是有用的。

翻译自: https://www.howtogeek.com/517098/linux-file-timestamps-explained-atime-mtime-and-ctime/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值