How to check delete / write permissions for a folder hierarchy?

In the script I work, I want rm -rf folder hierarchy. But before doing this, I want to make sure that the process can complete successfully. So this is the approach I came up with:

 #!/bin/bash set -o nounset set -o errexit BASE=$1 # Check if we can delete the target base folder echo -n "Testing write permissions in $BASE..." if ! find $BASE \( -exec test -w {} \; -o \( -exec echo {} \; -quit \) \) | xargs -I {} bash -c "if [ -n "{}" ]; then echo Failed\!; echo {} is not writable\!; exit 1; fi"; then exit 1 fi echo "Succeeded" rm -rf $BASE 

Now I wonder if there could be a better solution (more readable, shorter, more reliable, etc.).

Please note that I am fully aware that there may be changes in file permissions between verification and actual deletion. In my use case, this is acceptable (compared to not making any checks). If there was a way to avoid this, I would like to know how to do it.

+4
source share
1 answer

Do you know that the -perm find switch (if present in your version)?

 find -perm -u+w 
+6
source

Source: https://habr.com/ru/post/1388956/


All Articles