Computer >> 컴퓨터 >  >> 체계 >> Linux

pwd 명령으로 디렉토리 검색 방법

Linux 명령줄 인터페이스를 사용할 때 배우는 가장 중요한 명령 중 하나는 pwd 입니다. 작업 디렉토리 인쇄를 나타내는 명령입니다.

이 가이드는 pwd를 사용하는 방법을 보여줍니다. 명령. 또한 작업 중인 디렉터리의 실제 경로와 작업 중인 논리 디렉터리를 찾는 방법도 보여줍니다.

현재 어떤 Linux 디렉토리에 있는지 확인하는 방법

현재 어떤 디렉토리에 있는지 확인하려면 명령줄을 열고 다음 명령을 실행하세요.

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.