Computer >> Máy Tính >  >> Hệ thống >> Linux

Cách khám phá thư mục của bạn bằng lệnh pwd

Một trong những lệnh quan trọng nhất bạn học được khi sử dụng giao diện dòng lệnh Linux là pwd lệnh, viết tắt của thư mục làm việc in.

Hướng dẫn này chỉ cho bạn cách sử dụng pwd yêu cầu. Nó cũng chỉ cho bạn cách tìm đường dẫn vật lý đến thư mục bạn đang làm việc và thư mục logic mà bạn đang làm việc.

Cách xác định bạn đang sử dụng thư mục Linux nào

Để biết bạn hiện đang ở thư mục nào, hãy mở một dòng lệnh và chạy lệnh sau:

pwd

The output for the pwd command will be something like this:

/home/gary

As you move around the system, the working directory changes to reflect your current position within the file system.

For example, if you use the cd command to navigate to the documents folder, the pwd command produces the following output:

/home/gary/documents

What Does pwd Show When You Navigate to a Symbolically Linked Folder?

To answer this question, we set up the following scenario.

Imagine that you have the following folder structure:

  • home
    • gary
      • documents
        • folder1
        • folder2

Now imagine that you created a symbolic link to folder 2, as follows:

ln -s /home/gary/documents/folder1 /home/gary/documents/accounts

The folder tree would now look like this:

  • home
    • gary
      • documents
        • folder1
        • folder2
        • accounts

The ls command shows the files and folders within a particular location:

ls -lt 

If you ran the ls command against your documents folder, for accounts it would show something like this:

accounts -> folder2

Symbolic links point to another location within the file system.

Now imagine that you're in the documents folder and you use the cd command to move into the accounts folder. What would the output of pwd will be?

If you guessed that it would show /home/gary/documents/accounts, then you'd be correct. But, if you ran the ls command against the accounts folder, it shows you the files within the folder2 folder.

Look at the following command:

pwd -P

When you run the above command within a symbolically linked folder, you see the physical location, which in this case is /home/gary/documents/folder2.

To see the logical folder, you can use the following command:

pwd -L

This command would show the same folder as the pwd command on its own, which is /home/gary/documents/accounts.

Whether the command defaults to the physical path or the logical path depends on how you've set up and compiled pwd on your system. Therefore, best practice is to use the -P or -L switch (depending on which behavior you want to see).

How to Use the $PWD Variable

You can view the current working directory by displaying the value of the $PWD variable, as follows:

echo $PWD

How to Display the Previous Working Directory

If you want to view the previous working directory, run the following command:

echo $OLDPWD

The output displays the directory you were in before you moved to the current directory.

Multiple Occurrences of pwd

The pwd command may behave differently based on how you set it up. A good example is in Kubuntu Linux.

The shell version of pwd, which you use when you run the pwd command, shows the logical working directory when you're within a symbolically linked folder. If you run the following command, however, you'll see that it shows the physical working directory when you're within a symbolically linked folder:

/usr/bin/pwd

This output isn't helpful: You're essentially running the same command but getting the reverse result when you run it in a default mode. That's why it's good to get into the habit of using the -P and -L switches.

Helpful Switches With pwd

Two further switches are helpful with the pwd command. The first:

pwd --version

...displays the current version number for pwd.

When run against the shell version of pwd, the --version switch may not work. It will, however, work against the /bin/pwd version.

The other switch:

pwd --help

...displays the manual page to the terminal window.

Again, this switch doesn't work for the shell version of pwd, only against the /bin/pwd version.