How to Change Directory in Linux Using cd Command

The cd command is a crucial tool for accessing the file system by altering the current working directory when using the Linux command line interface. You can navigate between directories and access files and directories on your Linux system by learning how to use the cd command. We’ll go through how to use the cd command correctly in this post and examine all of its features.

Prerequisites

  • A system running a Linux distribution.
  • You should have a non-root user on an Ubuntu 22.04 server with Sudo permissions in order to finish this tutorial.
  • Access to the terminal window or command line
  • Read our basic server setup guide to find out how to accomplish this configuration.

How to Use the Linux cd Command

Change Directory is the meaning of the cd command using terminal. Cd, one of Linux’s most basic commands, is now used everywhere to change folders. There are also several other command implementations available, like chdir for MS-DOS platforms.

Fundamental Syntax

The basic syntax for the cd command is:

cd [options] [path]

In the command above:

  • Uses the cd command when entered.
  • [options]: Including options modifies how the command operates.
  • [directory]: The directory’s path where you want to move to.

In its most basic form, cd will transport you to your home directory when invoked without any arguments.

See also  6 Best AI Home Design generators 2023 (Exteriors +Interior)

You can use the Tab key to have the names of directories autocomplete while traversing the file system. It is not required to conclude the directory name with a slash. You need to have executable permissions on a directory in order to switch to it.

To display the full path of your current directory, enter the following command:

pwd

Absolute and Relative Path Names

Both absolute and relative path names can be used to identify a directory to change to. The full or absolute path begins at the system root /, while the relative path begins in the current directory.

Your home directory is the default setting for your current working directory when you log into a Linux system. If the Downloads directory is present in your home directory, you can access it by using the relative directory path:

cd Downloads

Using its absolute path, you can access the same directory as well:

cd /home/username/Downloads

Simply put, the absolute path to the directory is one that begins with a slash (/).To easily go to different directories without having to write the entire pathname, Cd keeps track of which directories are above and below the current directory.

Use the ls command to list down every directory in that folder if you are confused of the exact name of the subdirectory you wish to go to.

Go to the Parent Directory

A directory that has one or more subdirectories is referred to as the parent directory. In other words, if you’re /home directory contains the directories /Desktop and /Downloads, then that directory will serve as the parent directory for those two folders.

See also  How to Enhance Your Google Maps Experience with AI Google Map Features

The letters .. and. stand for the parent directory and the current directory, respectively.

Switch to the parent directory by using the double-dot character (..).

cd ..

You can go up the directory tree one level with the aforementioned command. You can advance up the directory tree by passing more. characters.

Moving two levels up from your current working directory:

cd ../../

A specific directory can also be supplied that is one level above the the working directory at present.

cd ../../

To Change the Previous Working Directory

The – (hyphen) character makes it simple to return to the previous working directory while working with many directories at once.

If your current working directory is /home and you change to /root, as an illustration. Then the working directory will change to /root, and the working directory before that was /home.

You can return to the previous directory, /home, by typing the following command.

cd –

Additionally, using the cd command and then a space will return the user to their previous working directory.

cd

Switch to the Home Directory

The /home directory is a particular location on a Linux system designated for a user’s private documents, applications, and subfolders. The home directory is automatically configured as the current working directory when you log in to your system.

The ~ (tilde) character has been designated as a special character for the home directory.With the cd command, you can simply pass the ~ character to change the current working directory to /home rather than entering the full path to your home directory (/home/username).

cd ~

You can follow these steps to navigate to another user’s home directory.

See also  How to Change MySQL Root Password Simple Trick

cd ~username

We changed the current working directory to /Desktop in the previous section. You can reduce the size of the command to half by using the character to indicate the /home directory.

cd ~/Desktop

Navigate to the Root Folder

The / character in a Linux-based operating system stands for the /root directory, just like the home directory. The root folder can be reached at any time by:

cd /

Make the switch to a directory name with spaces

Your computer’s folder names won’t all be single words. The character “space” may be present in some of them. as in /home/username/Important Documents.

The directory name will not be specified in such cases, and an error will be returned.

cd /Important Documents output bash: cd: too many arguments

Use quotes around the pathname to change to directories that have spaces in their names. Please take note that the command supports both single and double quotes.

cd ‘Important Documents’ cd “Important Documents”

As an alternative, you can remove spaces by using the backslash () character.

cd Important Documents

Changing Directory and List Content

To switch to a new directory and list its contents at the same time, add the ls command to the cd command using the && flag.

cd [path to directory] && ls

Using the above example:

cd Example_Directory && ls

Autocomplete Directory Name

The Linux terminal doesn’t have autocomplete by default. You can install and configure bash-completion package and use the autocomplete feature. If you don’t know the name of the directory you’re trying to move to you can press press the Tab key on your computer following the command.

cd X[Tab]

Conclusion

After reading this guide, you ought to be able to use the Linux cd command to navigate around and alter the current working directory in the terminal window. Please feel free to share your thoughts and feedback in the comment section below.