In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will take you to understand what the solution for efficient directory switching in the Linux system is. The knowledge points in the article are very detailed. Friends who think it is helpful can browse the content of the article together with the editor, hoping to help more friends who want to solve this problem to find the answer to the problem. Let's follow the editor to learn more about "how to efficiently switch directories in the Linux system."
In the Linux system, everything is a file, so the directory structure of the Linux system is very complex, if you simply use the cd command to switch between directories will be very troublesome.
To introduce you to three efficient commands to change directories: pushd, popd, dirs.
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-v 0 ~ / test/dir2 1 ~ / test/dir1 2 ~ / test/dir3 3 ~ / test Please note that the top element is always the same as the current directory, and if you look at the directory stack in other directories, 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-v 0 ~ / 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 ~ / test [alvin@VM_0_16_centos dir1] $pushd.. / dir2 ~ / test/dir2 ~ / test/dir1 ~ / test [alvin@VM_0_16_centos dir2] $pushd.. / dir3 ~ / test/dir3 ~ / test/dir2 ~ / test/dir1 ~ / test [alvin@VM_0_16_centos dir3] $dirs-v 0 ~ / test/dir3 1 ~ / test/dir2 2 ~ / test/dir1 3 ~ / 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-v 0 ~ / test/dir3 1 ~ / test/dir2 2 ~ / test/dir1 3 ~ / 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 ~ / test [alvin@VM_0_16_centos dir2] $pwd / home/alvin/test/dir2 # correspondence Record changes [alvin@VM_0_16_centos dir2] $dirs-v 0 ~ / test/dir2 1 ~ / test/dir3 # Index 0 and 1 switch 2 ~ / test/dir1 3 ~ / 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-v 0 ~ / test/dir2 1 ~ / test/dir3 2 ~ / test/dir1 3 ~ / test [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-v 0 ~ / test/dir1 1 ~ / test 2 ~ / test/dir2 3 ~ / 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-v 0 ~ / test/dir3 1 ~ / test/dir1 2 / test 3 ~ / test/dir2 [alvin@VM_0_16_centos dir3] $popd ~ / test/dir1 ~ / test ~ / test/dir2 [alvin@VM_0_16_centos dir1] $dirs-v 0 ~ / test/dir1 1 ~ / test 2 ~ / 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-v 0 ~ / test/dir1 1 ~ / test 2 ~ / test/dir2 [alvin@VM_0_16_centos dir1] $popd + 1 ~ / test/dir1 ~ / test/dir2 [alvin@VM_0_16_centos dir1] $dirs-v 0 ~ / test/dir1 1 ~ / test/dir2 what is the Linux system Linux is a free and freely distributed UNIX-like operating system Is a POSIX-based multi-user, multi-tasking, multi-threaded and multi-CPU operating system, using Linux to run major Unix tools, applications and network protocols.
Thank you for your reading. The above is the whole content of "how is the solution for efficient directory switching in Linux system". Friends who learn to learn to do it quickly. I believe that the editor will certainly bring you better quality articles. Thank you for your support to the website!
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: 0
*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.