Setting creation or change timestamps
Using utimes , futimes , futimens , etc., it is possible to set the access and modification timestamps on a file. 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.) Is there a function to set change timestamps? (Where «change» is the attribute modification or ‘ctime’, not modification 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.) 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.) If it’s not possible, what is the reasoning behind it not being so?
@Madhur Ahuja: touch does not have a parameter (at least, my version does not or is not documented to have one) for changing creation or change times. touch will change modification or access times, however.
I see nothing on that page to indicate that touch has the capability to set change timestamps in any way. Note that I’m looking for change timestamps, which are a different beast from modification timestamps. I’ve tried to clarify my post on this point, if this is what is confusing. If it is not, what passage from that page gives you that suggestion?
@Madhur Ahuja: And pardon my unclear post. I will re-read it in a bit, to see if I can improve it once I’ve let myself get unfamiliar with it.
5 Answers 5
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:
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.
@eitan27 AFAIK debugfs will refuse to work if the disk is mounted, given it can harm the data on the disk.
@Eitan: Because you’re changing bytes on disk, not going through the kernel’s VFS cache. So the kernel’s in-memory data could get out of sync with the data structures on disk.
I had a similar issue, and wrote my answer here.
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).
According to http://lists.gnu.org/archive/html/coreutils/2010-08/msg00010.html ctime cannot be faked (at least it’s not intended to be fakeable):
POSIX says that atime and mtime are user-settable to arbitrary times via the utimensat() family of syscalls, but that ctime must unfakeably track the current time of any action that changes a file’s metadata or contents.
If you just need to change a file’s ctime for some testing/debugging, bindfs might 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 .
I’m not experienced in that area; but according to unix.stackexchange.com/a/20464 the various standards don’t require file creation time to be stored. Some filesystems store creation time anyway, and it can be accessed with nonstandard ways.
ctime is not create-time but file attributes’ change-time. For example, the file size is such an attribute. Hence if you modify a file, ctime and mtime get altered. So backup tools can rely on ctime , as it cannot be faked (except if you warp the system time).
The easiest way:
1) change System time 2) copy paste a file on another location.
I tried this on windows 7 and I succeed to change all three timestamps. The stat command on linux shows that all three timestamps are changed.
The script below automates running debugfs . set_inode_field . ctime . in ismail’s answer for many files. It will copy ctime values from files in /media/MYUSER/MYFS/FOO/BAR (recursively) to /media/MYUSER/MYFS2/FOO/BAR , and umount /media/MYUSER/MYFS2 as a side effect. It will work only if the filesystem of /media/MYUSER/MYFS2 is ext2, ext3 or ext4 (because debugfs works only for these filesystems).
mydev2="$(df /media/MYUSER/MYFS2 | perl -ne '$x = $1 if !m@^Filesystem @ and m@([^ ]+) @; END < print "$x\n" >')" cd /media/MYUSER/MYFS find FOO/BAR -type f | perl -ne 'chomp; my @st = lstat($_); if (@st and -f(_)) < s@"@""@g; print "set_inode_field \"/$_\" ctime \@$st[10]\n" >' >/tmp/sif.out sudo umount /media/MYUSER/MYFS2 # Repeat until success. sudo debugfs -w -f /tmp/sif.out /dev/"$mydev2"
It handles filenames with whitespace and special characters correctly.
It works independently of time zones. As a limitation of debugfs, its precision is seconds, it ignores anything smaller (e.g. milliseconds, microseconds, nanoseconds). Depending the version of debugfs used, it may use 32-bit timestamps, thus it works correctly with dates before 2038-01-19.
If the current user doesn’t have enough read permissions for /media/MYUSER/MYFS , then the commands above should be run as root ( sudo bash ).
Linux Change Modification date of files?
I extract .php files in one of my directory and there were many files in there so most of the files replaced ! but I have a problem since the modification date of new files are 23 April 2013 so I want to find all files and folders in this directory that are not 23 April 2013 ! In other way, I want to change all files in this directory that have 23 April 2013 modification date to 30/08/2013 ! How its possible to find and change the files ? Combine FIND and TOUCH function to replace all files modification date.
1 Answer 1
You could cd to the folder containing the PHP files and:
touch -d '30 August 2013' *.php
Or if it has sub folders with php files — search through them recursively:
find /path/to/your/php/ -exec touch -d '30 August 2013' *.php <> \;
the folder ‘php’ in the command above would be included.
If you ONLY need to find/change EXACTLY files modified on 23 April 2013, you can use the -mtime parameter in your find command.
- -mtime +60 means you are looking for a file modified 60 days ago or more.
- -mtime -60 means less than 60 days.
- -mtime 60 If you skip + or — it means exactly 60 days.
So modifying the command above like this:
find /path/to/your/php/ -mtime 127 -exec touch -d '30 August 2013' *.php <> \;
Where 127 is the exact amount of days since 23 April (if my quick head calculation is correct). Else you can change the number to the correct amount of days, or use the + or — as described above if it doesn’t need to be ‘that’ exact.
You can read more about the find commands -mtime parameter here: http://www.cyberciti.biz/faq/howto-finding-files-by-date/
(yes I borrowed 3 lines from there)
How can I change ‘change’ date of file?
You cannot change the ctime by ordinary means. This is by design: the ctime is always updated to the current when you change any of the file’s metadata, and there is no way to impose a different ctime. To change the ctime of a file, you need to do one of the following:
- Set the system time to the ctime you want to impose, then touch the file, then reset the system time.
- Modify the kernel to add an interface to change the ctime.
- Access the disk image directly (e.g. with debugfs ) and twiddle the bits on the disk (don’t do it while the filesystem is mounted).
You have the answer on related SO question pointed by jw013, for extX, on unmounted disk :
# 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
$ NOW=$(date) && date -s «2030-08-15 21:30:11» && touch file.txt && date -s «$NOW»
The ctime of a file is updated when any of the metadata is changed.
$ ls -l x.py -rw-rw-r--. 1 ignacio ignacio 485 Mar 26 2010 x.py $ stat -c %z x.py 2010-03-26 11:57:56.237068175 -0400 $ chown ignacio x.py $ stat -c %z x.py 2012-04-08 15:31:33.682383575 -0400 $ ls -l x.py -rw-rw-r--. 1 ignacio ignacio 485 Mar 26 2010 x.py
evandrix’s answer excerpted in the next line,
NOW=$(date) && date -s «2030-08-15 21:30:11» && touch file.txt && date -s «$NOW»
needs to be improved as described below :
In some systems like mine, date output doesn’t give a valid format to set with date -s
My system bash shell version : GNU bash, version 5.0.3(1)-release (x86_64-pc-linux-gnu)
My system date command version : date (GNU coreutils) 8.30
My system date command output and setting the date with this format is:
# date Tue 21 Jan 2020 01:36:22 PM +03 # date -s "Tue 21 Jan 2020 01:36:22 PM +03" date: invalid date ‘Tue 21 Jan 2020 01:36:22 PM +03’
So it is necessary to improve evandrix answer;
It would be better to assign the NOW variable to the timestamp value
change NOW=$(date) to NOW=$(date +@%s)
NOW=$(date +@%s) && date -s "2030-08-15 21:30:11" && \ touch file.txt && date -s "$NOW"
Adding sudo command for non root user
sudo bash -c 'NOW=$(date +@%s); date -s "2030-08-15 21:30:11"; touch file.txt; date -s "$NOW"'
sudo bash -c 'NOW=$(date +@%s); date -s "$2"; touch "$1"; date -s "$NOW"' -- \ "file.txt" "2030-08-15 21:30:11"
In this way for easy use, the filename and setting date are assigned as arguments at the end of the line.
How can I change the «last modified» (or «last changed») time of files on Linux?
I tried touch -t , but its precision is only one second: when I use stat , I see there are 9 digits all zero after the decimal ( . ):
Access: 2013-10-10 15:12:00.000000000 +0200 Modify: 2013-10-10 15:12:00.000000000 +0200 Change: 2015-11-22 18:39:54.369524868 +0100
How can I change the «last modify» and «last change» time of files to the precision of 9 numbers after the decimal point?
2 Answers 2
touch -m -d '2015-01-01 01:01:01.123456789' file.ext
When you modify a file’s contents, both the mtime and ctime get updated. touch -m will alter both times. When you rename a file or change the permissions, only the ctime is updated.
@glennjackman I mean how to modify the last change time, the command @Patriceleversque gave is for modifying the last modify time only
Oh, I see. Interesting. When you specify a time, the mtime changes to that time and the ctime changes to the current time. Since the ctime really reflects the file’s inode, I guess the OS (or the filesystem) won’t let you set the ctime to a specified time.
The touch command allows you to alter the modification time and/or the access time. You cannot set the change time arbitrarily: that is set to the time when you alter either of the other two times.
The reason for this is that touch relies upon a system call which can do only the combinations noted above. The source-code (in GNU coreutils) uses this chunk:
ok = (fdutimensat (fd, AT_FDCWD, (fd == STDOUT_FILENO ? NULL : file), t, (no_dereference && fd == -1) ? AT_SYMLINK_NOFOLLOW : 0) == 0);
which in turn (see source) calls futimens or utimensat . These functions are both POSIX, which notes
Upon completion, futimens() and utimensat() shall mark the last file status change timestamp for update.
The status change timestamp is what you cannot set arbitrarily.
The two POSIX functions accept timespec parameters, which provide resolution in nanoseconds. Your computer’s filesystem may or may not support that (though the result from stat indicates that it may).
The default POSIX-style -t option is as noted limited to 1-second resolution. However (still POSIX) the -d option provides for fractions of a second:
GNU coreutils supports this -d option, allowing nanosecond resolution. The documentation for touch gives as an example
--date="2004-02-27 14:19:13.489392193 +0530"
(where —date is a long name equivalent to -d ).