Change directory

2 snippets across 2 stacks - Bash & Linux, Docker

SHBash & Linux

Change Directory

SH · Navigation & Files
Syntax
cd [directory]
Example
cd /var/log
cd ~
cd ..
cd -

Note cd with no argument returns to your home directory. cd - switches to the previous directory you were in, which is great for toggling between two locations.

DKDocker

WORKDIR - Set Working Directory

DK · Dockerfile
Syntax
WORKDIR /path/to/dir
Example
WORKDIR /app
COPY . .
RUN npm ci

Note All subsequent RUN, CMD, COPY, and ENTRYPOINT instructions use this directory. WORKDIR creates the directory if it does not exist. Avoid using RUN cd /app - it only affects that single RUN layer.

Frequently asked questions

How do you change directory?
This task is covered in 2 stacks on this page: Bash & Linux, Docker. The "Change Directory" snippet in Bash & Linux uses `cd [directory]`.
Which command does the Bash & Linux example use?
The "Change Directory" snippet uses `cd [directory]`, from the Navigation & Files section of the Bash & Linux cheat sheet.
Which stacks cover "change directory" on this page?
Bash & Linux, Docker. Together they hold 2 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Change Directory": cd with no argument returns to your home directory. cd - switches to the previous directory you were in, which is great for toggling between two locations.