roger34
roger34

Reputation: 275

How to chmod of subdirectories of a provided path (BASH)

I need a script that takes a single command line argument that is a directory path. The script should check the argument to determine if it is in fact a directory. If it is a directory, then the script should change the protection mode of any subdirectories located in it to 600. If the argument is not a directory, then an appropriate message should be printed out.

I have

if [ -d $1 ] ; then


else

echo "This is not a directory"

fi

Basically I don't know what to put on the blank line. I was fooling around with chmod but my line seemed to want to change the inputted path and not just the subdirectories.

Upvotes: 1

Views: 1032

Answers (2)

nicerobot
nicerobot

Reputation: 9235

([ -d "$1" ] && find $1 -type d -mindepth 1 | xargs chmod 600 | true) || echo 'Not a directory'

Upvotes: 0

ephemient
ephemient

Reputation: 204828

if test -d "$1"; then
    find "$1" -type d -exec chmod 600 '{}' \;
else
    echo "Not a directory: $1" >&2
    exit 1
fi

Various variants may be faster, but depend on features not in ancient find or xargs.

find "$1" -type d -exec chmod 600 '{}' +
find "$1" -type d -print0 | xargs -0r chmod 600

Upvotes: 8

Related Questions