APLawrence - Information and Resources for Unix and Linux Systems, Bloggers and the self-employed
RSS Feeds Get APLawrence.com by RSS











(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Home > Unix Articles > Recursive chown
Printer Friendly Version




Recursive chown


I had email this morning from someone using "chown" to fix up permissions on a directory. He had discovered "-R" in the man page but had run into a small problem.

Let's say the directory was /usr/fred. He had done:


cd /usr/fred
chown -R fred:group *
 

He noted that had done pretty much what he wanted, but had ignored the "dot" files: .profile, .login etc.

So to fix that, he did:

cd /usr/fred
chown -R fred:group .*
 

That succesfully changed the ownership of the "dot" files, but had an unexpected (to him) side effect: /usr was also changed.

Of course that would be true, because ".*" includes ".." and the ".." of /usr/fred is /usr. A useful command flag seemed to be difficult or impossible to use as desired.

Well, that's not the case. The "-R" is perfectly happy to do the job if you invoke it like this:

cd /usr/fred
chown -R fred:group .
 

See the difference? Just ".", meaning current directory. That will correctly change all fles, including .login, .profile and everything else, but it won't touch ".." and therefore leaves /usr alone.

In this case, the misuse was noticed immediately and fixed, but I have often had panic calls from people where no one can login because of making this same mistake.












Actually, there's a little more to this. How did /usr/fred get the wrong ownership to start with? I looked more closely at the email and saw that "rcp" had been used to copy files from another system. It had been correctly invoked with "-p" and "-r", so the permissions and ownership should have been preserved.

However: rcp can't create users. If, for example, "fred" doesn't yet exist as a user on this new system, rcp can copy Fred's files from aother system (assuming proper access) but can't magically create files owned by Fred if "fred" doesn't exist here.

So the solution is to create all necessary users before using rcp. That would have avoided all of this.


Technorati tags:
If this page was useful to you, please click to help others find it:  

Your +1's can help friends, contacts, and others on the web find the best stuff when they search.

8 comments




More Articles by Anthony Lawrence - Find me on Google+



Click here to add your comments





Fri Feb 27 09:32:01 2009:   Vladimir
http://strixbg.blogspot.com
gravatar
Very helpful. Thank you!





Fri Feb 27 17:50:17 2009:   OnkarJoshi
http://onkarjoshi.wordpress.com/
gravatar
Eeeks. Someone extracted, ran Tomcat, ActiveMQ, Apache httpd as root on one of the servers I am working on inside my normal application users home folders.

Your article served as a nice quick reference for recursive chgrp/chown.

Thanks.
OJ.



Tue Feb 2 21:31:41 2010:   anonymous

gravatar
Hi, why the heck would you put code that you KNOW breaks stuff, and then AFTER the code, you explain that it breaks stuff? That is ridiculous. Thanks a lot. Serves me right for trusting you!



Tue Feb 2 21:51:22 2010:   TonyLawrence

gravatar
Sigh..

So you don't read, you just charge ahead and type something that you don't understand?

And we are supposed to feel what? Sorry for you?

No.



Sat Feb 13 12:06:14 2010:   marytee

gravatar
thanks for the useful information. This should prevent me from getting into trouble--at least as far as permissions go.



Sat Feb 27 10:37:38 2010:   Bijuteriifantezie
http://www.all-bijoux.ro
gravatar
Founded your explanation about chown using google and was exaclty what I need. To modify chown rights for a lot of files and directory. Thank.



Mon May 23 05:57:53 2011:   thomas

gravatar
Thank you, sir. No, I did not learn about the real details and wonders of recursively using chown. Instead, you saved me all of that and gave me some proper code. :]

Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar



Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.


My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!


book graphic unix and linux troubleshooting guide




 I sell and support
 Kerio Mail server
pavatar.jpg

This post tagged:

       - Basics
       - Linux
       - MacOSX
       - Popular
       - Shell
       - Unix




Unix/Linux Consultants

Skills Tests

Guest Post Here