Using the ~ Character with Linux Commands to Represent the Home Directory of a User
On most PC keyboards, the ~ (tilde character) is located near the top at the left, beside the number 1 key.
Linux uses the ~ to represent the directory path to the home directory of the currently logged in user. This character can be used with commands to do many tasks, such as view the contents or change directory into the home directory.
For example, if you log in as cwest, then you can use the ls command: ls ~ to list the files in the cwest directory, below /home. Running ls with the ~ is the equivalent of running: ls /home/cwest.
Run the ls with the ~ now.
If you are currently working as the root user, do the steps to work as a regular user (such as type in exit and press Enter).
Now run the same ls command as above again and you should see the contents of your home directory (below /home).
If you’re working as a user named cwest, then the command: cd ~ is the equivalent of: cd /home/cwest and changes your current directory to the cwest directory, below /home.
Use the following command to change into the etc subdirectory of the / and run the pwd command to see your current path.
$ cd /etc ; pwd
Now use the cd command with ~ to change into your home directory and then run pwd to see the path to your home directory.
Do the steps to start working as the root user (run su -) or (run sudo -i) and use the latter if you’re working in Ubuntu or a variation of this distro.
Now change into the usr directory below / (just to go out of your home directory) and then run the ls command with the ~ to see the contents of /root.
Now use cd and ~ to move into the home directory of the root user and then run pwd to see your current path.
And now I would like to offer you free access to my Linux Commands Training Mini-Course, a 7 Lesson, Daily Mini-Course, including the free Linux Commands ebook and Linux audio podcasts – showing you how to get started learning how to use Linux commands