8

I am trying to change the ownership of a folder and subfolders by typing either of these:

sudo chown $user -R /usr/bin/.folder/
sudo chown $user -R /usr/bin/folder/
sudo chown $user -R /usr/bin/.folder
sudo chown $user -R /usr/bin/folder

But I get the output:

chown: missing operand after '/usr/bin/.folder/'

What am I doing wrong?

3 Answers 3

11

Why Lower-Case $user Doesn't Work

To clarify and expand upon what others have said, shell and environment variables are case-sensitive. user and USER are different variables, so their contents (which $user and $USER expand to, respectively) may be different. Typically, no variable called user is defined and $user is expanded to nothing, while what you want is $USER which would expand to your username.

The echo command is usually suitable for checking the value of a variable:

$ echo "$user"

$ echo "$USER"
ek

For variables whose values don't contain any blank spaces, the quotes may be removed without affecting the output.
So I could've just used echo $USER if I wanted.

Another Option, for chown

You probably know your own username, so you can just use it in place of $USER. If you do this, make sure to omit the $ sign.

For example, since my username is ek, I could use:

sudo chown ek -R /usr/bin/.folder/

Why You Get That Error

You might be wondering why you get the particular error message:

chown: missing operand after '/usr/bin/.folder/'

That message actually is informative, in that when you see a message like that from a command where you expanded variables, it suggests one or more of your expanded variables may have been unintentionally empty or unset.

You ran sudo chown $user -R /usr/bin/.folder/, and since user was probably undefined, $user expanded to nothing and it was equivalent to running:

sudo chown -R /usr/bin/.folder/

The -R option makes chown act on every file in a directory tree, as you probably know. Since $user expanded to nothing, /usr/bin/.folder/ was the first non-option argument to chown, and thus was interpreted as the argument specifying the desired ownership (such as a username). /usr/bin/.folder is not valid syntax for that, but that particular error went unreported because a different error stopped chown first: there were no more arguments, and so there was nothing specified for chown to operate on.

To put it another way, if I ran sudo chown $USER -R /usr/bin/.folder/, it would expand to sudo chown ek -R /usr/bin/.folder/ and chown would interpret its arguments this way:

  • ek: username, since no username argument has been passed yet
  • -R: recursive option (the leading - is what makes chown know it's an option argument)
  • /usr/bin/.folder/: file or directory to change ownership on, since the username was already passed

But if I ran sudo chown $user -R /usr/bin/.folder, it would expand to sudo chown -R /usr/bin/.folder/ and chown would interpret its arguments this way:

  • -R: recursive option
  • /usr/bin/.folder/: username, since no username argument has been passed yet

Further Reading

For more information, you may be interested in:

6

Please use $USER instead of $user.

sudo chown $USER -R /usr/bin/.folder/
2

The user variable is probably empty. Try $USER.

You must log in to answer this question.

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