简体   繁体   English

如何在Linux中设置文件时间范围?

[英]How do I set file timesamps in Linux?

I need to update file timestamps (namely st_ctime , st_atime , st_mtime ) of an existing file in a C program. 我需要更新C程序中现有文件的文件时间戳(即st_ctimest_atimest_mtime )。

On Win32, I could use SetFileTime to update the timestamps. 在Win32上,我可以使用SetFileTime来更新时间戳。

How do I achieve the same on Linux? 我如何在Linux上实现相同的目标?


Edit 编辑
I know touch but since I've put a C tag to the question, I hoped that I could do it with a system call instead of calling an external command... 我知道touch但因为我已经在问题中加了一个C标签,我希望我可以通过系统调用而不是调用外部命令来完成它...

You are looking for utimensat or futimens : 您正在寻找utimensatfutimens

int utimensat(int dirfd, const char *pathname,
    const struct timespec times[2], int flags);

int futimens(int fd, const struct timespec times[2]);

utimensat, futimens - change file timestamps with nanosecond precision utimensat,futimens - 以纳秒精度更改文件时间戳

I'm not sure you can change the creation time of a file but you can use the utime function to change the access and modified times. 我不确定您是否可以更改文件的创建时间,但您可以使用utime函数来更改访问权限和修改时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM