Big Floppy Donkey Disk

Tuesday, September 18, 2012

Recursively chmod only files or directories

Hi Floppers,

I wrote a little script a while back that allows you to recursively change permissions on a directory tree in unix, specifying permissions for either directories or files or both.

From the documentation:
 
# Takes a path to recurse through and options for specifying directory and/or 
# file permissions.
# Outputs a list of affected directories and files.
# 
# If no options are specified, it recursively resets all directory and file
# permissions to the default for most OSs (dirs: 755, files: 644).

Check out chmodr.sh from Gist and feel free to tinker, comment or just use as you wish.

10 comments:

  1. This is VERY useful for VERY lazy people like me. Thanks!

    ReplyDelete
  2. Foudn this with google. Unfortunately, the script does not work as intended, as the getopts is executed before the PATH variable is set, so either the usage information needs to change or the PATH variable is set first and a shift operation executed so that getopts finds the options.

    ReplyDelete
  3. Also, I am wondering if there is an advantage to replacing
    find $ROOT -type d | xargs chmod -v $DIRPERMS
    by
    find $ROOT -type d -exec chmod $DIRPERMS {} \;
    Does this avoid any command line string length limitations of the shell?

    ReplyDelete
  4. a space in file/directory name causes an error. Thanks for the script, it will make my life easier.

    ReplyDelete
  5. this should take care of spaces. Appeared to work.

    # Recursively set directory/file permissions based on the permission variables

    if [ -n "$DIRPERMS" ] ; then

    find $ROOT -type d -print0 | xargs -0 chmod -v $DIRPERMS

    fi

    if [ -n "$FILEPERMS" ] ; then

    find $ROOT -type f -print0 | xargs -0 chmod -v $FILEPERMS

    fi

    ReplyDelete
  6. Sounds good, I updated the gist to include your changes.
    Thanks mate!

    ReplyDelete
  7. usage is wrong order. this works:

    [code]

    #!/bin/sh

    #

    # chmodr.sh

    #

    # author: Francis Byrne

    # date: 2011/02/12

    #

    # Generic Script for recursively setting permissions for directories and files

    # to defined or default permissions using chmod.

    #

    # Takes a path to recurse through and options for specifying directory and/or

    # file permissions.

    # Outputs a list of affected directories and files.

    #

    # If no options are specified, it recursively resets all directory and file

    # permissions to the default for most OSs (dirs: 755, files: 644).

    # Usage message

    usage()

    {

    echo "Usage: $0 -d DIRPERMS -f FILEPERMS PATH"

    echo "Arguments:"

    echo "PATH: path to the root directory you wish to modify permissions for"

    echo "Options:"

    echo " -d DIRPERMS, directory permissions"

    echo " -f FILEPERMS, file permissions"

    exit 1

    }

    # Check if user entered arguments

    if [ $# -lt 1 ] ; then

    "chmodr.sh" 72 lines, 1717 characters[/code]

    also, just found this:http://www.linuxquestions.org/questions/aix-43/chmod-recursion-files-only-208798/



    interesting

    ReplyDelete
  8. I suck. here is the pastebin

    http://pastebin.com/WW4bVMbT

    ReplyDelete
  9. Seems that the download link is down... :/

    ReplyDelete
  10. Thanks mate, I've updated the link. Should be working now.

    ReplyDelete