Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename a file but keeping the orginal creation, modification time in UNIX

Is there any way to rename a file while keeping the original creation / modification/ read time? This is in Solaris.

Thanks in advance.

like image 981
eboni Avatar asked Dec 21 '22 12:12

eboni


2 Answers

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

like image 145
smcphill Avatar answered Dec 27 '22 07:12

smcphill


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

like image 44
sehe Avatar answered Dec 27 '22 07:12

sehe