How do I add directories to a stack and switch to to them in the current shell session?



  • Aliases is an alternative.

    pushd command adds a directory to the top of a directory stack, The stack is just a list of remembered directories.

    dirs command provides the status of the remembered directories.

    What is currently in the directory stack?

    $ dirs -l -v
     0  /usr/local/bin
    

    Add a directory to the top of the stack.

    $ pushd /home/training/Documents/pdfs/techdocs/oracle/db
    ~/Documents/pdfs/techdocs/oracle/db /usr/local/bin
    

    Check the stack again.

    $ dirs -l -v
     0  /usr/local/bin
     1  /home/training/Documents/pdfs/techdocs/oracle/db
    

    Lets say we want to cd directory (without using cd) to the long directory path.

    $ pushd
    ~/Documents/pdfs/techdocs/oracle/db /usr/local/bin
    

    pushd switches to the ~/Documents/pdfs/techdocs/oracle/db /usr/local/bin directory.

    $ pwd
    /home/training/Documents/pdfs/techdocs/oracle/db
    

    Run pushd again and it switches to the other directory in the stack /usr/local/bin.

    $ pushd
    /usr/local/bin ~/Documents/pdfs/techdocs/oracle/db
    $ pwd
    /usr/local/bin
    

    Looking at the stack. The pushd command pushes the current working directory to the top of the stack.

    $ dirs -l -v
     0  /usr/local/bin
     1  /home/training/Documents/pdfs/techdocs/oracle/db
    

    Adding another directory to the stack. Our stack looks like this.

    $ pushd ~/20343546224544
    ~/20343546224544 /usr/local/bin ~/Documents/pdfs/techdocs/oracle/db
    $ dirs -l -v
     0  /home/training/20343546224544
     1  /usr/local/bin
     2  /home/training/Documents/pdfs/techdocs/oracle/db
    

    Running pushd without any arguments will switch between the top two directories.

    What if we want to switch to /home/training/Documents/pdfs/techdocs/oracle/db in the stack. We use pushd with the number argument. The number start at zero.

    Looking at our stack: `Take into account the main readme for this section. README first.

    Aliases is another option.

    pushd command adds a directory to the top of a directory stack, The stack is just a list of remembered directories.

    dirs command provides the status of the remembered directories.

    What is currently in the directory stack?

    $ dirs -l -v
     0  /usr/local/bin
    

    Add a directory to the top of the stack.

    $ pushd /home/training/Documents/pdfs/techdocs/oracle/db
    ~/Documents/pdfs/techdocs/oracle/db /usr/local/bin
    

    Check the stack again.

    $ dirs -l -v
     0  /usr/local/bin
     1  /home/training/Documents/pdfs/techdocs/oracle/db
    

    Lets say we want to cd directory (without using cd) to the long directory path.

    $ pushd
    ~/Documents/pdfs/techdocs/oracle/db /usr/local/bin
    

    pushd switches to the ~/Documents/pdfs/techdocs/oracle/db /usr/local/bin directory.

    $ pwd
    /home/training/Documents/pdfs/techdocs/oracle/db
    

    Run pushd again and it switches to the other directory in the stack /usr/local/bin.

    $ pushd
    /usr/local/bin ~/Documents/pdfs/techdocs/oracle/db
    $ pwd
    /usr/local/bin
    

    Looking at the stack. The pushd command pushes the current working directory to the top of the stack.

    $ dirs -l -v
     0  /usr/local/bin
     1  /home/training/Documents/pdfs/techdocs/oracle/db
    

    Adding another directory to the stack. Our stack looks like this.

    $ pushd ~/20343546224544
    ~/20343546224544 /usr/local/bin ~/Documents/pdfs/techdocs/oracle/db
    $ dirs -l -v
     0  /home/training/20343546224544
     1  /usr/local/bin
     2  /home/training/Documents/pdfs/techdocs/oracle/db
    

    Running pushd without any arguments will switch between the top two directories.

    What if we want to switch to /home/training/Documents/pdfs/techdocs/oracle/db in the stack. We use pushd with the number argument. The number start at zero.

    Looking at our `Take into account the main readme for this section. README first.

    Aliases is another option.

    pushd command adds a directory to the top of a directory stack, The stack is just a list of remembered directories.

    dirs command provides the status of the remembered directories.

    What is currently in the directory stack?

    $ dirs -l -v
     0  /usr/local/bin
    

    Add a directory to the top of the stack.

    $ pushd /home/training/Documents/pdfs/techdocs/oracle/db
    ~/Documents/pdfs/techdocs/oracle/db /usr/local/bin
    

    Check the stack again.

    $ dirs -l -v
     0  /usr/local/bin
     1  /home/training/Documents/pdfs/techdocs/oracle/db
    

    Lets say we want to cd directory (without using cd) to the long directory path.

    $ pushd
    ~/Documents/pdfs/techdocs/oracle/db /usr/local/bin
    

    pushd switches to the ~/Documents/pdfs/techdocs/oracle/db /usr/local/bin directory.

    $ pwd
    /home/training/Documents/pdfs/techdocs/oracle/db
    

    Run pushd again and it switches to the other directory in the stack /usr/local/bin.

    $ pushd
    /usr/local/bin ~/Documents/pdfs/techdocs/oracle/db
    $ pwd
    /usr/local/bin
    

    Looking at the stack. The pushd command pushes the current working directory to the top of the stack.

    $ dirs -l -v
     0  /usr/local/bin
     1  /home/training/Documents/pdfs/techdocs/oracle/db
    

    Adding another directory to the stack. Our stack looks like this.

    $ pushd ~/20343546224544
    ~/20343546224544 /usr/local/bin ~/Documents/pdfs/techdocs/oracle/db
    $ dirs -l -v
     0  /home/training/20343546224544
     1  /usr/local/bin
     2  /home/training/Documents/pdfs/techdocs/oracle/db
    

    Running pushd without any arguments will switch between the top two directories.

    What if we want to switch to /home/training/Documents/pdfs/techdocs/oracle/db in the stack. We use pushd with the number argument. The number start at zero.

    Looking at our stack: 2 /home/training/Documents/pdfs/techdocs/oracle/db is -0 and 0 /home/training/20343546224544 is +0

    Change current working directory to /home/training/Documents/pdfs/techdocs/oracle/db is done like this.

    $ pushd -0
    ~/Documents/pdfs/techdocs/oracle/db ~/20343546224544 /usr/local/bin
    $ pwd
    /home/training/Documents/pdfs/techdocs/oracle/db
    

    Also see:
    builtin help pushd
    builtin help dirs
    builtin help popd



© Lightnetics 2022