popd linux_如何在Linux上使用Push和Popd

popd linux

popd linux

A terminal window on a Ubuntu-style Linux desktop.
Fatmawati Achmad Zaenuri/Shutterstock Fatmawati Achmad Zaenuri / Shutterstock

Many Linux folks have never heard of pushd and popd, but they’ve been around forever. They can also dramatically speed up the process of navigating directories on the command line. We’ll walk you through how to use them.

许多Linux人士从未听说过pushdpopd ,但是他们已经永远存在了。 它们还可以极大地加快在命令行上浏览目录的过程。 我们将指导您如何使用它们。

什么被推动和弹出? (What Are pushd and popd?)

One of the innovations Bill Joy incorporated in his 1978 C Shell was the concept of a directory stack and the means to manipulate it: pushd and popd. Imitation being the sincerest form of flattery, the directory stack, pushd, and popd were soon incorporated into other shells (like Bash) and even other operating systems.

Bill Joy在1978年的C Shell中进行的一项创新就是目录堆栈的概念以及对其进行操作的方法: pushdpopd 。 仿被吹捧,目录栈,最真诚的pushd ,和popd很快并入其它壳(如击),甚至其他操作系统。

The concept of the stack is a simple one. Items are placed on the stack one at a time, with the most recently added item always occupying the top position. When items are retrieved from the stack, they’re removed, in order, from the top downward. Stacks of this nature are often referred to as Last In, First Out (LIFO) queues.

堆栈的概念很简单。 将项目一次放置在堆栈上,最近添加的项目始终占据最高位置。 从堆栈中检索项目时,会从上到下依次删除它们。 具有这种性质的堆栈通常称为后进先出(LIFO)队列。

Actually, pushd and popd are a little more flexible than this, but this is a good model to keep in mind for now.

实际上, pushdpopd比这要灵活一些,但这是目前要牢记的一个好模型。

As we’re referring to a directory stack, it probably comes as no surprise that the “d” in pushd and popd stands for “directory.” These commands allow you to push directories onto, or pop them off of, the directory stack.

正如我们指的是一个目录栈,它可能并不令人吃惊的是,“d”中pushdpopd代表“目录”。 这些命令允许您将目录推送到目录堆栈上或从目录堆栈中弹出。

But how does that benefit us?

但是,这对我们有何好处?

如何推动填充堆栈 (How pushd Populates the Stack)

When you use pushd, the following three things happen:

使用pushd ,会发生以下三件事:

  • You change the directory the same as if you’d used cd.

    您可以像使用cd一样更改目录。

  • The name and path of the directory are added to the stack.

    目录的名称和路径将添加到堆栈中。
  • The stack is displayed as a space-separated list of directories.

    堆栈显示为以空格分隔的目录列表。

In the following examples, note how the directory stack grows with each new pushd command. Also note that the top of the stack is to the left—this is where the new entries appear.

在以下示例中,请注意目录堆栈如何随每个新的pushd命令而增长。 还要注意,堆栈的顶部在左侧-这是新条目出现的位置。

After the first pushd command, there are two entries in the stack: the directory you left, and the one to which you moved.

第一后pushd命令,有两个项目在栈:你离开了目录,以及您移动的一个。

For our example, we type the following:

对于我们的示例,我们键入以下内容:

pushd ~/Desktop
pushd ~/Music
pushd ~/Documents
pushd ~/Pictures
pushd ~
The "pushd ~/Desktop," "pushd ~/Music," "pushd ~/Documents," "pushd ~/Pictures," and "pushd ~" commands in a terminal window.

The last pushd command took us back to our home directory, so the first and last entries in the stack are the tilde (~), which represents our home directory. This shows that, although a directory is already in the stack, it will be added again for other pushd commands.

最后一个pushd命令将我们带回到主目录,因此堆栈中的第一个和最后一个条目是波浪号( ~ ),它代表我们的主目录。 这表明,尽管目录已经在堆栈中,但将为其他pushd命令再次添加该目录。

Note also that the left-most entry in the stack, which is most recently added entry, is your current directory.

还请注意,堆栈中最左边的条目(即最近添加的条目)是您的当前目录。

dirs命令 (The dirs Command)

You can use the dirs command, as shown below, to display the directory stack:

您可以使用dirs命令,如下所示,以显示目录堆栈:

