Linux shell pushd popd dirs Command __linux

Source: Internet
Author: User

Http://www.cnblogs.com/davidwang456/p/3784102.html

http://blog.csdn.net/yucan1001/article/details/8455757

http://blog.163.com/yangfan876@126/blog/static/806124562013720104712282/


1, dirs

1) function
Displays all records in the current directory stack (the dirs command with no parameters displays the records in the current directory stack)

2) Grammar
(1) Format: dirs [-CLPV] [+n] [-n]
(2) option
-C deletes all records in the catalog stack
-l display in full format
-P shows the way a directory row
-V A directory for each row to display the contents of the stack, the number before each directory
+n shows the nth directory from left to right, with the number starting at 0
-N Displays the nth day record from right to left, with the number starting at 0
Note: The dirs always displays the contents of the course, and then the content on the stack, even though the directory stack is empty, the dirs command still displays only the course directory

2, pushd

1) function
The pushd command is often used to add directories to the stack, to add records to the top of the directory stack, and to switch to the directory, and if the pushd command does not add any arguments, it will be located at the top 2 directory on the record stack.

2) Grammar
(1) Format: pushd [Directory |-n | +n] [-n]
(2) option
Directory to add the directory to the top of the stack, and execute the "CD directory", switch to the directory
+n moves the nth directory to the top of the stack (counting from the left, the number starts at 0)
-N Moves the nth directory to the top of the stack (counting from the right, the number starts at 0)
-N does not switch directories when the directory is in the stack

3, popd

1) function
POPD is used to delete records from the directory stack, and if the popd command does not add any arguments, first delete the topmost record of the directory stack and switch to the top directory in the deleted directory stack.

2) Grammar
(1) Format: popd [-n | +n] [-n]
(2) option
+n to delete the nth directory (from the left, the number starting from 0)
-N Deletes the nth directory (from the right number, the number starts with 0)
-N does not switch directories when the directory is out of the stack


Both Bourne again and TC shells allow users to store lists of directories they are using so that they can move easily between these directories. This directory listing is called a stack. It simulates the dishes used in a restaurant: usually put the plate on top of the stack or take the tray from the top of the stack, which creates a FILO stack.

1. dirs: Display Stack

The built-in command dirs displays the contents of the catalog stack. If you call Dirs when the directory stack is empty, it displays the name of the working directory.

$ dirs
~/literature

The built-in command dirs uses a character ~ to indicate the name of the household owner directory. The examples in the following sections assume that the user uses the directory structure given in Figure 8-2.

Figure 8-2 The directory structure used by the example

2. PUSHD: Push the catalogue into the stack

Using the built-in command pushd (push directory to the top of the stack) you can change the directory and add a new directory to the top of the stack. In addition to changing the directory, the built-in command pushd also displays the contents of the stack. Figure 8-3 illustrates the following example.

    $ pushd. /demo
~/demo ~/literature
$ pwd
/home/sam/demo
$ pushd. /names
~/names ~/demo ~/literature
$ pwd
/home/sam/names

Figure 8-3 Creating a directory Stack

When using PUSHD with no parameters, the pushd switches the top two directories on the stack and takes the new top directory (that is, the original secondary stack) as a new working directory (Figure 8-4):

$ pushd
~/demo ~/names ~/literature
$ pwd
/home/sam/demo    

Figure 8-4 Changing working directory using pushd

By using pushd, you can easily move back and forth between two directories. You can use cd– to switch to the previous directory, regardless of whether you explicitly create a directory stack. In order to access other directories in the directory stack, you can call pushd with a number that begins with a plus sign as an argument. The number of the directory on the stack begins at the top of the stack, and the top of the stack is numbered 0. The following PUSHD command continues with the previous example, changes the working directory to literature, and moves the literature to the top of the stack.

$ pushd +2 
~/literature ~/demo ~/names
$ pwd
/home/sam/literature

3. POPD: Pop the directory from the stack

Use the built-in command popd (pop directory, pop-up directory) to move the directory out of the stack. POPD with no parameters remove the top of the stack from the stack and change the working directory to the new top directory. This is shown in the following example and figure 8-5.

$ dirs
~/literature ~/demo ~/names
$ popd
~/demo ~/names
$ pwd
/home/sam/demo

Figure 8-5 Removing a directory from the stack using the popd command

To remove a directory other than the top of the stack, you can use a number that begins with a plus sign as a parameter to POPD. The following example moves directory number 1 (that is, demo) out:

$ dirs
~/literature ~/demo ~/names
$ popd +1 ~/literature
~/names   

Removing a directory with a label of not 0 does not change the working directory.


There are still such commands in the shell, which used to be really useful ... pushd and popd. First of these two commands is the Shell built-in command, pushd role is to create a directory stack, and the specified directory to add to the top of the stack, the corresponding popd is the pop-up stack top directory. PUSHD has several parameters, +n (plus n)-n (minus N) This is the stack rotation, n is a number, that is, the entire directory stack rotated several digits .... Seemingly ambiguous description, look at the following example:

[yangfan@dhcp-13-42/]$ pushd/home/yangfan/test
~/test/~ ~/kernel/home
[yangfan@dhcp-13-42 test]$-pushd-2< c2/>~ ~/kernel/home ~/test/
[yangfan@dhcp-13-42 ~]$ pushd +2/home ~/test
/~ ~/kernel

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.