Is it possible to create a script to save and restore permissions?

前端 未结 13 1616
逝去的感伤
逝去的感伤 2021-01-30 04:29

I am using a linux system and need to experiment with some permissions on a set of nested files and directories. I wonder if there is not some way to save the permissions for t

13条回答
  •  臣服心动
    2021-01-30 04:59

    Here's an example for easily doing this with a single file. No additional tools, scripts, temp file, etc. are required. You could expand upon this method for working with more files if needed.

    In this specific example, the permissions are saved in a varibale via the stat command. Then, the file is temporarily stripped of any restrictive permissions. Next, something is done with it (that may have failed due to those prior restrictions). Finally, the original permissions are restored.

    file=$1
    saved_permissions=$(sudo stat -c %a $file)
    sudo chmod 777 $file
    # 
    sudo chmod $saved_permissions $file 
    

提交回复
热议问题