What is the command to change from a working directory to the users home directory?

To change directories from your current working directory, use the command cd.

The above command changes directories to the /tmp/ directory. The second word on the command line must be a path. It can either be relative or absolute, and can move one directory or many. If the cd command is entered at the shell prompt with no options or path specified, the default action is to move the user into their home directory. For example, user someone would be moved to the /home/someone/ directory.

The cd has many useful options. You can see all of these options by entering man cd at the shell prompt. The most commonly used options are listed below.

  • cd — Returns you to your login directory

  • cd - — Returns you to your previous working directory

  • cd ~ — Also returns you to your login directory

  • cd / — Takes you to the entire system's root directory.

  • cd /root — Takes you to the home directory of the root user. You must be the root user to access this directory.

  • cd /home — Takes you to the home directory, where user login directories are usually stored

  • cd .. — Takes you to the directory one level up.

  • cd ~otheruser — Takes you to otheruser's home directory, if otheruser has granted you permission.

Below are a few examples of the use of cd.

  • cd /dir1/dir2/ — Regardless of which directory you are in, this absolute path takes you directly to dir2, a subdirectory of /dir1/.

  • cd ../../dir2/dir3/ — This relative path takes you up two directories, then to dir2/, and finally into its subdirectory dir3/.

If you attempt to cd into a directory you do not have permission to access, you are denied permission to access that directory.

Denying access to the root and other users' accounts (or home directories) is one way your Red Hat Enterprise Linux system prevents accidental or malicious tampering. Refer to Section 4.11 Ownership and Permissions for more information.

System navigation is one of the most basic tasks that a user can perform on an operating system. While many file managers are available that allow you to switch directories and navigate your disk drives in a graphical way, doing the same using the command line can provide you better control over your system.

Luckily, Linux provides you with a command known as cd, which allows you to easily change the current working directory on your terminal.

Here's how you can use the cd command on Linux, the only utility that you'll ever need to traverse through directories on your system storage.

How To Use the cd Command in Linux

The cd command stands for Change Directory. One of the most fundamental commands in Linux, cd has become a global command for switching directories. Some other implementations of the command such as chdir, used in MS-DOS systems, are also present.

Basic Syntax

The basic syntax of the cd command is:

cd [options] [path]

...where options are the arguments passed with the command and path is the absolute or relative path to the directory.

Absolute and Relative Pathnames

Before you can use the cd command to its full extent, you must know the difference between absolute and relative pathnames. The absolute pathname is the complete path to the directory, starting from the / (root) folder.

On the other hand, the relative pathname is derived from the current working directory. You can move up and down the directory tree efficiently with the cd command if you're well-versed on relative paths.

For example, if your current working directory is /home, and you want to change the directory to /Desktop. Then, using the absolute pathname:

cd /home/username/Desktop

On the other hand, if you want to switch to the /Desktop directory using a relative path, all you have to type is:

cd /Desktop

Cd keeps track of what directories are above and below the current directory, to allow a user to quickly shift to other directories without having to type the whole pathname.

If you don't know the exact name of the subfolder that you want to switch to, use the ls command to list down every directory in that folder.

Switch to the Home Directory

On a Linux system, the /home directory is a special directory reserved for a user's personal files, programs, and subfolders. When you log in to your system, the home directory is set as the current working directory by default.

The home directory has a special character assigned to it---the ~ (tilde) character. Instead of specifying the complete path to your home directory (/home/username), you can just pass the ~ character with the cd command to change the current working directory to /home.

cd ~

Similarly, you can navigate to other user's home directory as follows.

cd ~username

In the previous section, we switched the present working directory to /Desktop. In the command, you can use the ~ character to denote the /home directory and strip down the command to half of its size.

cd ~/Desktop

Just like the home directory, the / character denotes the /root directory on a Linux-based operating system. To switch to the root folder anytime:

cd /

Change to the Previous Working Directory

If you're working with multiple directories at a time, you can easily switch back and forth to the previous working directory using the - (hyphen) character.

For example, if the current working directory is /home and you switch to the /root directory. Then, /root will become the current working directory, and /home will be the previous working directory.

Typing the following command will take you to the previous directory i.e. /home.

cd -

Also, issuing the cd command followed by a space character will take the user to the previous working directory.

cd 

Switch to the Parent Directory

The directory which consists of one or more sub-directories is known as the parent directory. Simply put, if you have /Desktop and /Downloads folders in your /home directory, then the /home directory will be the parent directory for /Desktop and /Downloads.

The .. and . characters stand for the parent directory and the current directory respectively.

Use the double dots character (..) to switch to the parent directory.

cd ..

The aforementioned command will take you one level up the directory tree. You can also pass additional .. characters to move further up the directory tree.

To move two levels above your current working directory:

cd ../../

You can also pass a specific directory one level above the present working directory.

cd ../Folder

Change to a Directory Name With Spaces

Not every folder on your system will have a one-word name. Some of them might include the space character. For example, /home/username/Important Documents.

In such situations, specifying the directory name will return an error.

cd /Important Documents

Output:

bash: cd: too many arguments

To switch to directories that have spaces in the name, wrap the pathname in quotes as follows. Note that you can use both single and double quotes in the command.

cd 'Important Documents'
cd "Important Documents"

Alternatively, you can use the backward slash (\) character to escape the spaces.

cd Important\ Documents

System Navigation Through the Command Line

The Linux terminal is a powerful text interface for controlling the working of your computer. You can carry out almost any task using the command line. Changing directories, copying files and folders remotely, listing file information, editing configuration files, you name it. There's a command for everything that you want to do on Linux.

However, there's a drawback to having so many commands. Not everyone can learn and memorize the commands, not to mention the hefty amount that's available to use in the first place. But as a beginner Linux user, you won't be using all of the utilities that are available on your system.

Instead, learning some basic commands that will help you in performing fundamental operations is more than enough to get started with the operating system.

How do I change my working directory to my home directory?

If we type cd followed by nothing, cd will change the working directory to our home directory. A related shortcut is to type cd ~user_name . In this case, cd will change the working directory to the home directory of the specified user. Typing cd - changes the working directory to the previous one.

Which command is used to change directory to the home directory?

cd ~ : this command is used to change directory to the home directory.

How do I change the working directory to my home directory in Linux?

To navigate to your home directory, use "cd" or "cd ~" To navigate up one directory level, use "cd .." To navigate to the previous directory (or back), use "cd -"

What is the command for home directory?

The commands cd , and cd ~ are very useful for quickly navigating back to your home directory. We will be using the ~ character in later lessons to specify our home directory.