DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > How the CD Command Works in Linux

How the CD Command Works in Linux

In this tutorial, we'll look at the cd command, which stands for "change directory." The cd command is one of the most commonly used commands in Linux.

Johnny Simpson user avatar by
Johnny Simpson
CORE ·
Mar. 05, 22 · Web Dev Zone · Tutorial
Like (4)
Save
Tweet
8.22K Views

Join the DZone community and get the full member experience.

Join For Free

The cd command in Linux stands for "change directory." It is used to change the directory when you have a terminal window open. It's used frequently, so it is useful to know.

The syntax for the cd command looks like this:

PowerShell
 
cd [OPTIONS] directory

How the CD Command Works

At its most basic, when we open a new terminal window, we can use cd to navigate between directories. Suppose we are in Documents, and there is a subfolder called Project. If we want to move into the Project folder, we would type the following:

PowerShell
 
cd Project

If we want to move back up to documents, we can use ../ - so assuming we are now in Project, the following command will bring us back to the Documents directory:

PowerShell
 
cd ../

Using Relative Path Names With CD in Linux

While ../ refers to the directory one level above, ./ refers to the current directory. As such, if we are in Documents and want to move to Project again, the following command will also work:

PowerShell
 
cd ./Project

We can also string ../ together - so the following will move two directories up:

PowerShell
 
cd ../../

How To Navigate to the Home Directory in Linux

If you want to navigate to the home directory in Linux, we have to use the cd command along with a tilde, or ~.

As such, the following command will navigate us to the home directory:

PowerShell
 
cd ~

We can also navigate to directories within the home directory by following it with a slash, so the following will move us into a folder called Project within our home directory:

PowerShell
 
cd ~/Project

Options for the CD Command in Linux

There are two options available to the cd command which we can mention straight after we type cd. These are:

  • -L - which is by default enabled, and ensures recognition of symlinks within Linux.
  • -P - which does the opposite of -L and ignores symlinks.

What Are Symlinks?

Symlinks or symbolic links are virtual folders. They link to other folders in other directories. If we use -P with cd, then symlinks are ignored.

An example of a cd command with symlinks disabled looks like this:

PowerShell
 
cd -P ~/Project
Contextual design CI/CD Command (computing) Linux (operating system) Directory Home directory PowerShell

Published at DZone with permission of Johnny Simpson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Blocking Ads on Your Network Using Raspberry Pi 3 + Fedora + Pi-hole
  • Delegating JWT Validation for Greater Flexibility
  • Modern Application Security Requires Defense in Depth
  • Troubleshooting HTTP 502 Bad Gateway in AWS EBS

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo