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.

  • 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 – Edward Torvalds 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 ? – Edward Torvalds Apr 12 '15 at 10:17
  • 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
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.

| improve this answer | |
  • 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
  • 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
  • 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
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.