Switching Between Directories

In the last few examples, you moved between the /var and /usr/bin directories by specifying either relative or absolute paths. Switching back and forth between two directories is something you’ll do more often than you’d think. This has a nice shortcut command. Whenever you change to a new working directory, your shell stores the previous directory.

To switch back to the previous directory, use cd -. This command retrieves the value of the previous directory and uses it to change the directory.

Try this exercise out. First, go to your home directory. Then, navigate to the /var directory. Use cd - to switch back and forth between the two:

 $ ​​cd
 $ ​​cd​​ ​​/var
 $ ​​cd​​ ​​-
 /home/brian
 $ ​​cd​​ ​​-
 /var

When you use the cd - command, it automatically prints the new current working directory, so you don’t have to use the pwd command manually.

Now you can switch between two working directories with ease. But you may find yourself moving between a few locations. You can change your working directory in another way.

Using pushd and popd to Manage a Stack of Directories

The pushd command, short for “push directory,” changes your current working directory and adds it to a directory stack, or a list of directories. You can then view this stack or easily jump to an entry in the stack. Try it out. First, navigate to your home directory.

 $ ​​cd

Now use pushd to switch to the /var folder:

 $ ​​pushd​​ ​​/var
 /var ~

The pushd command switches your working directory, but it also displays the contents of the directory stack. The current directory is listed first, followed by the previous directory. Use pushd to navigate to the /tmp directory:

 $ ​​pushd​​ ​​/tmp
 /tmp /var ~

Your location changes, and the directory stack has a new entry at the front. Now switch to the /usr folder with pushd:

 $ ​​pushd​​ ​​/usr
 /usr /tmp /var ~

The popd command removes the first entry from the stack, and then changes your location to the new first entry:

 $ ​​popd
 /tmp /var ~

This places you in the /tmp directory now, and the stack reflects this. You can keep using popd to traverse backward through your previous locations.

Use pushd to navigate to the /usr/bin directory:

 $ ​​pushd​​ ​​/usr/bin
 /usr/bin /tmp /var ~

You can view the directory stack with the dirs command, and if you use the -v switch, you’ll see the index associated with each entry in the stack:

 $ ​​dirs​​ ​​-v
 0 /usr/bin
 1 /tmp
 2 /var
 3 ~

You can use these indexes to jump from one folder to another. Jump to the /var folder with:

 $ ​​pushd​​ ​​+2
 /var ~ /usr/bin /tmp
 $ ​​pwd
 /var

This moves the /var folder to the top of the stack. If you wanted to switch to the /tmp folder with pushd, what index would you use?

You’d use 3:

 $ ​​pushd​​ ​​+3
 /tmp /var ~ /usr/bin
 $ ​​pwd
 /tmp

Using pushd and an index doesn’t add or remove entries from the stack, but it does change the order of the entries. In fact, it rotates them.

You can also use the cd command to switch to an entry in the stack, but this does modify the stack. The command cd ~3 will take you to the /usr/bin directory:

 $ ​​cd​​ ​​~3
 $ ​​pwd
 /usr/bin

But look at the directory stack now:

 $ ​​dirs​​ ​​-v
» 0 /usr/bin
  1 /var
  2 ~
  3 /usr/bin

The entry for /tmp, which was the first entry in the stack, has been replaced with the new current directory.

Now, use the popd command to get back to your home directory. The first time you use it, you’ll navigate to /var. Use it again, and you’re back home:

 $ ​​popd
 /var ~ /usr/bin
 $ ​​popd
 ~ /usr/bin
 $ ​​pwd
 /home/brian

You’re probably wondering why there are so many different ways to do things, and the easiest answer is that this allows for more flexibility. In some situations using cd to change directories is faster, and in other cases, like when creating complex scripts to automate tasks, pushd and popd make more sense. Knowing how to use both methods, and understanding how they work, will let you choose what works best in a given situation.

Navigating around the filesystem is one thing, but knowing where files are is another. Let’s look at how to search for files and do something with the results.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.47.25