23

I was trying to chown within /opt and for some reason chown jumped up to the parent and chowned everything.

Can anyone suggest why/how this might happen, and how to avoid doing it in future? It's a bit concerning that running a command in a given dir can effectively jump up and run it in root dir.

ubuntu: /opt > sudo chown -R root:www-data .*
chown: changing ownership of '../var/lib/lxcfs/proc/cpuinfo': Operation not permitted
chown: changing ownership of '../var/lib/lxcfs/proc/meminfo': Operation not permitted
chown: changing ownership of '../var/lib/lxcfs/proc/stat': Operation not permitted
chown: changing ownership of '../var/lib/lxcfs/proc/uptime': Operation not permitted
chown: changing ownership of '../var/lib/lxcfs/proc/diskstats': Operation not permitted
chown: changing ownership of '../var/lib/lxcfs/proc/swaps': Operation not permitted
chown: changing ownership of '../var/lib/lxcfs/proc': Operation not permitted
chown: changing ownership of '../var/lib/lxcfs/cgroup/devices': Operation not permitted
chown: changing ownership of '../var/lib/lxcfs/cgroup/blkio': Operation not permitted
chown: changing ownership of '../var/lib/lxcfs/cgroup/hugetlb': Operation not permitted
chown: changing ownership of '../var/lib/lxcfs/cgroup/rdma': Operation not permitted
chown: changing ownership of '../var/lib/lxcfs/cgroup/pids': Operation not permitted
chown: changing ownership of '../var/lib/lxcfs/cgroup/freezer': Operation not permitted
chown: changing ownership of '../var/lib/lxcfs/cgroup/cpuset': Operation not permitted
chown: changing ownership of '../var/lib/lxcfs/cgroup/memory': Operation not permitted
chown: changing ownership of '../var/lib/lxcfs/cgroup/perf_event': Operation not permitted
chown: changing ownership of '../var/lib/lxcfs/cgroup/cpu,cpuacct': Operation not permitted
chown: changing ownership of '../var/lib/lxcfs/cgroup/net_cls,net_prio': Operation not permitted
chown: changing ownership of '../var/lib/lxcfs/cgroup/name=systemd': Operation not permitted
chown: changing ownership of '../var/lib/lxcfs/cgroup/unified': Operation not permitted
chown: changing ownership of '../var/lib/lxcfs/cgroup': Operation not permitted
chown: changing ownership of '../var/lib/lxcfs': No such file or directory
^C
:ubuntu: /opt >
10
  • 2
    I would have done it like this: sudo chown -R root:wwwdata /opt as per the --help dialog... perhaps using that pipe caused some issue??? Jul 24, 2019 at 21:54
  • 13
    .* matches .. (the parent directory, which is /) - see Does “chmod 777 .* -R” chmod parent directories (..)? Jul 24, 2019 at 21:55
  • 7
    @steeldriver that sounds like it should be posted as an answer ;) Jul 24, 2019 at 21:56
  • 2
    So what is the correct way to set permissions on hidden files, which is what I was trying to do? Jul 24, 2019 at 22:00
  • 4
    @JoshuaBesneatte I try to avoid running recursive commands on arguments beginning with / because most keyboards put / fairly close to the Enter key, and it's too easy to accidentally hit Enter before typing the rest of the command. To mitigate this risk, one can either cd to the root directory and omit the leaing /, or start the command with (, which means the command won't be executed until the matching ) is typed, giving the opportunity to hit Ctrl-C and bail out of a bad mistake (such as rm -rf /tmp/foo-install and hitting Enter instead of T). Jul 25, 2019 at 16:23

3 Answers 3

45

The shell glob .* matches .. (the parent directory) in this case unfortunately that's /:

steeldriver@t400s:/opt$ ls .*
.:

..:
bin  boot  cdrom  dev  etc  home  initrd.img  initrd.img.old  lib  lib32  lib64
libx32  lost+found  media  mnt  opt  proc  root  run  sbin  snap  srv  swapfile  sys
tmp  usr  var  vmlinuz  vmlinuz.old

For additional discussion see:

