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

How to Execute Commands from Within a Shell Script

In bash, executing commands from a shell script can be a little intimidating at first and may require a leap of faith....
29/12/2020

tput, printf and shell expansions: how to create awesome outputs with bash scripts?

1. Why are good outputs so important in bash scripts? There are many, many times when you, as a system administrator, need...
28/12/2020

Ansible Archive and Unarchive

Ansible is a great tool to automate your configuration management. The benefit of Ansible is that you don’t need to set...
29/12/2020
Bài Viết

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

mua Proxy riêng ở đâu, và nó đem lại lợi ích gì cho người sử dụng
22/11/2022

Hướng dẫn sử dụng Proxy Helper Fakeip khi thuê proxy
21/11/2022

PROXY NUÔI TÀI KHOẢN FACEBOOK – KINH NGHIỆM FAKE IP – THUÊ PROXY GIÁ RẺ
14/11/2022

Mua Proxy Nuôi Zalo Giá Rẻ Tại Onet.com.vn
14/11/2022

BẢNG GIÁ MUA PROXY VIỆT NAM và PROXY US Onet.com.vn
14/11/2022