Table of Contents
pushd and popd command basics" >pushd and popd command basics
Some advanced usage" >Some advanced usage
总结" >总结
Home System Tutorial LINUX Tips on using command line tools in Linux systems (2)

Tips on using command line tools in Linux systems (2)

Feb 09, 2024 pm 04:40 PM
linux linux tutorial linux system linux command shell script embeddedlinux Getting started with linux linux learning

In the first part of this series of articles, we focused on command line navigation in Linux and discussed some related points/concepts by exploring the use of the cd – command. Now, we'll take a closer look at how to use the pushd and popd commands for a faster navigation experience in the Linux command line.

Tips on using command line tools in Linux systems (2)

Before we begin, it’s worth stating that all instructions and commands mentioned hereafter have been tested on Ubuntu 14.04 and the Bash shell (4.3.11).

pushd and popd command basics

To better understand the role of pushd and popd commands, let us first discuss the concept of stack. Imagine you have an empty space on your kitchen countertop and you want to place a set of dishes on it. What do you do? Simple, put them on top one after the other.

So at the end of the whole process, the first plate on the chopping board is the last plate in the pile, and the last plate in your hand is the first plate in the pile. Now when you need a plate, you select the one at the top of the pile and use it, then select the next one when needed.

pushd and popd commands are similar concepts. On Linux systems there is a directory stack where you can stack directory paths for future use. You can use the dirs command to quickly view the contents of the stack at any point in time.

The following example shows the output of using the dirs command on my system immediately after starting the command line terminal:

$ dirs
 ~
Copy after login

The tilde (~) in the output indicates that the directory stack currently contains only the user's home directory.

Go ahead and use the pushd and popd commands to store the directory path and delete it. Using pushd is very easy - just pass the path to be stored in the directory stack as an argument to this command. Here is an example:

pushd /home/himanshu/Downloads/
Copy after login

The above command changes the current working directory to the directory you pass as a parameter, and also adds the path to the directory stack. For user convenience, the pushd command produces the contents of the directory stack in its output. Therefore, when running the above command, the following output is produced:
~/Downloads ~
The output shows that there are now two directory paths in the stack: one is the user's home directory, and the user's download directory. They are saved in the order that the main directory is at the bottom and the newly added Downloads directory is above it.

To verify that the output of pushd is correct, you can also use the dirs command:

$ dirs
~/Downloads ~
Copy after login

So you can see that the dirs command also produces the same output.

Let’s use the pushd command again:

$ pushd /usr/lib/; pushd /home/himanshu/Desktop/
Copy after login

/usr/lib ~/Downloads ~

~/Desktop /usr/lib ~/Downloads ~

So the directory stack now contains a total of four directory paths, with the home directory (~) at the bottom and the user's desktop directory at the top.

Be sure to remember that the head of the stack is your current directory. This means our current working directory is now ~/Desktop.

Now, let's say you want to get back to the /usr/lib directory, so all you have to do is execute the popd command:

$ popd
/usr/lib ~/Downloads ~
Copy after login

The popd command not only changes the current directory to /usr/lib, it also removes ~/Desktop from the directory stack, as you can see from the command output. This way, the popd command will allow you to browse the directories in reverse order.

Some advanced usage

Now that we have discussed the basics of pushd and popd commands, let us move on to discuss some other details related to these commands. First, these commands also allow you to manipulate directory stacks. For example, let's say your directory stack looks like this:

$ dirs
~/Desktop /usr/lib ~ ~/Downloads
Copy after login

Now, our requirement is to change the order of directory paths in the stack, with the topmost element (~/Desktop) being moved to the bottom, and each of the remaining elements being moved up one position. This can be achieved using the following command:

pushd +1
Copy after login

The result of the above command on the directory stack:

$ dirs
/usr/lib ~ ~/Downloads ~/Desktop
Copy after login

So we see that the order of elements in the directory stack has changed and is now the same as we wanted. Of course, you can move the directory stack elements any number of times. For example, the following command will move them up twice:

$ pushd +2
~/Downloads ~/Desktop /usr/lib ~
Copy after login

You can also use negative index values:

$ pushd -1
/usr/lib ~ ~/Downloads ~/Desktop
Copy after login

Similarly, you can use this technique with the popd command to remove any entry from the directory stack without leaving the current working directory. For example, if you want to use popd to remove the third entry from the top (currently ~/Downloads), you would run the following command:

popd +2
Copy after login

Remember that the initial value of the stack index is 0, so we use 2 to access the third entry.

So the directory stack now contains:

$ dirs

/usr/lib ~ ~/Desktop
Copy after login

Confirm that the entry has been removed.

如果由于某些原因,你发现你很难记住元素在目录堆栈中的位置以及它们的索引,你则可以对在 dirs 命令中使用 -v 选项。这里有一个例子:

$ dirs -v
0 /usr/lib
1 ~
2 ~/Desktop
Copy after login

你可能已经猜到了,左边的数字是索引,接下来跟的是这个索引对应的目录路径。

注意: 在 dir 中使用 -c 选项清除目录堆栈。

现在让我们简要地讨论一下 popd 和 pushd 命令的实际用法。虽然它们第一眼看起来可能有点复杂,但是这些命令在编写 shell 脚本时会派上用场 – 你不需要记住你从哪里来;只要执行一下 popd,你就能回到你来的目录。

经验丰富的脚本编写者通常以以下方式使用这些命令:

popd >/dev/null 2>&1
Copy after login

上述命令确保 popd 保持静默(不产生任何输出)。同样,你也可以静默 pushd。

pushd 和 popd 命令也被 Linux 服务器管理员使用,他们通常在几个相同的目录之间移动。 在这里介绍了一些其他有用的使用场景。

总结

我同意 pushd 和 popd 的概念不是很直接。但是,它需要的只是一点练习 – 是的,你需要多实践。花一些时间在这些命令上,你就会开始喜欢它们,特别是当它们提供了方便时。

The above is the detailed content of Tips on using command line tools in Linux systems (2). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1262
29
C# Tutorial
1235
24
Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

vscode Previous Next Shortcut Key vscode Previous Next Shortcut Key Apr 15, 2025 pm 10:51 PM

VS Code One-step/Next step shortcut key usage: One-step (backward): Windows/Linux: Ctrl ←; macOS: Cmd ←Next step (forward): Windows/Linux: Ctrl →; macOS: Cmd →

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

How to run sublime after writing the code How to run sublime after writing the code Apr 16, 2025 am 08:51 AM

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

laravel installation code laravel installation code Apr 18, 2025 pm 12:30 PM

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

How to use VSCode How to use VSCode Apr 15, 2025 pm 11:21 PM

Visual Studio Code (VSCode) is a cross-platform, open source and free code editor developed by Microsoft. It is known for its lightweight, scalability and support for a wide range of programming languages. To install VSCode, please visit the official website to download and run the installer. When using VSCode, you can create new projects, edit code, debug code, navigate projects, expand VSCode, and manage settings. VSCode is available for Windows, macOS, and Linux, supports multiple programming languages ​​and provides various extensions through Marketplace. Its advantages include lightweight, scalability, extensive language support, rich features and version

See all articles