1
  • 6
    This is the correct, and much simpler, answer
    – abligh
    Jul 25, 2019 at 17:58
25

This happened because you used:

sudo chown -R root:www-data .*

when you should have used this instead:

sudo chown -R root:www-data ./*

First, -R is recursive for all directories under the target directory.

Additionally, * will match all files and directories under the current directory. Next, .* will match all files and directories one level above the current directory.

To avoid this in the future, you can use the ls command to verify the path before you execute the chown command like in these examples:

ls -a ./*
ls -a *
ls -a .*
ls -a ../*

Another way to avoid this is to always use the full path to the directory you wish to run a command to.

Here is an example:

sudo chown -R root:www-data /opt/*

Edit:

You can use the following command to chmod all hidden files or directories directly under /opt (assuming the first character after the . that makes them hidden is a letter, a number, a dash or an underscore which should be true for most files).

for i in /opt/.[A-Za-z0-9-_]*; do sudo chmod root:www-data "/opt/$i"; done

You can verify what files this will chmod by running the following command:

ls /opt/.[A-Za-z0-9-_]*

The first part of the command: for i in /opt/.[A-Za-z0-9-_]* says that, for all the results of the glob /opt/.[A-Za-z0-9-_]* assign each result to the variable "i".

The glob here says that the first character must be . and that the next character [A-Za-z0-9-_] must be any character that is A-Z or a-z or any number 0-9 or a - or a _.

This will exclude the results . and .. which represent the current directory and the directory above the current directory and will only include hidden files and directories.

The second part of the command: do sudo chmod root:www-data "/opt/$i" says to run the command for all variables that match the current value of $i.

The third part of the command: done says that I am finished.


Additionally, you used the -R option with chmod and the -R option is recursive and will apply to all directories and files.

When you only only use the chmod command with no options, the command will only apply to the specific file or directory you gave it and will not apply recursively to directories.

17
  • 5
    My intent was to target hidden files. I mistakenly assumed that the syntax used for grepping hidden files as described here stackoverflow.com/questions/10375689/… is generally valid syntax for hidden files. Seems not. Jul 24, 2019 at 22:07
  • 2
    @DukeDougal You shouldn't accept the first answer that comes up right away. It's generally better to wait, say, 24 hours before accepting. In that time other more useful or better written answers may come up that would deserve accepting. You can upvote all answers you think are useful. StackExchange is not (or should not be) about "who answers first" but "who provides the best answer" (both in terms of content and clarity). Jul 25, 2019 at 11:06
  • 11
    Edit is terrible. It suggests to parse ls output and is very slow while answer is to use find. Jul 25, 2019 at 14:22
  • 9
    (1) No wildcard (glob/pattern) is recursive in bash except for **, and even that has to be explicitly enabled.  IMHO, you should be clearer about the role of -R.  (2) People are advised to avoid using plain * because it can match filenames beginning with -, which will then be interpreted as options.  command -- * should protect against that, but I’m not sure all commands honor that convention. … (Cont’d) Jul 25, 2019 at 15:11
  • 6
    (Cont’d) …  (3) *, ./* and even /opt/* fail to find “dot files” (.*) unless the dotglob option is set.  As Joshua Besneatte and ilkkachu say, chown -R /opt and chown -R . are better.  … (Cont’d) Jul 25, 2019 at 15:11
5

Your troubles came because .* matches everything that begins with a dot. The context is the current directory, since this expression does not include a path. So, if there are any hidden files or folders like .git in the current directory, you'll match them. But (as you'll see by running ls -a in that folder), you will also match . and ..

And .., of course, is the parent directory, so chmod -R recursively targeted everything in the parent directory.

2
  • An absolute path like /opt/.* wouldn't help, /opt/.. is the same as .. with CWD = /opt. Jul 26, 2019 at 19:26
  • @Peter: Yes, that is correct: If the expression included a path, that would give the context (the starting point), instead of it being the current directory. The OP meant to use . as the context, but it didn't work out that way due to the missing slash...
    – alexis
    Jul 27, 2019 at 8:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .