Changing ownership of folder not working!
Solution 1:
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 makeschown
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:
- BashGuide/CommandsAndArguments
- BashGuide/Parameters
man chown
Solution 2:
Please use $USER
instead of $user
.
sudo chown $USER -R /usr/bin/.folder/
Solution 3:
The user
variable is probably empty. Try $USER
.