Is there any way to rename a file while keeping the original creation / modification/ read time? This is in Solaris.
Thanks in advance.
Is there any way to rename a file while keeping the original 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.
touch(1)
to alter them back. –
Jobe 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"
Ahoy!
It really seems like there should be an easier solution for this problem. I wanted to do some autonumbering for files in a directory, and the only solution I came up with to preserve timestamps was to use rsync -aHv to copy everything to a different directory with the new name, then erase the old directory and replace it with the new one. It worked but ugh...
I did that for the first couple hundred files but I needed something a little cleaner and easier. Then I stumbled across the rename command in a thread here
Perl Rename command to add number sequence to folder & filenames (Linux)
Larry Wall wrote a rename command in Perl! How cool! The only caveat is that it doesnt work with an input and output file like cp or mv, it works with regular expressions. Therefore instead of
mv oldfile newfile
you would have to do something like this
rename -n "s/oldfile/newfile/" *
I think its because this command is more for a bunch of files instead of a single file, though it will work with one file just not like cp. I had a list of files that I wanted to autonumber with zero padding and a timestamp. Rename was an incredibly elegant solution and I used a command like this
ls -rt | rename -n 'BEGIN{$a="001"} s/^/blah - $a - /;$a++'
This will rename all files in a directory from "name" to "header - 001 - name" in ascending order (first file first, last file last) or FIFO. Really excellent solution as the first way would have taken hours to copy all the directories, and duplicate all the data, while this way takes less than a minute even for a huge amount of files. All timestamps preserved. Only change rename -n to rename -v when you are done testing.
I am using ubuntu linux, and I installed it using
sudo apt install rename
But apparently if you have perl installed, it comes with this command named as prename for perl rename. So as long as your Solaris system has Perl, or you can install it, it should work using prename. Here is a tutorial for the command
https://loopedline.com/post/renaming-files-with-rename-command-the-perl-version/
It is basically the same syntax as Perl. If Perl is not installed perhaps you can install it as a user and use it that way. Here is a post talking about how to do this on Solaris but I cant confirm that it works.
Steps to install perl version 5.26.3 in Solaris 11 Host
When I use Solaris, Perl is usually already installed but I know this is not always the case. If it is installed but prename is not present, you can install it from cpan using
perl -MCPAN -e 'install File::Rename'
Also you might need a tutorial on using basic regular expressions, specifically search and replace. Here is one
https://perldoc.perl.org/perlretut
Good Luck!
© 2022 - 2025 — McMap. All rights reserved.