Skip to main content
clarify the starting point of the command chain
Source Link
AdminBee
  • 23.6k
  • 25
  • 56
  • 78

pushd, popd, and dirs are shell builtins which allow you manipulate the directory stack. This can be used to change directories but return to the directory from which you came.

For example

start up with the following directories:

$ pwd /home/saml/somedir $ ls dir1 dir2 dir3 $ pwd /home/saml/somedir 

pushd to dir1

$ pushd dir1 ~/somedir/dir1 ~/somedir $ dirs ~/somedir/dir1 ~/somedir 

dirs command confirms that we have 2 directories on the stack now. dir1 and the original dir, somedir. NOTE: Our "current" directory is ~/somedir/dir1.

pushd to ../dir3 (because we're inside dir1 now)

$ pushd ../dir3 ~/somedir/dir3 ~/somedir/dir1 ~/somedir $ dirs ~/somedir/dir3 ~/somedir/dir1 ~/somedir $ pwd /home/saml/somedir/dir3 

dirs shows we have 3 directories in the stack now. dir3, dir1, and somedir. Notice the direction. Every new directory is getting added to the left. When we start popping directories off, they'll come from the left as well.

manually change directories to ../dir2

$ cd ../dir2 $ pwd /home/saml/somedir/dir2 $ dirs ~/somedir/dir2 ~/somedir/dir1 ~/somedir 

Now start popping directories

$ popd ~/somedir/dir1 ~/somedir $ pwd /home/saml/somedir/dir1 

Notice we popped back to dir1.

Pop again...

$ popd ~/somedir $ pwd /home/saml/somedir 

And we're back where we started, somedir.

Might get a little confusing, but the head of the stack is the directory that you're currently in. Hence when we get back to somedir, even though dirs shows this:

$ dirs ~/somedir 

Our stack is in fact empty.

$ popd bash: popd: directory stack empty 

pushd, popd, and dirs are shell builtins which allow you manipulate the directory stack. This can be used to change directories but return to the directory from which you came.

For example

start up with the following directories:

$ ls dir1 dir2 dir3 $ pwd /home/saml/somedir 

pushd to dir1

$ pushd dir1 ~/somedir/dir1 ~/somedir $ dirs ~/somedir/dir1 ~/somedir 

dirs command confirms that we have 2 directories on the stack now. dir1 and the original dir, somedir. NOTE: Our "current" directory is ~/somedir/dir1.

pushd to ../dir3 (because we're inside dir1 now)

$ pushd ../dir3 ~/somedir/dir3 ~/somedir/dir1 ~/somedir $ dirs ~/somedir/dir3 ~/somedir/dir1 ~/somedir $ pwd /home/saml/somedir/dir3 

dirs shows we have 3 directories in the stack now. dir3, dir1, and somedir. Notice the direction. Every new directory is getting added to the left. When we start popping directories off, they'll come from the left as well.

manually change directories to ../dir2

$ cd ../dir2 $ pwd /home/saml/somedir/dir2 $ dirs ~/somedir/dir2 ~/somedir/dir1 ~/somedir 

Now start popping directories

$ popd ~/somedir/dir1 ~/somedir $ pwd /home/saml/somedir/dir1 

Notice we popped back to dir1.

Pop again...

$ popd ~/somedir $ pwd /home/saml/somedir 

And we're back where we started, somedir.

Might get a little confusing, but the head of the stack is the directory that you're currently in. Hence when we get back to somedir, even though dirs shows this:

$ dirs ~/somedir 

Our stack is in fact empty.

$ popd bash: popd: directory stack empty 

pushd, popd, and dirs are shell builtins which allow you manipulate the directory stack. This can be used to change directories but return to the directory from which you came.

For example

start up with the following directories:

$ pwd /home/saml/somedir $ ls dir1 dir2 dir3 

pushd to dir1

$ pushd dir1 ~/somedir/dir1 ~/somedir $ dirs ~/somedir/dir1 ~/somedir 

dirs command confirms that we have 2 directories on the stack now. dir1 and the original dir, somedir. NOTE: Our "current" directory is ~/somedir/dir1.

pushd to ../dir3 (because we're inside dir1 now)

$ pushd ../dir3 ~/somedir/dir3 ~/somedir/dir1 ~/somedir $ dirs ~/somedir/dir3 ~/somedir/dir1 ~/somedir $ pwd /home/saml/somedir/dir3 

dirs shows we have 3 directories in the stack now. dir3, dir1, and somedir. Notice the direction. Every new directory is getting added to the left. When we start popping directories off, they'll come from the left as well.

manually change directories to ../dir2

$ cd ../dir2 $ pwd /home/saml/somedir/dir2 $ dirs ~/somedir/dir2 ~/somedir/dir1 ~/somedir 

Now start popping directories

$ popd ~/somedir/dir1 ~/somedir $ pwd /home/saml/somedir/dir1 

Notice we popped back to dir1.

Pop again...

$ popd ~/somedir $ pwd /home/saml/somedir 

And we're back where we started, somedir.

Might get a little confusing, but the head of the stack is the directory that you're currently in. Hence when we get back to somedir, even though dirs shows this:

$ dirs ~/somedir 

Our stack is in fact empty.

$ popd bash: popd: directory stack empty 
clarify the starting point of the command chain
Source Link

pushd, popd, and dirs are shell builtins which allow you manipulate the directory stack. This can be used to change directories but return to the directory from which you came.

For example

start up with the following directories:

$ ls dir1 dir2 dir3 $ pwd /home/saml/somedir 

pushd to dir1

$ pushd dir1 ~/somedir/dir1 ~/somedir $ dirs ~/somedir/dir1 ~/somedir 

dirs command confirms that we have 2 directories on the stack now. dir1 and the original dir, somedir. NOTE: Our "current" directory is ~/somedir/dir1.

pushd to ../dir3 (because we're inside dir1 now)

$ pushd ../dir3 ~/somedir/dir3 ~/somedir/dir1 ~/somedir $ dirs ~/somedir/dir3 ~/somedir/dir1 ~/somedir $ pwd /home/saml/somedir/dir3 

dirs shows we have 3 directories in the stack now. dir3, dir1, and somedir. Notice the direction. Every new directory is getting added to the left. When we start popping directories off, they'll come from the left as well.

manually change directories to ../dir2

$ cd ../dir2 $ pwd /home/saml/somedir/dir2 $ dirs ~/somedir/dir2 ~/somedir/dir1 ~/somedir 

Now start popping directories

$ popd ~/somedir/dir1 ~/somedir $ pwd /home/saml/somedir/dir1 

Notice we popped back to dir1.

Pop again...

$ popd ~/somedir $ pwd /home/saml/somedir 

And we're back where we started, somedir.

Might get a little confusing, but the head of the stack is the directory that you're currently in. Hence when we get back to somedir, even though dirs shows this:

$ dirs ~/somedir 

Our stack is in fact empty.

$ popd bash: popd: directory stack empty 

pushd, popd, and dirs are shell builtins which allow you manipulate the directory stack. This can be used to change directories but return to the directory from which you came.

For example

start up with the following directories:

$ ls dir1 dir2 dir3 

pushd to dir1

$ pushd dir1 ~/somedir/dir1 ~/somedir $ dirs ~/somedir/dir1 ~/somedir 

dirs command confirms that we have 2 directories on the stack now. dir1 and the original dir, somedir. NOTE: Our "current" directory is ~/somedir/dir1.

pushd to ../dir3 (because we're inside dir1 now)

$ pushd ../dir3 ~/somedir/dir3 ~/somedir/dir1 ~/somedir $ dirs ~/somedir/dir3 ~/somedir/dir1 ~/somedir $ pwd /home/saml/somedir/dir3 

dirs shows we have 3 directories in the stack now. dir3, dir1, and somedir. Notice the direction. Every new directory is getting added to the left. When we start popping directories off, they'll come from the left as well.

manually change directories to ../dir2

$ cd ../dir2 $ pwd /home/saml/somedir/dir2 $ dirs ~/somedir/dir2 ~/somedir/dir1 ~/somedir 

Now start popping directories

$ popd ~/somedir/dir1 ~/somedir $ pwd /home/saml/somedir/dir1 

Notice we popped back to dir1.

Pop again...

$ popd ~/somedir $ pwd /home/saml/somedir 

And we're back where we started, somedir.

Might get a little confusing, but the head of the stack is the directory that you're currently in. Hence when we get back to somedir, even though dirs shows this:

$ dirs ~/somedir 

Our stack is in fact empty.

$ popd bash: popd: directory stack empty 

pushd, popd, and dirs are shell builtins which allow you manipulate the directory stack. This can be used to change directories but return to the directory from which you came.

For example

start up with the following directories:

$ ls dir1 dir2 dir3 $ pwd /home/saml/somedir 

pushd to dir1

$ pushd dir1 ~/somedir/dir1 ~/somedir $ dirs ~/somedir/dir1 ~/somedir 

dirs command confirms that we have 2 directories on the stack now. dir1 and the original dir, somedir. NOTE: Our "current" directory is ~/somedir/dir1.

pushd to ../dir3 (because we're inside dir1 now)

$ pushd ../dir3 ~/somedir/dir3 ~/somedir/dir1 ~/somedir $ dirs ~/somedir/dir3 ~/somedir/dir1 ~/somedir $ pwd /home/saml/somedir/dir3 

dirs shows we have 3 directories in the stack now. dir3, dir1, and somedir. Notice the direction. Every new directory is getting added to the left. When we start popping directories off, they'll come from the left as well.

manually change directories to ../dir2

$ cd ../dir2 $ pwd /home/saml/somedir/dir2 $ dirs ~/somedir/dir2 ~/somedir/dir1 ~/somedir 

Now start popping directories

$ popd ~/somedir/dir1 ~/somedir $ pwd /home/saml/somedir/dir1 

Notice we popped back to dir1.

Pop again...

$ popd ~/somedir $ pwd /home/saml/somedir 

And we're back where we started, somedir.

Might get a little confusing, but the head of the stack is the directory that you're currently in. Hence when we get back to somedir, even though dirs shows this:

$ dirs ~/somedir 

Our stack is in fact empty.

$ popd bash: popd: directory stack empty 
attempt explain it does a cd
Source Link
slm
  • 380.2k
  • 127
  • 793
  • 897

pushd, popd, and dirs are shell builtins which allow you manipulate the directory stack. This can be used to change directories but return to the directory from which you came.

For example

start up with the following directories:

$ ls dir1 dir2 dir3 

pushd to dir1

$ pushd dir1 ~/somedir/dir1 ~/somedir $ dirs ~/somedir/dir1 ~/somedir 

dirs command confirms that we have 2 directories on the stack now. dir1 and the original dir, somedir. Our NOTE: Our "current" directory is ~/somedir/dir1 now~/somedir/dir1.

pushd to ../dir3 (because we're inside dir1 now)

$ pushd ../dir3 ~/somedir/dir3 ~/somedir/dir1 ~/somedir $ dirs ~/somedir/dir3 ~/somedir/dir1 ~/somedir $ pwd /home/saml/somedir/dir3 

dirs shows we have 3 directories in the stack now. dir3, dir1, and somedir. Notice the direction. Every new directory is getting added to the left. When we start popingpopping directories off, they'll come from the left as well.

manually change directories to ../dir2

$ cd ../dir2 $ pwd /home/saml/somedir/dir2 $ dirs ~/somedir/dir2 ~/somedir/dir1 ~/somedir 

Now start popping directories

$ popd ~/somedir/dir1 ~/somedir $ pwd /home/saml/somedir/dir1 

Notice we popped back to dir1.

Pop again...

$ popd ~/somedir $ pwd /home/saml/somedir 

And we're back where we started, somedir.

Might get a little confusing, but the head of the stack is the directory that you're currently in. Hence when we get back to somedir, even though dirs shows this:

$ dirs ~/somedir 

Our stack is infactin fact empty.

$ popd bash: popd: directory stack empty 

pushd, popd, and dirs are shell builtins which allow you manipulate the directory stack. This can be used to change directories but return to the directory from which you came.

For example

start up with the following directories:

$ ls dir1 dir2 dir3 

pushd to dir1

$ pushd dir1 ~/somedir/dir1 ~/somedir $ dirs ~/somedir/dir1 ~/somedir 

dirs command confirms that we have 2 directories on the stack now. dir1 and the original dir, somedir. Our "current" directory is ~/somedir/dir1 now.

pushd to ../dir3 (because we're inside dir1 now)

$ pushd ../dir3 ~/somedir/dir3 ~/somedir/dir1 ~/somedir $ dirs ~/somedir/dir3 ~/somedir/dir1 ~/somedir $ pwd /home/saml/somedir/dir3 

dirs shows we have 3 directories in the stack now. dir3, dir1, and somedir. Notice the direction. Every new directory is getting added to the left. When we start poping directories off, they'll come from the left as well.

manually change directories to ../dir2

$ cd ../dir2 $ pwd /home/saml/somedir/dir2 $ dirs ~/somedir/dir2 ~/somedir/dir1 ~/somedir 

Now start popping directories

$ popd ~/somedir/dir1 ~/somedir $ pwd /home/saml/somedir/dir1 

Notice we popped back to dir1.

Pop again...

$ popd ~/somedir $ pwd /home/saml/somedir 

And we're back where we started, somedir.

Might get a little confusing, but the head of the stack is the directory that you're currently in. Hence when we get back to somedir, even though dirs shows this:

$ dirs ~/somedir 

Our stack is infact empty.

$ popd bash: popd: directory stack empty 

pushd, popd, and dirs are shell builtins which allow you manipulate the directory stack. This can be used to change directories but return to the directory from which you came.

For example

start up with the following directories:

$ ls dir1 dir2 dir3 

pushd to dir1

$ pushd dir1 ~/somedir/dir1 ~/somedir $ dirs ~/somedir/dir1 ~/somedir 

dirs command confirms that we have 2 directories on the stack now. dir1 and the original dir, somedir. NOTE: Our "current" directory is ~/somedir/dir1.

pushd to ../dir3 (because we're inside dir1 now)

$ pushd ../dir3 ~/somedir/dir3 ~/somedir/dir1 ~/somedir $ dirs ~/somedir/dir3 ~/somedir/dir1 ~/somedir $ pwd /home/saml/somedir/dir3 

dirs shows we have 3 directories in the stack now. dir3, dir1, and somedir. Notice the direction. Every new directory is getting added to the left. When we start popping directories off, they'll come from the left as well.

manually change directories to ../dir2

$ cd ../dir2 $ pwd /home/saml/somedir/dir2 $ dirs ~/somedir/dir2 ~/somedir/dir1 ~/somedir 

Now start popping directories

$ popd ~/somedir/dir1 ~/somedir $ pwd /home/saml/somedir/dir1 

Notice we popped back to dir1.

Pop again...

$ popd ~/somedir $ pwd /home/saml/somedir 

And we're back where we started, somedir.

Might get a little confusing, but the head of the stack is the directory that you're currently in. Hence when we get back to somedir, even though dirs shows this:

$ dirs ~/somedir 

Our stack is in fact empty.

$ popd bash: popd: directory stack empty 
Added command results that will illustrate the effect of manually changing directories
Source Link
Loading
Some copy edit, added link to related documentation.
Source Link
Loading
Source Link
slm
  • 380.2k
  • 127
  • 793
  • 897
Loading