[Linux Server] [bash] makes it easier to switch directories

Source: Internet
Author: User
Tags switches

This article reproduces: [Linux server][bash] makes it easier to switch directories:

One, why use these commands?
We may have questions about why we have to use these commands,
Wouldn't it be possible to switch directories with a CD?
Yes, you can switch to the directory you want to access by using a CD.
But sometimes it is a directory with a long path and a lot of layers, and when we go into this directory, we accidentally run the CD command,
Of course, we went back to our home directory, what if we want to go back?

Also: Because of the need for work, we need to constantly switch between a few very deep directories, more than one,
So even with the TAB key, we're going to spend a lot of time on a CD command, and it's easier to switch between multiple directories.
PUSHD,POPD is our good helper.

Description: Pushd,popd,dirs These are the commands we've talked about that are built in bash,
So as soon as you log in to bash, these commands can be used

Two, how to switch between two directories?

If you just switch between two directories, you don't have to use pushd,
Just use the CD.

CD-The function is to go back to the previous directory,
See Example:

[Email protected] ~]# cd/usr/share/kde4/apps/kget/pics/
[Email protected] pics]# CD-
/root
[Email protected] ~]# CD-
/usr/share/kde4/apps/kget/pics
[Email protected] pics]#

What do you think? Easy switching between two directories without the need to enter a long path

Description
Why CD-Can I go back to the previous directory?
This is because--here is the equivalent of $OLDPWD variable,
This issue can be found in bash info,
CD one has related instructions: an argument of-is equivalent to $OLDPWD.

$OLDPWD variable is the previous directory that bash records
In other words: $OLDPWD and-equal

Let's look at one more example:
[Email protected] ~]# cd/usr/share/kde4/apps/kget/pics/
[Email protected] pics]# echo $OLDPWD;
/root
[Email protected] pics]# CD $OLDPWD
[Email protected] ~]# echo $OLDPWD;
/usr/share/kde4/apps/kget/pics
[Email protected] ~]# CD $OLDPWD;
[Email protected] pics]# echo $OLDPWD;
/root

We can get the structure: use CD-and CD $OLDPWD to switch between the two most recently operated directories


Third, how to switch between multiple directories?
Because CD-and CD $OLDPWD are only two directories that support most recent operations, when you want to operate between multiple directories,
We need to use pushd

1,
Let's start with the 3 commands we're going to use.

PUSHD: Switches to the directory as a parameter and presses the original directory and the current directory into a virtual stack
If you do not specify a parameter, you will go back to the previous directory and swap the last two directories in the stack

POPD: Pop up the nearest directory in the stack
Dirs: Lists the list of directories saved in the current stack


See Example:
[Email protected] ~]# pushd/usr/local/sbin/
/usr/local/sbin ~
[Email protected] sbin]# dirs
/usr/local/sbin ~
[Email protected] sbin]# Dirs-p-V
0/usr/local/sbin
1 ~
[Email protected] sbin]# pushd/usr/share/kde4/apps/kget/
/usr/share/kde4/apps/kget/usr/local/sbin ~
[Email protected] kget]# Dirs-p-V
0/usr/share/kde4/apps/kget
1/usr/local/sbin
2 ~


Description: The-p parameter of dirs can display a list of directories in the stack in the form of one directory per row
The-v parameter can be numbered before the table of contents
Note: When there is-V, do not add-P can also be displayed as one directory per line
Description of the second: we can see: the most recently pressed into the stack of the directory on the top

2, how to switch between the last two directories?

Switch between the last two directories: Use pushd to add no parameters

[Email protected] kget]# pushd/boot/grub/
/boot/grub/usr/share/kde4/apps/kget/usr/local/sbin ~
[Email protected] grub]# dirs-v
0/boot/grub
1/usr/share/kde4/apps/kget
2/usr/local/sbin
3 ~
[Email protected] grub]# pushd
/usr/share/kde4/apps/kget/boot/grub/usr/local/sbin ~
[Email protected] kget]# dirs-v
0/usr/share/kde4/apps/kget
1/boot/grub
2/usr/local/sbin
3 ~
[Email protected] kget]# pushd
/boot/grub/usr/share/kde4/apps/kget/usr/local/sbin ~
[Email protected] grub]# dirs-v
0/boot/grub
1/usr/share/kde4/apps/kget
2/usr/local/sbin
3 ~

Note: You can see that when you switch between the last two directories with pushd without parameters,
The current directory is always on top of the stack

3, how can I switch between multiple directories?

You can use pushd +n
Description
N is a number that, when you have this parameter, switches to the nth directory in the stack and pushes the directory to the top of the stack in a stack loop
Note: Stacks start at the beginning of the No. 0

See Example:

[Email protected] grub]# dirs-v
0/boot/grub
1/usr/share/kde4/apps/kget
2/usr/local/sbin
3 ~
[Email protected] grub]# pushd +2
/usr/local/sbin ~/boot/grub/usr/share/kde4/apps/kget
[Email protected] sbin]# dirs-v
0/usr/local/sbin
1 ~
2/boot/grub
3/usr/share/kde4/apps/kget

4, how to remove the directory from the stack?
You can use POPD

See Example:
[Email protected] sbin]# dirs-v
0/usr/local/sbin
1 ~
2/boot/grub
3/usr/share/kde4/apps/kget
[Email protected] sbin]# popd
~/boot/grub/usr/share/kde4/apps/kget
[Email protected] ~]# dirs-v
0 ~
1/boot/grub
2/usr/share/kde4/apps/kget

[Email protected] ~]# popd +1
~/usr/share/kde4/apps/kget
[Email protected] ~]# dirs-v
0 ~
1/usr/share/kde4/apps/kget

Note: You can see the popd operation without parameters:
POPD Remove the directory at the top of the stack from the stack and switch to the directory at the top of the new
Description bis: When the popd is added with the parameter +n,
n is the nth directory in the stack, which means that the nth directory in the stack is removed from the stack


Four, learn a little more knowledge

Both 1,pushd and popd can affect the stack only and do not switch directories
You can use the-n parameter

See Example:
[Email protected] ~]# dirs-v
0 ~
1/usr/share/kde4/apps/kget
[Email protected] ~]# pushd-n/boot/grub
~/boot/grub/usr/share/kde4/apps/kget
[Email protected] ~]# dirs-v
0 ~
1/boot/grub
2/usr/share/kde4/apps/kget

2, dirs can empty the directory stack
You can use the-c parameter

See Example:
[Email protected] ~]# dirs-v
0 ~
1/boot/grub
2/usr/share/kde4/apps/kget
[Email protected] ~]# dirs-c
[Email protected] ~]# dirs-v
0 ~

Description: The directory at the top of the stack is the current directory, it cannot be popped out of the

[Linux Server] [bash] makes it easier to switch directories

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.