Setting file time on NAS



I’m writing a program that copies files from one directory to another on NAS drive. I need to set the new file modified time to be the same as the source file using:

    # Create target directory if required
    os.makedirs(targetDir, 777, True)
    shutil.copyfile(sourcePath, targetPath)
    sourceTime = os.path.getmtime(sourcePath)
    os.utime(targetPath, (sourceTime, sourceTime))

The last statement fails with the error message:

    os.utime(targetPath, (sourceTime, sourceTime))
    PermissionError: [Errno 1] Operation not permitted

Is this a parent folder permission problem? How to create the folder/file so that the program can set file time?


Did you try checking if you can make the equivalent change via the terminal?

Yes, I did touch filename and it worked. But touch -m fileName didn’t work, got “Operation not permitted” error.

The PermissionError you encountered is probably due to insufficient permissions to modify the target file or directory. To set the file’s modified time using os.utime, the process needs to have write permissions on the target file.

Check Permissions: Make sure the user running the program has write permissions on the target directory and file. You can use the ls -l command on the command line to check the permissions of the target directory and file. Ensure that the user running the script has write permissions, shown as w for both the target directory and file.

Change ownership or the permissions: If the target directory or file is owned by another user or has restrictive permissions, you can change the ownership or permissions using the chown and chmod commands.To change the ownership of a directory/file, you can use:

sudo chown your_username:your_groupname targetDirTo

Please note, I don’t know why, but I can never get chown right and always need to google it or something, but it is something along this line. Also, don’t try this if you aren’t a system admin, but somehow still have rights to do so.

Change the permissions of the directory/file, you can use:

sudo chmod 777 targetDir

Be careful when using chmod 777, as it provides full read, write, and execute permissions to everyone. It’s generally better to use more restrictive permissions based on your specific use case.

Check the NAS Settings: If the NAS drive has specific settings or restrictions on modifying files, you might need to configure it to allow the user to modify files. Check the NAS settings or consult the NAS documentation for any specific limitations.

Run the Script with Appropriate Privileges: If changing ownership or permissions is not possible, you can try running the script with elevated privileges.

Use sudo before the command to run the script as the root user, which typically has more permissions.

This would be my last resort. The fact that you didn’t try this, might indicate that you don’t know the true power of sudo.

When using sudo, script privileges are elevated, which can be dangerous if the script is not properly written or if it accesses sensitive parts of the system.

Finally, remember that modifying ownership and permissions on system directories or files should be done with caution, as it can lead to security risks if not handled properly.

Got it. All I had to do was to add uid=myuserid,git=mygroupid to the relevant entry in fstab.

Thank you everyone.

1 Like