chown -R user:user .* changes permissions backwards: is this the correct behaviour?
Solution 1
Remember that the command line is expanded (interpreted) by the shell before being executed
sudo chown -R luca:luca /myfolder/.*
is interpreted first as :
sudo chown -R luca:luca /myfolder/. /myfolder/.. /myfolder/.adobe /myfolder/.bash_history
note the /myfolder/..
in your command line
chown -R luca:luca /myfolder/..
is equivalent to chown -R luca:luca /
that makes the chown running "backwards"
Use echo /myfolder/.*
when you use "*" to verify .
Solution 2
Well. Command line as root is very powerful. Read some of these classics. And yes, .*
matching to ..
is exactly what is intended. Dot is not a special character. It is a convention. By convention, files that start with a dot are hidden from the default view when listing a directory -- nothing less, and nothing more. By convention, the inode leading to the current directory gets the .
name and the inode leading to the parent directory gets the ..
name.
What you should have done was
chown -R luca:luca /myfolder
Did I mention that there is nothing special about the file names that start with a dot? Recursive chown
doesn't think so.
Right now, you might be able to rescue some of the functionality by changing the ownership back to root. In the long run, you will probably have to reinstall the system, though.
As a general rule:
- Avoid working as root.
- If you work as root, read each command twice before hitting Enter.
- If you are unsure about expansion, try it first with a "safe" command (like
echo .*
). - Do not work as root.
- There are many tasks that can be performed safely using a graphical interface (your problem is an example of such a task).
- Did I mention that you should avoid using the root account?
lucacerone
Updated on September 18, 2022Comments
-
lucacerone over 1 year
I got into a lot of trouble when changing the permission of a folder
myfolder
residing in/
. I issued the commandsudo chown -R luca:luca /myfolder/.*
My intention was to change ownership of all the hidden files in
/myfolder
.Unfortunately I realized that also the ownership of
/
was changed, which of course left me with a broken system. I think this happened because..
matches.*
, but still seems weird to me.Is changing the parent directory the correct behaviour or should I file a bug report?
If it was my mistake in using chown, what are the best practices to use to prevent changing the ownership of system folders and files?
-
NickTux almost 11 years
.*
means any file that ends with.
*.
means any file that starts with.
-
Uwe Plonus almost 11 years@NikTh It the other way round!
-
edwin almost 11 yearsWell one best practice might be not modifying
/
directly (creating folders, removing, etc) unless utterly necessary (open a question for what you wanted to achieve). Also is dangerous using wildcards+root privileges because, more often than not, you are not 100% sure what is been affected by the wildcard. -
Lekensteyn almost 11 years
-
Lekensteyn almost 11 years@LucaCerone It is related,
chmod
andchown
are similar commands, both start withch
, contains ano
and have two other similar letters (n
andm
). </literal> Seriously, they are both of the form:[cmd] -R [mode or user] [one or more files]
. Some solutions offered on that question work for your case too, for instance this one from Sean Reifschneider (in the middle). This answer is also applicable to hidden files only by removing the glob pattern for non-hidden files. -
lucacerone almost 11 years@Lekensteyn thanks for the links, (I didn't know about shopt..) I still think though that the purpose of AskUbuntu is provide help to newbies the that can not know that chown and chmod have the same syntax :) (I discovered it today :))
-
Lekensteyn almost 11 years@LucaCerone That link is from Serverfault.com by the way; I find myself sometimes running
chmod 0:0 [file]
instead ofchown 0:0
due to the similarity. Are you wondering what0:0
means? RTFM! :D
-
-
January almost 11 years@Emmanuel: why would you use xargs and find when the -R option is sufficient?
-
lucacerone almost 11 yearsI didn't want to change the ownership of all the files in the folder... only of the hidden files..
-
lucacerone almost 11 years@Emmanuel: thanks! can you expand a bit about the find and xargs?
-
January almost 11 yearsOK, my bad then. In that case,
chown -R luca:luca /myfolder/.[^.]*
I think -
Lekensteyn almost 11 years@LucaCerone I am not sure why you accepted this one, it recursively changes all files in
/myfolder
. Thefind
command is superfluous and breaks with filenames containing whitespace. (if you want to recursively change files,find /myfolder | xargs chown luca:luca
is similar tochown -R luca:luca /myfolder
). -
Emmanuel almost 11 yearsLekensteyn is right I forgot to grep ^.
-
Anish almost 11 years
echo .*
is a good way of trying out the expansion. It'll show you exactly what the shell sees.ls .*
can be a bit more confusing, as it'll go down directories (ls -d .*
might be better, but might as well just useecho .*
) -
Emmanuel almost 11 years@Lekensteyn I changed the find to not match space
-
Emmanuel almost 11 years@January the find command alone lets you check first if you are modifying the files you want, then you can do the find piped into xargs
-
lucacerone almost 11 years@Lekensteyn I voted this because clearly explains how the filenames are expanded... and helped me understand where things went wrong.