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;/threeto 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 ./*;!./cacheI hope my pseudo command lines make sense.
4 Answers
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 threeIn 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 extglobSee: Bash Reference Manual # Pattern Matching
2Make 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 threeor 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` 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` 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:
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