Menu

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

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
Get the full course,
completely free.
Join 57,000+ students learning Python, SQL & ML. One year of access, all resources included.
📚 10 Courses
🐍 Python & ML
🗄️ SQL
📦 Downloads
📅 1 Year Access
No thanks
🎓
Free AI/ML Starter Kit
Python · SQL · ML · 10 Courses · 57,000+ students
🎉   You're in! Check your inbox (or Promotions/Spam) for the access link.
⚡ Before you go

Python.
SQL. NumPy.
All free.

Get the exact 10-course programming foundation that Data Science professionals use.

🐍
Core Python — from first line to expert level
📈
NumPy & Pandas — the #1 libraries every DS job needs
🗃️
SQL Levels I–III — basics to Window Functions
📄
Real industry data — Jupyter notebooks included
R A M S K
57,000+ students
★★★★★ Rated 4.9/5
⚡ Before you go
Python. SQL.
All Free.
R A M S K
57,000+ students  ★★★★★ 4.9/5
Get Free Access Now
10 courses. Real projects. Zero cost. No credit card.
New learners enrolling right now
🔒 100% free ☕ No spam, ever ✓ Instant access
🚀
You're in!
Check your inbox for your access link.
(Check Promotions or Spam if you don't see it)
Or start your first course right now:
Start Free Course →
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