Harness the Combinatoric Power of Command-Line Tools and Utilities

Bash Basics

Lesson 3 - Working with Directories

Bash

Directories (also called folders) help you organize your files. In this lesson you’ll navigate between directories and create new ones.

The Current Directory

The pwd command (print working directory) shows where you are:

pwd

Changing Directories

The cd command (change directory) moves you to a different location:

cd /tmp
pwd

You can use cd .. to go up one level to the parent directory:

cd /tmp
cd ..
pwd

Creating Directories

The mkdir command creates a new directory. Create a project folder:

cd /tmp
mkdir myproject

Use the ls -l command to view the contents of the current directory to see the new myproject directory you created.

ls -l

The ls command shows directory contents, the -l flag shows a long listing of files and directories with timestamps.

You can create nested directories with the -p flag:

cd /tmp/myproject
mkdir -p src/components

Now list the contents of the src directory using the ls -l command:

ls -l src

Specifying ls without a directory gives you a listing of the current working directory. Specifying a directory gives you a listing of that directory.

Now move into the directories we created and explore:

cd /tmp/myproject/src/components
pwd

Your Turn

Move to the /tmp directory.

Then create a directory structure for a web project with css and js folders inside a public directory:

cd /tmp
mkdir -p webproject/public/css
mkdir -p webproject/public/js
ls -l webproject/public

Removing Directories

Use rmdir to remove an empty directory, or rm -r to remove a directory and its contents:

cd /tmp
mkdir empty_folder
rmdir empty_folder
ls -l | grep empty

Useful Directory Commands

CommandPurpose
pwdPrint current directory
cdChange directory
cd ..Go up one level
cd ~Go to home directory
mkdirCreate a directory
mkdir -pCreate nested directories
rmdirRemove empty directory
rm -rRemove directory and contents

What You Learned

  • pwd shows your current location
  • cd changes directories
  • mkdir creates directories, with -p for nested paths
  • rmdir removes empty directories