38

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.

48

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

chmod 644 one two three

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

chmod -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

| improve this answer | |
  • The answer is correct but the first two examples have an error. Chown should specify a user, not octal permissions. Chmod should specify octal permissions. In the examples, chown should be changed to chmod or the octal permissions should be changed to a username. For example: chown dennis one two three – MasterOfNone Apr 10 at 16:19
4

Make a list of filenames in ./list-of-filenames-to-change.txt and then run:

chmod g+w `cat list-of-filenames-to-change.txt`

or use the method described by others here:

chmod 644 one two three

or go to town with the first option but manipulate each line in the file (in this example: replace " /" (WITH whitespace before) by " ./"):

chmod g+w `sed 's/ \// .\//' update-writable.txt` 
| improve this answer | |
3

If the files you want to modify are in one directory you can ls them with the needed pattern.

e.g. I need all downloaded .run files in pwd to be changed to executable:

chmod +x `ls *.run`
| improve this answer | |
2

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:
https://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

| improve this answer | |
  • KISS win, Keep it simple and stupid ! no need to complex things that can be still clean and easy. – Al-Mothafar Nov 20 '16 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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