dirs

file managementLinux/Unix
The dirs command is one of the most frequently used commands in Linux/Unix-like operating systems. dirs Display the list of currently remembered directories in the directory stack

Quick Reference

Command Name:

dirs

Category:

file management

Platform:

Linux/Unix

Basic Usage:

dirs [options] [arguments]

Common Use Cases

  • 1

    Directory navigation

    View and manage the directory stack for quick navigation

  • 2

    Multi-directory workflow

    Efficiently work between multiple directories without typing full paths

  • 3

    Shell history

    Track previously visited directories for easy return

  • 4

    Script path management

    Manage directory context in shell scripts

Syntax

dirs [OPTION]

Options

Option Description
-c Clear the directory stack, removing all entries
-l Long format; display directory names in full without tilde substitution
-p Print each directory in the stack on a separate line
-v Print each directory in the stack with its position number
+N Display the Nth entry from the left of the list (starting with zero)
-N Display the Nth entry from the right of the list (starting with zero)

Examples

How to Use These Examples

The examples below show common ways to use the dirs command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

# Basic Examples Basic
dirs
Display the directory stack with the current directory first.
pushd ~/Documents pushd /etc dirs
Show the directory stack after adding directories.
# Advanced Examples Advanced
dirs -v Display the directory stack with numerical indices. dirs -p Display each directory in the stack on a separate line. dirs -c Clear the directory stack. dirs -l Display directory stack with full paths (no tilde substitution). pushd +2 dirs Rotate the stack and display the result.

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

Dirs Command Overview: The dirs command displays the list of currently remembered directories (the directory stack). It works in conjunction with the pushd and popd commands to help navigate directories in the shell. Directory Stack: - The directory stack is a list of directories that can be quickly accessed - The current directory is always at the top of the stack (position 0) - New directories are added with the pushd command - Directories are removed with the popd command - The dirs command shows the contents of the stack Working with Directory Stack: 1. Use pushd to add a directory to the stack and change to it 2. Use popd to remove a directory from the stack and return to the previous one 3. Use dirs to view the current stack Navigation Shortcuts: - pushd +N: Rotate the stack so that the Nth directory is at the top - popd +N: Remove the Nth directory from the stack - dirs +N: Display only the Nth directory from the stack Shell Compatibility: The dirs command is a built-in command in Bash, Zsh, and other shells. Its behavior might slightly differ between shells, particularly regarding option flags. Practical Uses: - Quickly switch between multiple working directories - Bookmark frequently used directories for easy navigation - Create scripts that need to return to previous working directories - Maintain a history of visited directories

Tips & Tricks

1

Use the -v option to show the directory stack with numeric indices

2

Use the -c option to clear the directory stack

3

Use the -l option to show full paths without tilde substitution

4

Use the -p option to display one directory per line

5

Access a specific directory with +N (from left) or -N (from right) notation

Common Use Cases

Directory navigation

View and manage the directory stack for quick navigation

Multi-directory workflow

Efficiently work between multiple directories without typing full paths

Shell history

Track previously visited directories for easy return

Script path management

Manage directory context in shell scripts

Directory bookmarking

Use the directory stack as a bookmarking system for important locations

Related Commands

These commands are frequently used alongside dirs or serve similar purposes:

Use Cases

1

Directory navigation

View and manage the directory stack for quick navigation

2

Multi-directory workflow

Efficiently work between multiple directories without typing full paths

3

Shell history

Track previously visited directories for easy return

4

Script path management

Manage directory context in shell scripts

5

Directory bookmarking

Use the directory stack as a bookmarking system for important locations

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the dirs command works in different scenarios.

$ dirs
View All Commands