☼
⌃
☰

Bash Navigation Commands

Table of Contents

Bash Navigation Commands

Navigating the filesystem is a fundamental skill for using the command line. These commands allow you to move between directories, view your current location, and list the contents of directories.

pwd: Print Working Directory

This command displays the full, absolute path of the directory you are currently in. It's useful for orienting yourself within the filesystem.

Usage:

SH
pwd

Example Output:

/home/gregory/documents/projects

ls: List Directory Contents

The ls command lists the files and directories within the current directory. It has many useful options (flags) to customize its output.

Usage:

SH
ls [options] [directory]

Common Options: * -l: Lists files in a long format, showing permissions, owner, size, and modification date. * -a: Shows all files, including hidden files (those starting with a .). * -h: Used with -l, it prints file sizes in a human-readable format (e.g., K, M, G). * -t: Sorts the output by modification time, with the newest files listed first. * -R: Lists the contents of sub-directories recursively.

Example:

SH
# List all files in long, human-readable format
ls -lah

cd: Change Directory

The cd command is used to move to a different directory in the filesystem.

Usage:

SH
cd [directory]

Common Arguments: * cd..: Moves to the parent directory (one level up). * cd ~ or just cd: Moves to your home directory. * cd -: Moves to the previous directory you were in. * cd /path/to/directory: Moves to a directory using its absolute path. * cd relative/path: Moves to a directory relative to your current location.

Directory Stack: pushd, popd, dirs

For more complex navigation, you can use a directory stack. This allows you to "remember" a series of directories and navigate back and forth between them easily.

  • pushd [directory]: Pushes a directory onto the stack and changes to it.
  • popd: Removes the top directory from the stack and changes to the new top directory.
  • dirs -v: Displays the numbered directory stack.