StudyLover
  • Home
  • Study Zone
  • Profiles
  • Typing Tutor
  • Contact us
  • Sign in
StudyLover Linux Command: cd
Download
  1. Linux
Linux command: ls : Linux Command: cp
Linux

What cd does (and why it matters)

  • cd changes your current working directory (where the shell is “standing”).

  • It’s a shell built-in (not an external program), so it affects your current terminal session immediately.

pwd         # show where you are

cd /etc     # go to /etc

pwd


1) Basic moves

cd                # go to your HOME directory (same as: cd ~)

cd /              # go to filesystem root

cd ~/Downloads    # go to Downloads inside your home

cd ..             # go up one level (to the parent directory)

cd ../..          # go up two levels

cd -              # jump back to the previous directory (toggles)

  • ~ expands to your home (e.g., /home/rohit).

  • . = current directory, .. = parent directory.


2) Absolute vs relative paths

  • Absolute: starts with / (e.g., /var/log) — works from anywhere.

  • Relative: from where you are now (e.g., cd ../project/src).


3) Paths with spaces & special characters

Use quotes or backslashes:

cd "My Projects/sem1"

cd My\ Projects/sem1


4) Handy environment variables

  • echo $HOME → your home directory

  • echo $PWD → current directory (“print working directory”)

  • echo $OLDPWD → previous directory (used by cd -)


5) Super useful: CDPATH

Let cd search preset folders so you can jump without typing full paths.

# Add to ~/.bashrc (then: source ~/.bashrc)

export CDPATH=".:$HOME:$HOME/projects:$HOME/lab"

 

cd linux101    # will jump to ~/lab/linux101 if it exists

Order matters (left to right).


6) Following or avoiding symlinks: -L vs -P (bash)

  • Default (-L, logical): keep symlinks in your PWD.

  • Physical (-P): resolve the real path on disk (no symlinks in PWD).

cd -P /var/www/current   # PWD becomes the real directory target, not the symlink

(If unsure, just remember: -P gives you the real path.)


7) Common errors (and quick fixes)

  • No such file or directory → path typo; tab-complete to verify.

  • Permission denied → you don’t have execute (x) on that dir; check ls -ld dir.

  • Stuck at root or protected dirs → normal users can’t cd into some system folders.


8) Faster navigation tricks

Put these in ~/.bashrc:

alias ..='cd ..'

alias ...='cd ../..'

# go up N levels: up 3  -> cd ../../..

up() { local n=${1:-1}; while ((n--)); do cd ..; done; }

 

# quick jump bookmarks (simple)

alias cdl='cd ~/lab'

alias cdp='cd ~/projects'

Reload: source ~/.bashrc


9) Script best practices (you’ll use this a lot)

  • Always quote variables so spaces don’t break paths.

  • Check cd success (exit code) before continuing.

#!/usr/bin/env bash

set -Eeuo pipefail

 

# go to the script’s directory

cd "$(dirname "$0")"

 

# or safely enter a target and fail early if missing

target="$HOME/projects/os-lab"

if cd "$target"; then

  echo "Now in: $PWD"

else

  echo "Cannot enter $target" >&2

  exit 1

fi


10) Related commands you’ll pair with cd

pwd            # confirm where you are

ls -la         # list contents after moving

pushd / popd   # directory stack (nice for bouncing between dirs)


11) Mini-lab (10–15 min)

mkdir -p ~/lab/cd/{alpha,beta,"My Projects"}

cd ~/lab/cd/alpha && pwd

cd ../beta && pwd

cd -                  # back to previous dir (alpha)

cd "../My Projects"   # path with spaces

pwd

# Try CDPATH:

echo 'export CDPATH="$HOME:~/lab/cd"' >> ~/.bashrc

source ~/.bashrc

cd beta               # should work from anywhere now


Exam-ready bullets

  • cd is a shell built-in to change the current directory.

  • cd, cd ~ → HOME; cd .. → parent; cd - → previous dir.

  • Absolute vs relative paths; quote paths with spaces.

  • $PWD/$OLDPWD track current/previous dirs; CDPATH speeds navigation.

  • cd -P uses the physical path (resolves symlinks).

Want a printable one-pager for your class with cd, pwd, and ls together? I can package it neatly.

 

Linux command: ls Linux Command: cp
Our Products & Services
  • Home
Connect with us
  • Contact us
  • +91 82955 87844
  • Rk6yadav@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website