Menu

cd command in linux – Mastering the ‘cd’ command in Linux

The 'cd' command is a shell built-in command used to change the current working directory in Linux and Unix-like operating systems. It allows users to navigate through the file system hierarchy, moving from one directory to another.

Written by Jagdeesh | 2 min read

Introduction

Linux is a versatile and powerful operating system, widely used in various applications and environments. One of the fundamental skills any Linux user should possess is the ability to navigate the file system effectively. The ‘cd’ (Change Directory) command is essential for this purpose. In this blog post, we will discuss the ‘cd’ command in depth, along with code examples to help you master it.

What is the ‘cd’ Command?

The ‘cd’ command is a shell builtin command used to change the current working directory in Linux and Unix-like operating systems. It allows users to navigate through the file system hierarchy, moving from one directory to another.

Basic Usage of the ‘cd’ Command

1. Change to a specific directory:

To change to a specific directory, you simply type ‘cd’ followed by the directory path. For example, to change to the ‘/home/user/Documents’ directory, you would use the following command:

python
$ cd /home/user/Documents

2. Change to the home directory:

To quickly navigate to your home directory, use ‘cd’ without any arguments:

python
$ cd

Alternatively, you can use the tilde (~) character as a shorthand for your home directory:

python
$ cd ~

3. Change to the parent directory:

To move up one level in the directory hierarchy (i.e., change to the parent directory), use the ‘..’ notation:

python
$ cd ..

4. Change to the previous working directory:

To return to the previous working directory, use the ‘-‘ character:

python
$ cd -

This command will switch back and forth between the current and previous working directory.

5. Advanced Usage of the ‘cd’ Command

a) Using relative paths:

Instead of specifying the full path, you can use relative paths to navigate through the file system. For example, if you are currently in the ‘/home/user’ directory and want to move to the ‘/home/user/Documents/Reports’ directory, you can use:

python
$ cd Documents/Reports

b) Using environment variables:

You can use environment variables in your ‘cd’ command to navigate the file system. For example, the ‘$HOME’ variable represents your home directory:

python
$ cd $HOME/Documents

c) Tab completion:

To save time and avoid typos, use tab completion while typing directory paths. Start typing a directory name and press the ‘Tab’ key. The shell will automatically complete the directory name if it is unique. If multiple directories share the same prefix, press ‘Tab’ twice to display a list of possible completions.

Conclusion

In this blog post, we have discussed the ‘cd’ command in Linux and provided various code examples to help you navigate the file system more effectively. With practice, you will become proficient in using the ‘cd’ command, allowing you to quickly and efficiently move through directories in your Linux environment.

Free Course
Master Core Python — Your First Step into AI/ML

Build a strong Python foundation with hands-on exercises designed for aspiring Data Scientists and AI/ML Engineers.

Start Free Course
Trusted by 50,000+ learners
Jagdeesh
Written by
Related Course
Master Linux — Hands-On
Join 5,000+ students at edu.machinelearningplus.com
Explore Course
Machine Learning Plus
MachineLearningPlus Expert AI/ML Training

Get a Free 30-Min
Guidance Call

Let our ML expert call you back and guide you for free

You're all set!
Tired of tutorials that go nowhere? This video shows you why and what to do instead.
Watch the Free Training
Redirecting in 5 seconds...
No thanks, I'll figure it out myself

Get your Start in AI/ML with the Foundations for Data Science (AI/ML) Course

Enroll for Free
Scroll to Top
Scroll to Top
Course Preview

Machine Learning A-Z™: Hands-On Python & R In Data Science

Free Sample Videos:

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science