Linux 设置创建或更改时间戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4537291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting creation or change timestamps
提问by Thanatos
Using utimes
, futimes
, futimens
, etc., it is possible to set the access and modification timestamps on a file.
使用utimes
、futimes
、futimens
等,可以设置文件的访问和修改时间戳。
Modification time is the last time the file data changed. Similarly, "ctime" or change time, is the last time attributes on the file, such as permissions, were changed. (Linux/POSIX maintains three timestamps: mtime and ctime, already discussed, and 'atime', or access time.)
修改时间是文件数据最后一次改变的时间。类似地,“ctime”或更改时间是文件的最后一次属性(例如权限)被更改的时间。(Linux/POSIX 维护三个时间戳:mtime 和 ctime,已经讨论过,以及 'atime',或访问时间。)
Is there a function to set change timestamps? (Where "change" is the attribute modification or 'ctime', notmodification time 'mtime'.) (I understand the cyclic nature of wanting to change the change timestamp, but think archiving software - it would be nice to restore a file exactly as it was.)
是否有设置更改时间戳的功能?(其中“更改”是属性修改或'ctime',而不是修改时间'mtime'。)(我理解想要更改更改时间戳的循环性质,但考虑归档软件 - 准确地恢复文件会很好因为它是。)
Are there any functions at all for creation timestamps? (I realize that ext2
does not support this, but I was wondering if Linux did, for those filesystems that do support it.)
是否有任何用于创建时间戳的函数?(我意识到它ext2
不支持这一点,但我想知道 Linux 是否支持,对于那些支持它的文件系统。)
If it's not possible, what is the reasoning behind it not being so?
如果不可能,那么它背后的原因是什么?
回答by ismail
For ext2/3
and possibly for ext4
you can do this with debugfs
tool, assuming you want to change the ctime
of file /tmp/foo
which resides in disk /dev/sda1
we want to set ctime to 201001010101
which means 01 January 2010, time 01:01:
对于ext2/3
并且可能对于ext4
您可以使用debugfs
工具执行此操作,假设您要更改驻留在磁盘中ctime
的文件/tmp/foo
,/dev/sda1
我们希望将 ctime 设置为201001010101
2010 年 1 月 1 日,时间 01:01:
Warning: Disk must be unmounted before this operation
警告:在此操作之前必须卸载磁盘
# Update ctime
debugfs -w -R 'set_inode_field /tmp/foo ctime 201001010101' /dev/sda1
# Drop vm cache so ctime update is reflected
echo 2 > /proc/sys/vm/drop_caches
Information taken from Command Line Kung Fu
blog.
信息取自Command Line Kung Fu
博客。
回答by Eitan
I had a similar issue, and wrote my answer here.
我有一个类似的问题,并在这里写下我的答案。
https://stackoverflow.com/a/17066309/391040
https://stackoverflow.com/a/17066309/391040
There are essentially two options:
基本上有两种选择:
- Slight change in kernel (code included in link)
- Change the system clock to the desired ctime, touch the file, then restore current time. (shell script for that included in link).
- 内核的细微变化(链接中包含代码)
- 将系统时钟更改为所需的 ctime,触摸该文件,然后恢复当前时间。(链接中包含的 shell 脚本)。
回答by oliver
According to http://lists.gnu.org/archive/html/coreutils/2010-08/msg00010.htmlctime cannot be faked (at least it's not intended to be fakeable):
根据http://lists.gnu.org/archive/html/coreutils/2010-08/msg00010.htmlctime 不能伪造(至少它不打算伪造):
POSIX says that atime and mtime are user-settable to arbitrary times via the utimensat() family of syscalls, but that ctime mustunfakeably track the current time of any action that changes a file's metadata or contents.
POSIX 说 atime 和 mtime 是用户可以通过 utimensat() 系列系统调用设置为任意时间的,但是 ctime必须不可伪造地跟踪更改文件元数据或内容的任何操作的当前时间。
If you just need to change a file's ctime for some testing/debugging, bindfsmight be helpful. It's a FUSE filesystem which mounts one directory into another place, and can do some transformation on the file attributes. With option --ctime-from-mtime
the ctime of each file is the same as its mtime, which you can set with touch -t
.
如果您只需要更改文件的 ctime 以进行某些测试/调试,则bindfs可能会有所帮助。它是一个 FUSE 文件系统,它将一个目录挂载到另一个地方,并且可以对文件属性进行一些转换。使用选项--ctime-from-mtime
每个文件的 ctime 与其 mtime 相同,您可以使用touch -t
.
回答by SteP
The easiest way:
最简单的方法:
1) change System time
2) copy paste a file on another location.
I tried this on windows 7and I succeed to change all three timestamps. The stat command on linux shows that all three timestamps are changed.
我在Windows 7上试过这个,我成功地改变了所有三个时间戳。linux 上的 stat 命令显示所有三个时间戳都已更改。