If you want to protect a folder or a file from accidental deletion, which means you have the permission required to delete it, but just want to avoid accidental operation, you may use chattr on Linux, or icacls on Windows.

Use chattr to protect folders/files from deletion on Linux

chattr is a tool to change file attributes on a Linux file system. Using it requires root permission.

To protect a folder from deletion, renaming, adding files to it, removing files from it and renaming files in it, or to protect a file from deletion, renaming and modifying, set the immutable (i) attribute on it:

chattr +i "name"

To check the immutable (i) attribute, use:

lsattr -d "name"

Using lsattr does not require root permission.

And to revert the change, use:

chattr -i "name"

Use icacls to protect folders from deletion on Windows

icacls is a tool to change file and folder permissions.

To protect a folder from deletion, and its content from deletion and renaming, use:

icacls "foldername" /deny everyone:(OI)(CI)(DE,DC)

(OI) for Object Inherit, (CI) for Container Inherit, DE for Delete, and DC for Delete Child.

To protect a folder from deletion, and its content from deletion, renaming, modifying, and adding new files/folders, use:

icacls "foldername" /deny everyone:(OI)(CI)(DE,DC,WD,AD)

WD for Write Data/add file, and AD for Append Data/add subdirectory.

To check these permissions (Access Control Lists, ACLs), use:

icacls "foldername"

And to revert the change, use:

icacls "foldername" /remove:d everyone

Note: You should use *S-1-1-0 in place of everyone if the system language is not English.

Use icacls to protect files from deletion on Windows

However, if a file is going to be protected, we need to use:

icacls "filename" /deny everyone:(DE,WD,AD)

That is, remove the inheritance specification ((OI)(CI)) which applys to directories only, and (optionally) remove the unnecessary DC (delete child) right.

The command to revert the change for a file is the same as it's for a folder.

icacls "filename" /remove:d everyone

Note: You should use *S-1-1-0 in place of everyone if the system language is not English.

References