​​How to Change a Directory via Terminal in Ubuntu 22.04?


Ubuntu 22.04 offers two ways for the users to interact with it: Graphical User Interface (GUI) and Terminal. GUI is easy and user-friendly and is popular with new Linux users. On the other hand, the Terminal offers fast and efficient user – Operating System (OS) interaction.

Navigation within the file system by changing the directory is one of the most frequently performed tasks. A user/admin changes the directory for various tasks, such as accessing, interacting, and manipulating the files.

This article will illustrate the following methods of changing a directory in Ubuntu 22.04 LTS:

Let’s begin by discussing changing a directory in Ubuntu 22.04 Terminal via the cd command.

 

Method 1: How to Change a Directory in Ubuntu 22.04 Using the cd Command?

The cd (Change Directory) command is used to navigate from the present working directory (PWD) to any other directory within Ubuntu 22.04’s file system. Let’s begin by demonstrating navigating to a specific directory.

 

Case 1: Change the Current Directory Using Absolute or Relative Path

To change the current working directory to a specific directory, run the cd command followed by the absolute or relative path of the directory.

An absolute path is the path of the directory relative to the root directory whereas a relative path is the path of the directory relative to where the user currently is in the file system.

To change the current directory to Directory1 by using an absolute path, execute the following command:

cd /home/linuxgenie/Directory1

Where /home/linuxgenie/Directory1 is the absolute path of the directory.

The present working directory is changed from “/home/linuxgenie” to “/home/linuxgenie/Directory1”.

Alternatively, if the working directory is already linuxgenie, then a user can opt for the relative path by executing the following command:


The present working directory is now changed to “/home/linuxgenie/directory1”

 

Case 2: Change the PWD and List the Contents of a Directory

The && operator enables a user to combine multiple commands. By using the && operator, the second command is only executed provided the successful execution of the first command. The cd command can be combined with the ls (list) command using the && operator to change the directory and list its contents as shown below:

cd /home/linuxgenie/Directory1 && ls -l

The present working directory is changed from “/home/linuxgenie” to “/home/linuxgenie/Directory1”.

 

Case 3: Change the Current Directory to the Root Directory

The root directory is located on the top of the Linux hierarchical file structure and is represented by a slash “/” symbol. To navigate to the root directory, run the following command:

The current directory is changed from “/home/linuxgenie/Directory1” to the root directory.

 

Case 4: Change the PWD to the Home Directory

The home directory is the default directory. To change the present working directory to the home directory, use the cd keyword or cd keyword followed by the tilde “~” symbol as shown below:


Or


The present working directory is changed from “/home/linuxgenie/Directory1” to the home directory, i.e., “/home/linuxgenie”.

 

Case 5: Change the Current Directory to the Previous Directory

A user can also navigate to the previous directory, i.e., the directory where the user was previously. To change the current directory back to the previous directory, a dash “” is used along with the cd keyword as shown below:


The present working directory is changed from “/home/linuxgenie/Directory1” to the previous directory, i.e., “/home/linuxgenie”.

 

Case 6: Change the PWD to the Parent Directory

A Parent directory is a directory that is one level up in the hierarchy. To navigate from the present working directory to the parent directory, the double period “..” is used along with the cd keyword as shown below:


The current directory is now changed from “/home/linuxgenie/Directory1” to “/home/linuxgenie”.

Similarly, to change the present working directory to multiple directories up in the hierarchy, more double periods “..” can be used. For example, to change the directory to two levels up in the hierarchy, execute the following command:


The present working directory is changed from “/home/linuxgenie/Downloads/Directory1” to “/home/linuxgenie”.

 

Case 7: Change the Current Directory to a Directory that Has White Spaces in its Name

To Change the PWD to a directory that has white spaces in its name, there are three possibilities:

Enclosing the directory name in single quotes (‘ ‘)


Enclosing the directory name in double quotes (“ “)

Using a backslash (\) for each white space

Where “Directory 1”, “Directory 2”, and “Directory 3” are directories with spaces in their names.

The above output indicates that the directories with spaces in their names: “Directory 1”, “Directory 2”, and “Directory 3” are changed successfully by enclosing the file name in Double (“ ”) quotes, Single(‘ ’) quotes and by using a backslash (\) for each white space respectively.

More details about dealing with files with spaces in their names can be viewed in the article: How to Deal with a Filename with Spaces in Linux/Ubuntu?

That’s all about changing a directory in Ubuntu 22.04 Terminal via the cd command. Next, changing a directory via an alias will be demonstrated.

 

Method 2: How to Change a Directory in Ubuntu 22.04 Using an Alias?

The alias command enables a user to create a shortcut for large commands by replacing the command with a string value. By using an alias, a user can avoid typing a large command again and again and instead use an alias. For example, to change a PWD to a particular directory, i.e., /home/linuxgenie/Directory, first create an alias by executing the following command:

alias cdir1=’cd /home/linuxgenie/Directory1′

Where cdir1 is the name of the alias containing the cd command along with the path of the directory.

Next, to use the alias, run the alias name as shown below:


The present working directory is changed from “/home/linuxgenie” to “/home/linuxgenie/Directory1”.

 

Method 3: How to Change a Directory in Ubuntu 22.04 Using a Bash Function?

A bash function enables a user to group a set of commands into reusable code blocks. For example, to navigate to a particular directory, i.e., /home/linuxgenie/Directory, first create a function by the syntax below:

function cdir1() {
cd /home/linuxgenie/Directory1
}

Where cdir1 is the name of the function containing the cd command along with the path of the directory.

To call the function, run the function name as follows:


The present working directory is changed from “/home/linuxgenie” to “/home/linuxgenie/Directory1”.

That’s all about changing a directory in Ubuntu 22.04 Terminal.

Conclusion

The cd is the primary command to change a directory via Terminal in Ubuntu 22.04. To change a directory via cd, either use the absolute path or relative path by running the “cd <absolute/path/of/the/directory>” or “cd <relative/path/of/the/directory>” commands respectively. Additionally, the cd command can be integrated into the alias command and bash function to reuse a single command or a group of commands respectively to create reusable code blocks. This article demonstrated different methods of changing a directory via Terminal in Ubuntu 22.04 LTS.

Print Friendly, PDF & Email