How to properly recursively chown files (including hidden dirs)

22,338

The following command works for me in Ubuntu. It changed all the files and directories ownership recusively

sudo chown -R someuser:somegroup YourDir
Share:
22,338

Related videos on Youtube

Steven Lu
Author by

Steven Lu

Updated on September 18, 2022

Comments

  • Steven Lu
    Steven Lu almost 2 years

    This conundrum is caused by running git pull from root.

    There are various reasons for me to do so... I want my device to update code when booting up, and rc.local is run by root.

    It all works mostly fine (npm install tends to fail when run by root, but that's a whole 'nother topic), the problem arises when I try to use the git repository with a non-root user afterward... some of the git files have been written by root, so now I can't use it anymore (permission denied).

    So I'd like to recursively chown it back but there doesn't seem to be anything that really works on all of the little git files. I tried the -R flag and ./**/* path.

    My guess is that neither of those techniques descend into dot-directories.

    • jimmij
      jimmij almost 9 years
      What shell are you using? In bash you need to set shopt -s dotglob in order to match hidden files by * or **.
    • Steven Lu
      Steven Lu almost 9 years
      @jimmij good to know, thanks (you can probably put that as an answer)
    • Peter Cordes
      Peter Cordes almost 9 years
      Did you chown -R *, instead of chown -R .?
  • Richard
    Richard over 6 years
    I tried sudo chown -R me:me . and when I ls -al all my files are still owned by root. Why does this not work?
  • vdegenne
    vdegenne about 6 years
    @Richard did you try using an absolute path or ./ ?