简体   繁体   中英

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.

On Win32, I could use SetFileTime to update the timestamps.

How do I achieve the same on 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...

You are looking for utimensat or futimens :

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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