Check Write Permissions for Folder Hierachcy

While working on a bash script, I wanted to check if I could rm -rf a folder before actually attempting it and flushing the console with tons of write permission error messages.

Upon failing to find what I wanted online, I constructed this beauty:

find . \( -exec test -w {} \; -o \( -exec echo {} \; -quit \) \) | xargs -I {} bash -c "if [ -n "{}" ]; then echo {} is not writeable\!; exit 1; fi"

This will print the name of the item that is not writeable in addition to the non-zero return value needed when used in more complex scripts.

If you don’t want/need the output, you can go with the simpler version:

find . \( -exec test -w {} \; -o \( -exec echo {} \; -quit \) \) | xargs -I {} test -z "{}"

If there is a more simple solution, please let me know.

Leave a Reply

You must be logged in to post a comment.