The Tilde in Unix

The tilde (~) character in the Unix shell can be confusing. Let's start with the following puzzle. Try to figure out what the following command means:

cp ~cs304/foo ~/cs304/

Note that this puzzle is not some esoteric thing divorced from the real world. In fact, it's an operation that we will do many times in CS 304. So, understanding this operation is important.

Try to phrase it as an English sentence. Click on the following when you think you've figured it out, but don't click too soon. Take a little time to think.

Copy the foo file from the CS 304 course account to my cs304 folder.

One hard part about that solution is the pronoun my, the meaning of which depends on who says it. (Linguists call this a deictic expression or maybe an indexical expression; I'm not a linguist.)

The cp command takes two arguments: the source and the destination. Let's discuss them one at a time.

The Source Argument

The first argument (the source, or, in other words, the file to be copied) to the cp command above is this pathname:

~cs304/foo

That's an absolute pathname, which means that it refers to the same file regardless of where you are (your current directory) when you type it.

The tilde is followed by the word cs304 so, as we learned, the meaning of ~cs304 is "the home directory of the user named cs304". In other words, our course directory. (You'll notice that the URL for this web page includes ~cs304, since the web page is in the CS 304 course account.)

It happens that the home directory of the CS 304 course account is /home/cs304/ so you could type that instead if you want: /home/cs304/foo, but most Unix people would type ~cs304, hence ~cs304/foo

So, the file to be copied is the file named foo in the CS 304 course account.

The Destination Argument

The second argument (the destination, or, in other words, the place to copy the file to) of the cp command above is this pathname:

~/cs304/

That's also an absolute pathname, which means that it refers to the same place regardless of where you are (your current directory) when you type it.

What does it refer to? Because the tilde is not followed by a name, it means "the home directory of the person typing the command". Or, more concisely, "my home directory".

If the command were being typed by Georgia Dome, whose account is gdome, then ~ means Georgia's home directory, which happens to be /students/gdome/

However, if the command were being typed by Wendy Wellesley, whose account might be wwellesl, then ~ means Wendy's home directory, which might be /students/wwellesl/.

Thus, the meaning of the second part of the command depends on who types it. But since the word "my" in English is similarly dependent on who says it, we can describe the tilde part of the argument as "my home directory".

But the whole argument is ~/cs304/; what about the rest of it?

The slash is there to separate the ~ folder from the subfolder cs304. So, the context or assumption of this command is that there is a folder, in your home directory, called cs304.

(We created that folder on the first day of class, so this assumption should be true.)

Thus, we can describe this second argument as "my cs304 folder".

Conclusion

What's the point? When I'm giving instructions, asking each of you to copy a particular file from the course account to your personal account, the most natural way to phrase that command is exactly what we saw above:

cp ~cs304/foo ~/cs304/

Hopefully, that command is a little less puzzling now.

Note that part of the confusion comes from a coincidence of names. If the CS 304 course account were named cs304course and your folder was called mycs304stuff, then the command becomes:

cp ~cs304course/foo ~/mycs304stuff/

That may be a little less confusing, but it's a lot more to type and it still has two different uses of the tilde.