cd / pwd
cd changes the current directory, and pwd prints the path of the current working directory. Both are essential terminal commands you will use every day.
Syntax
cd [directory] # Change to the specified directory pwd # Print the full path of the current directory
Command List
| Command / Notation | Description |
|---|---|
| cd directory | Changes to the specified directory. |
| cd | With no arguments, changes to the home directory ($HOME). |
| cd ~ | Changes to the home directory. Equivalent to cd with no arguments. |
| cd - | Returns to the previous directory. Useful for toggling between two directories. |
| cd .. | Moves up one level to the parent directory. |
| cd ../.. | Moves up two directory levels. |
| pwd | Prints the absolute path of the current working directory to standard output. |
| pwd -L | Prints the logical path without resolving symbolic links (default behavior). |
| pwd -P | Prints the physical path with all symbolic links resolved. |
Sample Code
The following examples use this directory structure.
Change to the home directory and check the current location.
cd pwd /home/user
Change to /var/log using an absolute path.
cd /var/log pwd /var/log
Move up one directory level.
cd .. pwd /var
Use $ cd - to return to the previous directory. This is handy when you need to switch back and forth between two directories.
cd - /var/log cd - /var
Navigate using a relative path from the home directory.
cd ~/Documents/Projects pwd /home/user/Documents/Projects
Move up two directory levels.
cd ../.. pwd /home/user
Notes
$ cd - toggles between the current directory and the previous one. It is extremely convenient when you need to switch back and forth between two directories during a work session. The previous directory is stored in the environment variable $OLDPWD.
$ pwd is a shell built-in, but an external command (/bin/pwd) also exists. Their behavior can differ under symbolic links, so in scripts it is safer to use $ pwd -P to obtain the physical path.
To list the contents of a directory, use ls.
If you find any errors or copyright issues, please contact us.