6

I ran chown -R root:root * by mistake in my home folder when I had root privilege (actually I was supposed to do that in other folder :-/) How do I revert back?

This is not duplicate of what it is showing up. I don't have any problem with .gvfs; folders that were affected were Desktop, Documents, Downloads, Music, Pictures, and Videos. By default, shell globbing * does not include hidden files.

JoKeR
  • 6,972
  • 9
  • 43
  • 65
Alex Jones
  • 7,982
  • 9
  • 55
  • 94
  • do you mean chown ...? If all of your files where owned by you, simply sudo chown -R youruser:yourgroup *. You can have to trim manually things afterward. There is really no "undo" for that command. – Rmano Apr 12 '15 at 10:15
  • yes i meant chown, pardon me for the error – Alex Jones Apr 12 '15 at 10:17
  • @Rmano i ran that command in my home folder so sudo chown -R edward:edward * will save to run ? – Alex Jones Apr 12 '15 at 10:17
  • @edwardtorvalds: Yes, that's it, check my answer. – 0x2b3bfa0 Apr 12 '15 at 10:20
  • 1
    Yes. The good thing is that * does not match dot files, and if you have any file not owned by you in your home will be probably a dot file... – Rmano Apr 12 '15 at 10:29
  • 1
    Notice that the command you used would have been much more destructive if issued in a place different from home. Do that on /, or on /var or /etc, and you probably will need to reinstall. – Rmano Apr 12 '15 at 10:31
  • Duplicate - http://askubuntu.com/questions/340316/my-home-folder-is-owned-by-root-how-can-i-fix-this This link addresses .gvfs – Panther Apr 12 '15 at 13:22
  • i dont have problem with .gvfs i dont think its a duplicate – Alex Jones Apr 12 '15 at 14:03
  • It is a duplicate of restoring ownership of /home , including .gvfs . The community will decide if your question should be closed as a duplicate or not, but this is a common problem for a variety of reasons from typos to inappropriate use or configuration of root and/or sudo – Panther Apr 12 '15 at 14:29
  • @bodhi.zazen now see my update, that duplicate does not applies – Alex Jones Apr 12 '15 at 14:43

1 Answers1

16

Run this command:

sudo chown -R ${USER}:$(id -g -n $USER) ~/*
  • sudo: Run the following command as root.
  • chown: Change the owner of a file/folder

    • -R: Recursive (apply that owner to a folder and its content)
    • ${USER}:$(id -gn)

      • ${USER}: A variable that contains your username by default.
      • :: This splits the username from the group.
      • $(id -gn) This returns the group, however it should be same as user.
        • $(): This is a command substitution, all the code in the inner of these tags will be executed, and then this will act as a variable that contains the output of these commands.
        • id: Prints user and group information for the specified USERNAME, or (when USERNAME omitted) for the current user.
        • -gn: (abbreviation of -g -n)
        • -g: Print only the effective group ID.
        • -n: Print the group name instead of the group ID.
    • ~/*: Do all these things on all the contents of the home folder.

0x2b3bfa0
  • 8,780
  • 6
  • 36
  • 55
  • 1
    are you sure? pardan me, I dont want to mess more with my system. – Alex Jones Apr 12 '15 at 10:23
  • This command will change the owner of all files in your home folder to match your user, however I'm editing my answer to explain the command, if do you want a explaination, please wait until I finish – 0x2b3bfa0 Apr 12 '15 at 10:24
  • what does -g and -n stand for? – Alex Jones Apr 12 '15 at 10:35
  • I updated the post. Please clenup comments. – 0x2b3bfa0 Apr 12 '15 at 10:46
  • 4
    @Rmano .gvfs is not owned by root in my case and I refuse to believe without evidence that anything in my home should be owned by root or some other user. If some program requires it, I will file a bug report. You probably ran sudo with some GUI program at some point without using sudo -H or gksudo. – muru Apr 12 '15 at 10:46
  • @muru: I think you have reason, should I edit my code to change the owner of all things? – 0x2b3bfa0 Apr 12 '15 at 10:48
  • 2
    @Helio not in this case, since OP had used *. But more generally, I'd just do sudo chown "$USER:$(id -g $USER)" ~ -R. – muru Apr 12 '15 at 10:50
  • 1
    @muru: If you see the edit history of my answer, you'll see that I edited my answer to put a ~ at the end of the command instead of a *. However I reverted that change because Rmano said me that to revert that change the OP only will need to put a * to exclude dotfiles. – 0x2b3bfa0 Apr 12 '15 at 12:18
  • 1
    I would advise against using variables such as $USER and "~" when running this command as, depending on how you invoke sudo, $USER may be evaluated to root. See http://askubuntu.com/questions/340316/my-home-folder-is-owned-by-root-how-can-i-fix-this for how to address .gvfs – Panther Apr 12 '15 at 13:23
  • 2
    @bodhi.zazen since we are using $USER cannot equal to root, try this sudo echo $USER – Alex Jones Apr 12 '15 at 14:38
  • 2
    Since $USER is a variable it can be evaluated in different ways. By default is the user who invoked sudo, but it can evaluate as root or one may need to invoke this fix as root for a second user. In both scenarios $USER would not work. See https://help.ubuntu.com/community/RootSudo#Special_notes_on_sudo_and_shells and http://www.sudo.ws/sudoers.man.html see the "Command environment" section. Now I know those all may be rare, still some users use those options. – Panther Apr 12 '15 at 14:45
  • 6
    @bodhi.zazen $USER is evaluated by the shell before sudo ever comes into play. The problem arises if you use $USER within a script or sh -c commands, for example. – muru Apr 12 '15 at 14:46
  • 1
    @muru - it depends and can be configured. – Panther Apr 12 '15 at 14:47
  • 5
    @bodhi.zazen no, it doesn't and no, it can't. – muru Apr 12 '15 at 14:48
  • 4
    @bodhi.zazen As muru says, for the command sudo echo $USER, the shell performs parameter expansion on $USER (replacing it with its value) before invoking sudo. sudo never sees the text $USER, it sees what the shell substituted for it, which is the value of the variable USER in the calling shell. Although sudo changes the environment when running commands, that's irrelevant to the behavior of a command like sudo echo $USER, because the literal text $USER is not part of the command sudo runs. – Eliah Kagan Apr 12 '15 at 16:05
  • 1
    @bodhi.zazen You say, "one may need to invoke this fix as root for a second user".  Well, yes, but that would be a different question, and one should expect the answer to be (at least a little bit) different.  For example, ~/* would have to be changed to ~second_user_name/*. – Scott - Слава Україні Apr 12 '15 at 21:28
  • You should do ~, not ~/*, to get your dotfiles as well, unless the original mistake didn't affect them. – Peter Cordes Apr 13 '15 at 00:36