Rename a file but keeping the orginal creation, modification time in UNIX
Is there any way to rename a file while keeping the ori开发者_如何学Pythonginal creation / modification/ read time? This is in Solaris.
Thanks in advance.
I don't think you can do that with mv
. However, you can with cp -p
; copy the file to a new name, then delete the original. The -p
flag preserves timestamps.
You will get a new inode though... something you wouldn't with mv
In a variation on the theme suggested by others:
cp -al "$oldname" "$newname"
unlink "$oldname"
should avoid any copying as long as $oldname and $newname are on the same mountpoint (filesystem).
You're in luck.
Solaris (with ZFS) is one of the very few filesystems that actually honour a creation time property for files.
Now on topic: No you cannot preserve all times: the inode will change and the filename changes. This means that the inode ctime will change by (POSIX) definition.
Your last accessed time will also change, unless you're running a noatime mount point (zfs set atime=off).
I don't think there is a way to change that. However, the file creation date time should not be changed at all. I was going to show the commands to show creation times, but unfortunately I don't have a Solaris box handy and I can't seem to find it. I think your best bet is man ls find stat
.
GL
You can probably use cp -p
and then remove the original.
The touch command can force the file modification time, but I am not sure this works with ZFS. If you are renaming large files this is lower overhead than cp -p. Here is a bash script:
oldFileTime=`find "$1" -maxdepth 0 -printf "%Ty%Tm%Td%TH%TM.%.2TS"`
mv "$1" "$2"
touch -t "$oldFileTime" "$2"
精彩评论