8.3. Changing the working directory

The cd command

Option Usage
cd ~ Goes to the home directory
cd dir Goes to dir, which is a sub directory located in the current directory
cd /dir Goes to dir, which is a sub directory located in the home directory
cd .. Goes to the parent directory
cd - Goes to the previous directory
cd ~username Goes to the user home directory

cd changes the current working directory. The syntax is cd new_path, where new_path can be the absolute or relative path of the new working directory.

Absolute paths are file locations with respect to the home directory and start with /. Relative paths are file locations with respect to the current directory.

Go to the parent directory Remember that double points (..) represent the parent directory. If my current working directory is /Users/user_name/Desktop/SomeFolder, after typing cd .. the new working directory will be /Users/user_name/Desktop (one folder up). If you type again cd .., now the working directory will be: /Users/user_name (another folder up), etc.

$ pwd
/Users/user_name/Desktop/SomeFolder
$ cd ..
$ pwd
/Users/user_name/Desktop
$ cd ..
$ pwd
/Users/user_name
$ cd ..
$ pwd
/Users
$ cd ..
$ pwd
/

/ represents the home directory and you cannot go up any more folders.

Go to a folder specified by its absolute path In this example, I am using the absolute path of a folder to jump from my current directory to that folder:

$ pwd
/Users/user_name/Desktop/SomeFolder
$ cd /Volumes/Shared
$ pwd
/Volumes/Shared

Go to the parent directory of my parent directory

$ pwd
/Users/user_name/Desktop/SomeFolder
$ cd ../..
$ pwd
/Users/user_name

Using other relative paths to change directory
If inside my current directory (/Users/user_name/Desktop/SomeFolder) there is another folder (i.e. AnotherFolder), I can go to that folder using its relative path:

$ pwd
/Users/user_name/Desktop/SomeFolder
$ cd AnotherFolder
/Users/user_name/Desktop/SomeFolder/AnotherFolder

$ pwd
/Users/user_name/Desktop/SomeFolder
$ cd ./AnotherFolder
/Users/user_name/Desktop/SomeFolder/AnotherFolder

Jumping to the previous directory
In this example, the first working directory is /Volumes/Shared, then I jump to /Volumes/Shared/Articles using the command cd. Finally, I go back to the first working directory /Volumes/Shared) using cd -.

$ pwd
/Volumes/Shared
$ cd Articles
$ pwd
/Volumes/Shared/Articles
$ cd -
$ pwd
/Volumes/Shared