Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to write a .sh script to setup some folder permissions, but was wonder if I can run something like:

chown -644 ./one;/two;/three

to chown a list of folders, rather than calling chown multiple times.

Also, would there be a way to chown a list of files, and exclude some others, like for example:

chown -R -664 ./*;!./cache

I hope my pseudo command lines make sense.

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

Almost all Unix tools accept a list of files as non-option arguments. As usual, the different arguments have to be separated by space:

chown 644 one two three

In your second example, if you're using Bash, a simple

chown -R 644 !(cache)

will be enough.

This approach requires extended pattern matching. If it's disabled, you can enable it with

shopt -s extglob

See: Bash Reference Manual # Pattern Matching

share|improve this answer
add comment

regex for the win!

using find is the best way I can think of outside of using the regex that may already exist in your particular shell or within the app you are using itself. first, I tried:

touch one&touch two &touch three&find -name "one|two|three" -exec chown -644 {} \;

But, you'll find that the pipe doesn't work in this case. Another sad thing to learn...like learning chmod,chown,chgrp,et. al. doesn't support multi-file/regex selection/exclusion itself...
The solution I found:
http://stackoverflow.com/questions/19111067/regex-match-either-string-in-linux-find-command

find \( -name one -o -name two -o -name three \) -exec chown -644 {} \;

so, not so much regex for the win, but at least we have a way to inject a list of files into the args of a program in a one liner.

you'll note that you need to escape the () meta-characters, and the addition of the -o parameter for each additional name...

other links and content from me which might interest you along your travels:
Various tidbits - notes from korn bourne and friends. - Dave Horner's Website

Cheers.
--dave

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.