Working Efficiently Within Different Directories in a Linux Shell

Table of Contents

Introduction

Suppose you need to perform work in multiple directories:

  • /var/www/html
  • /etc/apache2
  • /etc/ssl/certs
  • ~/Work/Projects/Web/src

You might use the cd command, but repeatedly typing it is inefficient:

$ cd ~/Work/Projects/Web/src
$ cd /var/www/html
$ cd /etc/apache2
$ cd ~/Work/Projects/Web/src
$ cd /etc/ssl/certs

A more efficient approach is using the directory stack.

Directory Stack

The directory stack allows pushing and popping directories.

  • Pushing a directory adds it to the top of the stack.
  • Popping removes the topmost directory.
  • Initially, the stack contains only your current directory.

Push a Directory onto the Stack

Use pushd to:

  1. Add a directory to the top of the stack.
  2. Change to that directory.
  3. Print the updated stack.

Example:

$ pwd
/home/john/Work/Projects/Web/src
$ pushd /var/www/html
/var/www/html ~/Work/Projects/Web/src
$ pushd /etc/apache2
/etc/apache2 /var/www/html ~/Work/Projects/Web/src
$ pushd /etc/ssl/certs
/etc/ssl/certs /etc/apache2 /var/www/html ~/Work/Projects/Web/src
$ pwd
/etc/ssl/certs

View a Directory Stack

Print the stack using dirs:

$ dirs -p
/etc/ssl/certs
/etc/apache2
/var/www/html
~/Work/Projects/Web/src

Or with numbering:

$ dirs -v
0 /etc/ssl/certs
1 /etc/apache2
2 /var/www/html
3 ~/Work/Projects/Web/src

Pop a Directory from the Stack

Use popd to:

  1. Remove the top directory from the stack.
  2. Change to the new top directory.
  3. Print the updated stack.

Example:

$ popd
/etc/apache2 /var/www/html ~/Work/Projects/Web/src
$ popd
/var/www/html ~/Work/Projects/Web/src
$ popd
~/Work/Projects/Web/src
$ popd
bash: popd: directory stack empty
$ pwd
~/Work/Projects/Web/src

Swap Directories on the Stack

Running pushd with no arguments swaps the top two directories:

$ dirs
/etc/apache2 ~/Work/Projects/Web/src /var/www/html
$ pushd
~/Work/Projects/Web/src /etc/apache2 /var/www/html
$ pushd
/etc/apache2 ~/Work/Projects/Web/src /var/www/html

Turn a Mistaken cd into a pushd

If you accidentally use cd, restore the lost directory:

$ dirs
~/Work/Projects/Web/src /var/www/html /etc/apache2
$ cd /etc/ssl/certs
$ dirs
/etc/ssl/certs /var/www/html /etc/apache2

Fix it with:

$ pushd -
~/Work/Projects/Web/src /etc/ssl/certs /var/www/html /etc/apache2
$ pushd
/etc/ssl/certs ~/Work/Projects/Web/src /var/www/html /etc/apache2

Go Deeper into the Stack

Use pushd +N to shift directories:

$ dirs
/etc/ssl/certs ~/Work/Projects/Web/src /var/www/html /etc/apache2
$ pushd +1
~/Work/Projects/Web/src /var/www/html /etc/apache2 /etc/ssl/certs
$ pushd +2
/etc/apache2 /etc/ssl/certs ~/Work/Projects/Web/src /var/www/html

Or print numbered directories before jumping:

$ dirs -v
0 /etc/apache2
1 /etc/ssl/certs
2 ~/Work/Projects/Web/src
3 /var/www/html

To jump to /var/www/html, use:

$ pushd +3

Remove directories with popd +N:

$ dirs
/var/www/html /etc/apache2 /etc/ssl/certs ~/Work/Projects/Web/src
$ popd +1
/var/www/html /etc/ssl/certs ~/Work/Projects/Web/src
$ popd +2
/var/www/html /etc/ssl/certs

Related Posts

Backup OPNsense to Nextcloud

What is OPNsense? OPNsense® is an open source, easy-to-use and easy-to-build FreeBSD based firewall and routing platform.

Read More