I want to be able to overwrite a certain file (‘file2’) with the contents of the new file (‘file1’) while preserving the original file if the copy operation fails. File 'file1' is owned by me and has 'rw rw rw' permissions. File 'file2' is owned by another user with 'rw rw rw' permissions.
I use the COPYFILE command in MATLAB to copy the contents of 'file1' file into the 'file2' file:
[a,b,c]=copyfile('file1,'file2')
I get an error:
a = 0 b = copy: preserving times for '/usr/otheruser/file2':Operation not permitted b = MATLAB:COPYFILE:OSError
The COPYFILE command returned an error, however, I observe that the contents of the 'file2' file has changed successfully.
I get the same result when I use the COPYFILE command with the 'f' option:
[a,b,c]=copyfile('file','file2','f')
When I use the CP command in Linux with the '-p' option on the same machine I get the same result – the contents of the file are copied but the CP command returns an error.
Best Answer