Cerin
Cerin

Reputation: 64850

Bash test and execute if directory pattern exists

How do you do an inline test for the existence of a directory pattern?

If a directory pattern exists, then I want to chmod that pattern.

e.g. I'm trying to do the following:

[ -d /usr/local/myproject/*/bin ] && chmod +x /usr/local/myproject/*/bin/*

but this gives me the error "-bash: [: too many arguments".

Upvotes: 3

Views: 3601

Answers (3)

sorpigal
sorpigal

Reputation: 26106

To salvage some usefulness out of my answer, just suppose you had too many bin directories that you couldn't do it yi_H's way.

find /usr/local/myproject/ -path '/usr/local/myproject/*/bin' -maxdepth 2 -type d -exec chmod a+x {} + 2>/dev/null

Upvotes: 1

Karoly Horvath
Karoly Horvath

Reputation: 96306

there's no need to test:

chmod +x /usr/local/myproject/*/bin/* 2>/dev/null

Upvotes: 7

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136495

It doesn't work because -d test takes one argument. You seem to be passing more than one. A fix would be:

for dir in /usr/local/myproject/*/bin; do
    [[ -d $dir ]] && chmod +x $dir/*
done

Upvotes: 2

Related Questions