Bash `pushd` command

29/12/2020
The Linux users may need to switch between many directories for doing a particular task and it is a time consuming task for the user to change the directory location frequently from the terminal. If the previously visited file or folder path information can be stored or retrieved from the terminal, then the user will be able to navigate the file system easily. There is a command in bash to solve this issue. To store the current directory information in the stack before moving to another directory location, `pushd` command is used in bash. This command works on LIFO (Last In First Out) based. This means, the directory information will be stored at the end of the stack location.  How you can apply `pushd` command to navigate file system on Ubuntu is explained in this tutorial.

Syntax:

pushd
pushd [drive] path
  • When `pushd` command is used without any drive and path then the list of previously pushed directory path will display.
  • When `pushd` command is used with path only then the current working directory information will store in the stack.
  • When `pushd` command is used with driver and path then the driver information will store in the stack.

Example-1: Using pushd with path and without path

Run the following commands to get the current directory information and retrieve any previously stored directory information. If no `pushd` command is executed before then “no other directory” message will print for the first command. When` pushd` command will execute with “Videos” then two entries will store in the stack. These are Videos and home directory(~). if `pushd` command will execute again then the entries of the stack information will display and the directory will change by the last entry of the stack that is home directory.

$ pushd
$ pushd Videos
$ pushd

The similar output will appear after running the above commands.

Example-2: Using `pushd` with drive and path

`pushd` command can be used with the full path of any directory. The first command `pwd` will print the current working directory information. The second command will push the “Pictures” directory by using full path of this directory with `pushd` command and the current directory will be changed to “Pictures” folder.

$ pwd
$ pushd /home/Fahmida/Pictures

Example-3: Check `pushed` directory list

The following commands are used to display the directory information from the stack those are inserted by `pushd` command. `dirs.` command displays the inserted directory name of the stack and `dirs –v` command displays the directory name of the stack with index value.

$ dirs
$ dirs -v

Example-4: Use `pushd` with positive and negative directory index

The directory information can be pushed by using positive or negative index value with `pushd` command. This example shows the use of index in `pushd` command. The following command is used here to show the current stack information with index value.

$ dirs –v

The following command will change the current directory to the folder that exist in the index 1 of the stack. Here, Videos folder exists at the index 1. After executing the command, the current directory will Videos and the index order will be changed.

$ pushd +1

The following command will count the index value from the right and change the current directory to home directory according to the index value.

$ pushd -2

Conclusion

If the user pushes the directory to the stack by using `pushd` command then the user doesn’t need to re-type the directory information multiple times to switch from one directory to another. Hope, the reader will able to use `pushd` command properly after reading this tutorial.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Linux Screen Command Tutorial

In this lesson on Linux Screen Command, we will install and use various commands related to Screen command. The screen...
29/12/2020

BASH command output to the variable

Different types of bash commands need to be run from the terminal based on the user’s requirements. When the user runs...
29/12/2020

Bash string manipulation

In bash, not unlike any other programing language, a program lives to put things into buckets and name them for later use....
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

SỰ KHÁC BIỆT GIỮA RESIDENTIAL PROXY VÀ PROXY DATACENTER
17/02/2024

Mua Proxy v6 US Private chạy PRE, Face, Insta, Gmail
07/01/2024

Mua shadowsocks và hướng dẫn sữ dụng trên window
05/01/2024

Tại sao Proxy Socks lại được ưa chuộng hơn Proxy HTTP?
04/01/2024

Mua thuê proxy v4 nuôi zalo chất lượng cao, kinh nghiệm tránh quét tài khoản zalo
02/01/2024