10

I'm a newbie to Ubuntu/Linux world, so don't be hard on me.

I'm using Ubuntu on Windows with WSL2 and the bash shell. Sometimes I want to copy files from Windows to Ubuntu or the other way around. I found some tutorials online on how to do this, and the simplest way is to navigate to /mnt/c/Users/<your_user>.

I want to create a variable like tilde ~ that would mean my Windows user's home directory. I thought about using double tilde ~~ since I saw that it isn't reserved or something.

I created an executable file in /bin named ~~ which would echo the necessary path. But that is cumbersome to use.

Is there a way to create a special character like ~? If not is there a better way to do this?

The end result I'd like is to be able to do something like:

cp ~~/Desktop/somefile.txt ~/somefile.txt
1
  • 1
    For even more fun ... Now that muru edited the title to reference the ~~, there's a related question that shows up here with even more suggestions on how to do this! Commented Jul 13, 2021 at 18:11

4 Answers 4

13

In your case, you can likely use a shell variable instead. You can call it $WH e.g. which is still reasonably short:

~$ export WH=/mnt/c/Users/your_user

This will set the variable WH to your Windows home directory and export it as environment variable. You can then use this variable as in

~$ cp $WH/Desktop/somefile.txt ~/somefile.txt

To make this setting permanent, you can add this variable specification to your .bashrc file.

Note that if the path to your Windows home directory contains spaces, you have to quote the path in the variable definition and in any later use:

export WH="/mnt/c/Users/your user"

in your .bashrc and

~$ cp "$WH"/Desktop/somefile.txt ~/somefile.txt

when using on the console.

11

My solution under WSL is to create a symlink in my home directory to my Windows profile. I personally use:

ln -s /mnt/c/Users/<username> ~/winhome

But you could shorten it. Under bash, at least ~~ appears to work as the symlink name as well, but I'm worried that in certain cases it could be misinterpreted. If you want to try that route:

ln -s /mnt/c/Users/<username> ~/~~

Then accessing it becomes something like:

cp ~/myfile ~/~~/Documents/
10

I know the question is tagged bash, but if you use zsh, you can do this:

hash -d w='/mnt/c/Users/<your_user>'

With that, ~w would expand to that path. This is a "static named directory". It's like AdminBee's suggestion using variable expansion, but one difference is that tab completion would work for expanding paths under ~w/. Also, if you're under that directory and your prompt is set to display your current directory, it would show the short version with the named directory rather than the expanded full path.

~ is an invalid character for this feature, so ~~ can't be used.

9

Also, for a method of @JoL's answer that works on most shells (at least Bash, Fish, Zsh, and even Dash), you can simply create a new user named w with a home directory pointing to your Windows' user profile. Then ~w/ becomes the shortcut. It also appears to be possible to use ~ for the username, resulting in a ~~ shortcut. See below for details.

There's really no downside to creating a user for this purpose on WSL, since there's no concept of user-level security (i.e. wsl -u root already gives you full access to everything in a WSL instance without a password).

A simple ...

sudo useradd --home-dir /mnt/c/Users/<username> w

... will work for WSL. However, "best practice" would probably be something more like:

sudo useradd --no-user-group --non-unique -u 1000 -g 1000 --shell /sbin/nologin -f0 -e0 --home-dir /mnt/c/Users/<username> w

This would create a user w with the same user id and group id (1000:1000) as the primary WSL user (just a personal preference on my part to avoid polluting the system with unnecessary uids/gids). That user would have the nologin shell, an expired password, and a locked account. Again, not that this extra "security" is really needed on WSL anyway.

As with JoL's answer, this will allow:

cp ~/myfile ~w/Documents/

And with the caveat that this could cause problems in some corner cases, you can use useradd's --badname option to force it to accept ~ as a username:

sudo useradd --badname --home-dir /mnt/c/Users/<username> "~" # or ...
sudo useradd --badname --no-user-group --non-unique -u 1000 -g 1000 --shell /sbin/nologin -f0 -e0 --home-dir /mnt/c/Users/<username> "~"

Resulting in your original request for:

cp ~/myfile ~~/Documents

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