×

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.

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 SCP Command

SCP stands for secure copy protocol. It is a command-line application for copying files and directories safely between two places. Command-line utilities are programs that operate on a computer's command...

3 minutes read.

Linux Distributions

Linux Distributions Linux OS is not like other operating systems. Linux can be modified according to the user’s requirements, and it is very flexible to any change. Linux distributions are often...

4 minutes read.

sudo Command in Linux/Unix with Examples

The sudo command in Linux is used to run a command or set of commands with the privileges of another user, typically the root user. This allows users to perform...

3 minutes read.

ssh Command in Linux/Unix with Examples

The ssh (Secure Shell) command in Linux is used to remotely access and control another computer or device over a network. It provides a secure and encrypted connection between the...

6 minutes read.

chmod Command in Linux/Unix with Examples

The chmod command in Linux is a command-line utility that is used to change the permissions of files and directories. It stands for "change mode" and it is used to...

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.

Difference Between Unix and Linux

Unix vs Linux Unix and Linux both have been of great use in changing the world of operating systems. Unix was among the very few operating systems which were programmed for...

4 minutes read.

addgroup Command in Linux with Examples

What is the addgroup command? It is a command in Linux to create a new group using specified command lined and the system’s default values. How to add groups Adding a user group:...

4 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.

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 Commands List

Linux Commands List The Linux systems are filled with commands for several use cases. We have divided these commands into some of the most common implementations for a better understanding of...

2 minutes read.

Linux mkdir | Linux Create Directory

The Linux mkdir command is very much similar to Windows’s "new folder" feature. This command is used in Linux os to create new directories. The command mkdir stands for "make...

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.

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.

Linux Tutorial for Beginners

What is Linux? Linux is an open source Operating System like Windows, Android, Mac OS, and iOS. An operating system is a software that enables communication between the user and the...

12 minutes read.

Rename Folder in Linux

Rename Folder in Linux In the Linux system, there is not any single dedicated command for renaming the files and directories. Mostly, the mv command is used extensively for the purpose...

3 minutes read.

Top 20 Linux Commands

1) clear command clear is a standard operating system command which is used to clear the terminal screen. This command brings the command line on the top of the terminal. By pressing the...

10 minutes read.

echo Command in Linux/Unix with Examples

The "echo" command in Linux is used to display a line of text on the terminal. It can also be used to print the value of a variable or to...

3 minutes read.