dirs
The "dirs" in a terminal window.

It doesn’t affect the stack, it just displays it. Some of the options you can use with pushd refer to the position of the directories in the stack.

它不影响堆栈,仅显示它。 您可以在pushd使用的某些选项是指目录在堆栈中的位置。

If you want to see the numeric position of each directory, you can use the -v (vertical) option as shown below:

如果要查看每个目录的数字位置,可以使用-v (垂直)选项,如下所示:

dirs -v

目录-v

The "dirs -v" command in a terminal window.

If you’d rather see the spelled-out path to your home directory instead of the tilde (~), add the -l (long format) option, like so:

如果您希望看到主目录的拼写路径而不是波浪号( ~ ),请添加-l (长格式)选项,如下所示:

dirs -v -l
The "dirs -v -l" command in a terminal window.

将目录添加到堆栈 (Adding a Directory to the Stack)

As we’ve seen, when you use the pushd command, it does three things: changes your directory, adds the new directory to the stack, and displays the stack for you. You can use the -n (no rotation) option to add a directory to the stack without changing the current directory.

如我们所见,当您使用pushd命令时,它会执行三件事:更改目录,将新目录添加到堆栈中,并为您显示堆栈。 您可以使用-n (不旋转)选项将目录添加到堆栈中,而无需更改当前目录。

Here’s our directory stack:

这是我们的目录堆栈:

dirs -v -l
The "dirs -v -l" command in a terminal window.

Now, we’ll use the pushd command with the -n option and pas in the /home/dave directory as a parameter. Then, we’ll check the directory stack again.

现在,我们将在pushd选项和/home/dave目录中使用带有-n选项和pas的pushd命令作为参数。 然后,我们将再次检查目录堆栈。

We type the following:

我们输入以下内容:

pushd -n /home/dave
dirs -v -l
The "pushd -n /home/dave" and "dirs -v -l" commands in a terminal window.

The  /home/dave directory was added to the stack in slot 1, which is the second place in the stack. It can’t occupy the top position because slot zero is always the current directory.

/home/dave目录已添加到堆栈中第二个位置的插槽1中。 它不能占据最高位置,因为插槽零始终是当前目录。

We didn’t leave the current directory, ~/Videos, so it wasn’t rotated to another position in the stack.

我们没有离开当前目录~/Videos ,因此它没有旋转到堆栈中的另一个位置。

通过旋转堆栈更改目录 (Changing Directory by Rotating the Stack)

You can use numeric parameters with pushd to move to any directory in a stack, and the stack rotates when you do so. The directory you’ve chosen to move then becomes the first entry in the stack.

您可以使用数字参数与pushd在这样做时移动到堆栈中的任何目录和堆栈旋转。 然后,您选择移动的目录将成为堆栈中的第一个条目。

You reference the directories in the stack by their position number. You can count from the top or bottom of the stack. For positive numbers, such as +3, count from the top; for negative numbers, such as -2, count from the bottom.

您通过位置编号引用堆栈中的目录。 您可以从堆栈的顶部或底部进行计数。 对于正数(例如+3),请从顶部算起; 对于负数(例如-2),请从底部开始计数。

The /home/dave/Documents directory is in position three. We can use the following command to move that directory:

/ home / dave / Documents目录位于第三位置。 我们可以使用以下命令移动该目录:

pushd +3
The "pushd +3" command in a terminal window.

The directories in the stack above the directory we’ve chosen are moved to the bottom of the stack. Our chosen directory now occupies the top position and we’re moved into that directory.

我们选择的目录上方的堆栈中的目录将移至堆栈的底部。 现在,我们选择的目录占据了最高位置,我们被移入了该目录。

If we want to change into the directory at the bottom of the stack, we can use the following command:

如果要更改到堆栈底部的目录,可以使用以下命令:

pushd -0
The "pushd -0" command in a terminal window.

The last directory is moved to the first slot, and all the others are moved down in the stack. We’re changed to the ~/Pictures directory.

最后一个目录移动到第一个插槽,所有其他目录在堆栈中向下移动。 我们已更改为~/Pictures目录。

popd命令 (The popd Command)

You can use the popd command to remove directories from the stack.

您可以使用popd命令从堆栈中删除目录。

