What Does cd ~ Do When Entered Into a Terminal Window

The 'cd' command changes directories; adding a tilde roots to your home

Within Linux, the tilde—the ~ symbol—is a metacharacter. Within the confines of a terminal's shell, it has a special meaning.

Uses for the Tilde

The tilde is a shortcut for home. Used on its own, the command cd ~ returns you to your user account's home folder.

Adding a different account's username takes you to that account's home directory, assuming you have permissions to view it. For example, the command cd ~bob takes you to the home of the account named bob.

Back Whence You Came

Another use of the tilde is to move back to the previous working directory. If you swtiched to your home folder from the /var/logs folder, get back to the /var/logs folder by typing:

cd ~-

Directory Stacks

pushd linux

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
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
2. Scripts

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

ls Command 

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 used to signify a backup by text editors.

Was this page helpful?