← Back to Bash Course | Chapter 9: File Testing & Filesystem | Lesson 10 of 13

Directory Operations

Scripts can create folders, move around between them, and remember where they came from, just like typing cd and ls by hand.

Creating Directories Safely

mkdir -p creates any missing parent directories in one call and does not fail if the target already exists, making it the safe default for scripts.

Example: Creating Directories Safely

bash
#!/bin/bash
mkdir -p project/src/utils
if [[ -d project/src/utils ]]; then
    echo "nested directories created"
fi
rm -rf project

Changing Directories Safely

cd can fail if the target does not exist or is not accessible. Chaining it with || exit 1 (or || return 1 inside a function) stops the script from continuing to operate in the wrong place.

Example: Changing Directories Safely

bash
#!/bin/bash
mkdir -p work_area
cd work_area || exit 1
echo "Now in: $(pwd | xargs basename)"
cd ..
rmdir work_area

Using pushd and popd

pushd dir changes directory while remembering where you came from on a stack, and popd returns to that remembered location. This is convenient for scripts that need to temporarily work elsewhere and come back.

Note: Redirect pushd/popd output to /dev/null in scripts since their default directory-stack printout is rarely useful there.

Example: Using pushd and popd

bash
#!/bin/bash
mkdir -p temp_place
pushd temp_place > /dev/null
echo "working here: $(pwd | xargs basename)"
popd > /dev/null
echo "back to: $(pwd | xargs basename)"
rmdir temp_place

Listing Directory Contents in a Script

While ls is fine for a human to glance at, scripts should generally prefer glob patterns or find to enumerate files, since ls output can be ambiguous with unusual filenames. Plain ls is still fine just to display a listing to a user.

Warning: Never parse ls output in scripts (e.g. for f in $(ls)); use globs (for f in *) or find instead.

Example: Listing Directory Contents in a Script

bash
#!/bin/bash
mkdir -p listing_demo
touch listing_demo/a.txt listing_demo/b.txt
ls listing_demo
rm -rf listing_demo
Common Mistakes
  1. Assuming cd always succeeds; scripts should check its exit status or use cd dir || exit 1 to avoid silently continuing in the wrong directory.
  2. Forgetting mkdir -p is needed to create nested directories or to avoid an error when the directory already exists.
  3. Not realizing pushd/popd maintain a stack, so nesting them incorrectly can leave you in an unexpected directory.
Chapter Summary
  • mkdir -p creates parent directories as needed and does not error if the directory already exists.
  • cd dir || exit 1 is a safe pattern that stops the script if the directory change fails.
  • pushd/popd push and pop directories on a stack, letting you return to a previous location easily.
  • ls inside scripts is best avoided for parsing; prefer globs or find for reliable filename handling.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.