If we look at the directory stack, we can see that the directory in position 1 is /home/dave. To remove this from the stack, we type the following to pass the number to popd:

如果查看目录堆栈,可以看到位置1的目录是/home/dave 。 要将其从堆栈中删除,我们键入以下内容将数字传递给popd

dirs -v -l
popd +1
The "dirs -v -l" and "popd +1" commands in a terminal window.

The /home/dave directory was removed, and those that were below it in the stack have each moved up one place.

/home/dave目录已删除,并且堆栈中位于其下方的目录都向上移动了一个位置。

Just as we can with pushd, we can count from the bottom of the stack with popd. To remove the last directory from the stack, we type:

正如我们能与pushd ,我们可以从堆栈底部数popd 。 要从堆栈中删除最后一个目录,我们输入:

popd -0
The "popd -0" command in a terminal window.

The ~/Music directory is removed from the last position in the stack.

~/Music目录将从堆栈中的最后一个位置删除。

To change the directory, do something, and then hop back to the previous directory, you can use pushd and popd together.

要更改目录,请执行某些操作,然后跳回到上一个目录,可以同时使用pushdpopd

We’ll use pushd to move to a different directory. We’ll use popd to discard the topmost directory in the stack and move to the directory in the second position. This is the directory you just moved out of, so you’re dropped back into the directory you were originally in.

我们将使用pushd移至其他目录。 我们将使用popd放弃堆栈中最顶层的目录,然后移至第二个目录。 这是您刚刚移出的目录,因此您又回到了原来所在的目录中。

We type the following:

我们输入以下内容:

pushd ~
popd
The "pushd ~" and "popd" commands in a terminal window.

We started in the ~/Projects directory, pushd to the home directory, and then popd back to the ~/Projects directory.

我们开始在~/Projects目录, pushd主目录,然后popd回到~/Projects目录。

旋转整个堆栈 (Rotating Through the Entire Stack)

We’re going to illustrate how to rotate through a stack with some nested directories, but you could use any directories anywhere in the file system.

我们将说明如何在带有一些嵌套目录的堆栈中旋转,但是您可以在文件系统中的任何位置使用任何目录。

Our deepest level of nesting is:

我们最深层的嵌套是:

/home/dave/Projects/htg/articles

From the home directory, we’ll progressively descend through each directory until we reach the articles directory. Then, we’ll look at the directory stack.

从主目录开始,我们将逐步下降到每个目录,直到到达商品目录。 然后,我们来看目录堆栈。

We type the following:

我们输入以下内容:

pushd ~/Projects
pushd htg
pushd articles
dirs -v -l
The "pushd ~/Projects," "pushd htg," "pushd articles," and "dirs -v -l" commands in a terminal window.

When you repeatedly issue pushd +1 commands, you can cycle round and round through the stack of directories. If you do this often, pushd +1 would be a good candidate for an alias.

当您反复发出pushd +1命令时,可以在目录堆栈中循环pushd +1 。 如果您经常这样做,则pushd +1将是别名的理想选择。

Type the following:

输入以下内容:

pushd +1
The "pushd +1" command in a terminal window.

在堆栈上冲压 (Stamping Over the Stack)

It’s easy to revert to old habits and use cd to change directory. If you do that, you’ll stamp over the first directory in the stack. This is inevitable, as the first slot is reserved for the current working directory—none of the others change position.

恢复旧习惯并使用cd更改目录很容易。 如果这样做,则将标记堆栈中的第一个目录。 这是不可避免的,因为第一个插槽是为当前工作目录保留的,其他插槽都不会更改位置。

To do this, type the following:

为此,请键入以下内容:

dirs -v -l
cd ~/Music
dirs -v -l
The "dirs -v -l," "cd ~/Music," and "dirs -v -l" commands in a terminal window.


After you get used to the pushd and popd commands (and, perhaps, use them to create a few aliases), you’ll have a super-fast way to hop between directories.

在习惯了pushdpopd命令(也许还可以使用它们创建一些别名)之后,您将拥有一种在目录之间跳转的超快速方式。

This is why we hang around the command line. Efficiency rocks, right?

这就是为什么我们在命令行中徘徊。 效率高低,对不对?

翻译自: https://www.howtogeek.com/659146/how-to-use-pushd-and-popd-on-linux/

popd linux

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值