In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to switch directories efficiently in Linux". In daily operation, I believe many people have doubts about how to switch directories efficiently in Linux. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "how to switch directories efficiently in Linux". Next, please follow the editor to study!
These three commands actually operate on the directory stack, and the directory stack is a stack structure that holds the directory, and the top of the stack structure always stores the current directory (hit the blackboard, key point!).
Students with a basic programming foundation know that stacks follow the principle of last-in, first-out. In other words, in the stack structure, the elements that enter the stack will leave the stack first.
After reviewing the basic concepts, let's elaborate on these three commands.
Show contents of directory stack: dirs
The first is dirs. This command is simple enough to display the contents of the directory stack. It has the following three common options:
Option meaning-p displays one record per line-v displays one record per row, while showing the record in the stack index-c empties the catalog stack
The difference between the-p and-v options is that the-v option displays the index of each record on the stack, which is exactly the same. If there is a catalog stack now, let's see what's in it:
[alvin@VM_0_16_centos dir2] $pwd/home/alvin/test/ Dir2 [Alvin @ VM_0_16_centos dir2] $dirs-v0 ~ / test/dir21 ~ / test/dir12 ~ / test/dir33 ~ / test
Note that the topmost element is always the same as the current directory, and if you look at the directory stack in another directory, the first element will change accordingly. Similarly, if you use the pushd and popd described later to manipulate the directory stack, the current directory will switch to the address corresponding to the first element of the directory stack.
If we want to empty the directory stack, just use the-c option.
[alvin@VM_0_16_centos diff] $dirs-c [Alvin @ VM_0_16_centos diff] $dirs-v0 ~ / projects/blogdemos/diff push into the directory stack: pushd
After each execution of the pushd command, a dirs command is executed by default to display the contents of the directory stack. The main uses of pushd are as follows:
(1) pushd + directory
If pushd is used directly with a directory, it changes to that directory and places the directory at the top of the directory stack. Example:
[alvin@VM_0_16_centos test] $pushd dir1~/test/dir1 ~ / Testt [Alvin @ VM_0_16_centos dir1] $pushd.. / dir2~/test/dir2 ~ / test/dir1 ~ / Testt [Alvin @ VM_0_16_centos dir2] $pushd.. / dir3~/test/dir3 ~ / test/dir2 ~ / test/dir1 ~ / Testt [alvin @ VM_0_16_centos dir3] $dirs-v0 ~ / test/dir31 ~ / test/dir22 ~ / test/dir13 ~ / test
(2) pushd (without any parameters)
The effect of pushd execution without any parameters is to swap the two directories at the top of the directory stack. As we emphasized earlier, the first element of the directory stack is related to the current directory, so when the first element changes, the current directory will switch accordingly, and vice versa.
[alvin@VM_0_16_centos dir3] $dirs-v0 ~ / test/dir31 ~ / test/dir22 ~ / test/dir13 ~ / pwd/home/alvin/test/ [Alvin @ VM_0_16_centos dir3] $pwd/home/alvin/test/ dir3 [Alvin @ VM_0_16_centos dir3] $pushd~/test/dir2 ~ / test/dir3 ~ / test/dir1 ~ / Testt [Alvin @ VM_0_16_centos dir2] $pwd/home/alvin/test/dir2 # changes the corresponding directory [alvin@VM_0_16_centos dir2] $dirs-v0 ~ / test/dir21 ~ / test/dir3 # Index 0 and 1 swap the contents of 2 ~ / test/dir13 ~ / test
(3) pushd + /-n
Pushd + /-n is to change directly to the directory of the corresponding index value. Note that you can use either the plus sign or the minus sign here. If it is a plus sign, it will be counted from top to bottom from the directory stack, while with a minus sign, it will be counted from the bottom to the top of the directory stack.
Then back to the question at the beginning of this article, what if we want to switch frequently between two or more directories with long paths?
First, we add these paths to the directory stack as pushd + directories
Then, use pushd + /-n to quickly switch between different directories. The specific demonstration is as follows:
[alvin@VM_0_16_centos dir2] $pwd/home/alvin/test/ dir2 [Alvin @ VM_0_16_centos dir2] $dirs-v0 ~ / test/dir21 ~ / test/dir32 ~ / test/dir13 ~ / Testt [alvin @ VM_0_16_centos dir2] $pushd + 2~/test/dir1 ~ / test ~ / test/dir2 ~ / test/ dir3 [alvin @ VM_0_16_centos dir1] $pwd/home/alvin/test/ dir1 [alvin @ VM_0_16_centos] Dir1] $dirs-v0 ~ / test/dir11 ~ / test2 ~ / test/dir23 ~ / test/dir3 pop-up directory stack: popd
After each execution of the popd command, a dirs command is executed by default to display the contents of the directory stack. The main uses of popd are as follows:
(1) popd (without any parameters)
The effect of popd execution without any parameters is to unstack the top element in the directory stack. At this point, the elements at the top of the stack change, and naturally the current directory will switch accordingly.
[alvin@VM_0_16_centos dir3] $dirs-v0 ~ / test/dir31 ~ / test/dir12 ~ / test3 ~ / test/ dir2 [alvin @ VM_0_16_centos dir3] $popd~/test/dir1 ~ / test ~ / test/ dir2 [alvin @ VM_0_16_centos dir1] $dirs-v0 ~ / test/dir11 ~ / test2 ~ / test/dir2
(2) popd + /-n
Delete the nth element in the directory stack. Similarly, the plus and minus sign means to count from the top down or from the bottom up.
[alvin@VM_0_16_centos dir1] $dirs-v0 ~ / test/dir11 ~ / test2 ~ / test/ dir2 [Alvin @ VM_0_16_centos dir1] $popd + 1~/test/dir1 ~ / test/ dir2 [Alvin @ VM_0_16_centos dir1] $dirs-v0 ~ / test/dir11 ~ / test/dir2, the study on "how to efficiently switch directories in Linux" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 289
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.