×

cd Command in Linux/Unix with Examples

The 'cd' command in Linux is used to change the current working directory. The command stands for "change directory" and it is used to navigate the file system on a Linux computer.

When you open a terminal in Linux, you are placed in your home directory. This is the directory that is specific to your user account and is usually located at /home/username. You can use the 'cd' command to move around the file system and access different directories.

The basic syntax for the 'cd' command is:

cd [directory]

Where "directory" is the name of the directory you want to change to. For example, if you want to change to the '/home/username/documents' directory, you would use the command:

cd /home/username/documents

There are also some shortcut commands and special directories that can be used with the 'cd' command:

  • 'cd ~' : This command will take you to your home directory, regardless of where you currently are in the file system.
  • 'cd ..' : This command will take you up one directory level. For example, if you are currently in the '/home/username/documents' directory and you use the 'cd ..' command, you will be taken to the '/home/username' directory.
  • 'cd -' : This command will take you to the last directory you were in. This can be useful if you need to switch back and forth between two directories frequently.
  • '/' : This is the root directory of the file system, it contains all the other directories and files on your Linux computer.
  • './' : This indicates the current directory, So 'cd ./' will not change the directory you are in
  • '../' : This indicates the parent directory of the current directory.

In addition to using the 'cd' command to navigate the file system, you can also use it to create new directories and remove existing ones.

To create a new directory, you can use the 'mkdir' command followed by the name of the directory you want to create. For example, to create a new directory called 'newfolder' in the current working directory, you would use the command:

mkdir newfolder

To remove a directory, you can use the 'rmdir' command followed by the name of the directory you want to remove. Be careful when using this command, as it will permanently delete the directory and all of its contents.

If a directory contains files, you can use the 'rm -r' command to remove the directory and its contents. For example, to remove the directory 'newfolder' and all of its contents, you would use the command:

rm -r newfolder

Additionally, the 'cd' command can also be used in combination with other commands to perform various tasks. For example, you can use the 'cd' command to change to a specific directory and then use the 'ls' command to view the contents of that directory. You can also use the 'cd' command to change to a specific directory and then use the 'pwd' command to view the path of the current working directory.

Another important aspect of the 'cd' command is that you can use tab completion to avoid typing the entire path of a directory. For example, if you want to change to the '/home/username/documents' directory, you can type 'cd /home/username/d' and then press the tab key. The terminal will automatically complete the rest of the directory name for you, saving you time and effort.

You can also use the 'cd' command to navigate directories in a different file system. For example, if you have a USB drive connected to your computer and it is mounted to the '/mnt/usb' directory, you can use the 'cd' command to change to that directory and access the files on the USB drive.

Another feature of the 'cd' command is that you can use variables and wildcards to navigate through directories. For example, you can use the '$HOME' variable to navigate to your home directory, or use wildcard characters to navigate through directories that match a certain pattern.

In short, the 'cd' command is a powerful and versatile command that is essential for navigating the Linux file system. It allows you to change the current working directory, access special directories, create and remove directories, and even navigate through different file systems. With a little practice, you'll be able to quickly and efficiently navigate your Linux file system and access the files and directories you need.


Related Topics

Linux Admin Tutorial

In publishing and graphic design, lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document without relying on meaningful content....

1 minute read.

netstat Command in Linux/Unix with Examples

The "netstat" command in Linux is a command-line utility that displays network connections, routing tables, and various network statistics. It is used to display the status of network connections, both...

3 minutes read.

Linux Path Completion

Every operating system has some features which make them special. The Linux operating system primarily works on the command line interface, and we do have many features for this CLI....

1 minute read.

rm Command in Linux/Unix with Examples

The "rm" command in Linux (and other Unix-like operating systems) is used to remove files and directories. The "rm" command stands for "remove" and it is used to delete files...

4 minutes read.

Linux apt-get command

Apt-get is a command-line that helps to handle the packages in Linux. Its major task is to assist in retrieving data and packages from trusted sources for purposes of installation,...

6 minutes read.

wget Command in Linux/Unix with Examples

The wget command is a command-line utility in Linux that is used to download files from the internet. It supports various protocols including HTTP, HTTPS, and FTP and can be...

5 minutes read.

adduser Command in Linux with Examples

What is the adduser command? The adduser command creates a new user, and this command may be used to add apre-existing user to a group that already exists. How to use the...

4 minutes read.

Linux Absolute and Relative Path

The concept of the absolute and relative path is very simple. The names “relative path” and “absolute path” give us little understanding of the concept we are learning here. The...

1 minute read.

apt-get Command in Linux with Examples

The apt-get command is a command-line tool that is used to handle packages in Linux. This command handles the processes like retrieving the packages and information from authentic sources for...

5 minutes read.

awk Command in Linux/Unix with Examples

The 'awk' command in Linux is a powerful text processing tool that allows you to perform various operations on text files, such as searching, replacing, and manipulating text. The command...

4 minutes read.

ping Command in Linux/Unix with Examples

The "ping" command in Linux (and other Unix-like operating systems) is used to test the connectivity between two networked devices. The "ping" command sends a small packet of data, called...

4 minutes read.

ps Command in Linux/Unix with Examples

The 'ps' command in Linux is used to display information about the running processes on a system. The command stands for "process status" and it allows you to view details...

4 minutes read.

sed Command in Linux/Unix with Examples

The 'sed' command in Linux is a powerful text processing tool that allows you to perform various operations on text files, such as searching, replacing, and manipulating text. The command...

3 minutes read.

ls Command in Linux/Unix with Examples

The ls command in Linux is a command-line utility that is used to list the files and directories in a directory. It is one of the most basic and commonly...

4 minutes read.

cd Command in Linux/Unix with Examples

The 'cd' command in Linux is used to change the current working directory. The command stands for "change directory" and it is used to navigate the file system on a...

3 minutes read.

cut Command in Linux/Unix with Examples

The "cut" command in Linux (and other Unix-like operating systems) is used to remove sections from each line of a file or stream of data. The cut command is often...

3 minutes read.

touch Command in Linux/Unix with Examples

The "touch" command in Linux (and other Unix-like operating systems) is used to create new empty files or update the timestamps of existing files. The touch command is a simple...

4 minutes read.

Linux cd

Linux cd: As in GUI operating systems like Windows 10, we use the mouse to travel to various folders back and forth; similarly, we have the “cd” command in Linux...

4 minutes read.

telnet Command in Linux/Unix with Examples

The telnet command in Linux is a command-line utility that allows you to connect to a remote host using the Telnet protocol. Telnet is a network protocol that provides a...

3 minutes read.

How to change the ApacheHTTP port in Linux

What is an Apache HTTP server in Linux? When we talk about the most commonly used web servers, one of the web servers that cannot be missed is Apache. With the...

4 minutes read.