What Does cd ~ Do When Entered Into A Terminal Window

cd ~
cd ~.

Ever wonder what the following symbol is?

  • ~

The ~ is called a tilde and originates from the Latin for titulus and according to Wikipedia it came to the English language via the Spanish language. It's meaning is title or superscription.

Within Linux the tilde (~) symbol is what is known as a metacharacter and within the confines of a terminal's shell it has a special meaning.

So what exactly does the following command do:

cd ~

The above command simply takes you back to your home directory. It is a great shortcut. If you have navigated to another folder such as the /var/logs or /mnt etc then typing cd ~ returns you back to your user's home directory.

The tilde (~) does more than that though.

Whilst using the tilde on its own takes you to your current user's home directory you can move to another user's home directory by typing the user's name after the tilde.

For example, if you have a user called Fred on your system then you can move to his home folder by typing the following:

cd ~fred

Another use of the tilde is to move back to the previous working directory. Imagine you have just switched into Fred's home folder from the /var/logs folder. You can get back to the /var/logs folder by typing the following:

cd ~-

The opposite of ~- is ~+ which when used with the cd command takes you to the present working directory.

This, of course, isn't particularly useful because you are already within the present working directory.

Typing the cd ~ into the terminal and pressing the tab key provides a list of all the potential folders you can go to.

An example of this can be seen in the image above.

To move to the games folder type the following:

cd ~games

This takes you to the folder /usr/games. 

Note that not all of the options listed work with the cd command. 

The last couple of uses of the tilde are as follows:

cd ~0

cd ~1

cd ~-1

This notation lets you move through the directory stack. Folders can be added to the directory stack using pushd.

For example, if you are in your music folder and you want it to appear in the directory stack type the following:

pushd /home/username/Music

Now type the following dirs command:

dirs -v

This shows a list of all the items on the stack.

Think of a stack in its physical form. Imagine you have a stack of magazines. To get to the second magazine down you need to remove one from the top to get to it.

Imagine you had a stack as follows:

0. Music
1. Downloads
2. Scripts

Using the term cd ~2 takes you to the folder in the second position in the stack. Note that the first position is always the current directory so the next time you type dirs -v you will see the following:

0. Scripts
1. Downloads
2. Scripts

If you cd back to the Music folder, position 0 will again be Music. 

The cd command isn't the only command that works with the tilde (~). The ls command works as well.

For example to list all the files in your home folder type the following:

ls ~

The tilde is also used in filenames and is generally created as a backup by text editors. 

The tilde is one of many metacharacters used in Linux. Other metacharacters include the full stop or period (.) which is used to denote current position when used to navigate the file system, the asterisk (*) is used as a wildcard character in searches as is the question mark (?).

The carat symbol (^) is used to denote the start of a line or a string and the dollar symbol is used to denote the end of a string or line whilst searching.

This article describes the use of metacharacters.