Linux pwd Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the pwd command in Linux, which stands for "Print Working Directory". The lab covers the purpose of the pwd command, how to explore it in different directories, and how to combine it with other Linux commands to perform various tasks. The lab provides practical examples and step-by-step instructions to help you understand and effectively use the pwd command in your Linux environment.

The lab covers the following topics:

  1. Understand the Purpose of the pwd Command
  2. Explore the pwd Command in Different Directories
  3. Combine pwd with Other Linux Commands

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/FileandDirectoryManagementGroup -.-> linux/pwd("`Directory Displaying`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/BasicFileOperationsGroup -.-> linux/touch("`File Creating/Updating`") subgraph Lab Skills linux/cd -.-> lab-422870{{"`Linux pwd Command with Practical Examples`"}} linux/pwd -.-> lab-422870{{"`Linux pwd Command with Practical Examples`"}} linux/ls -.-> lab-422870{{"`Linux pwd Command with Practical Examples`"}} linux/touch -.-> lab-422870{{"`Linux pwd Command with Practical Examples`"}} end

Understand the Purpose of the pwd Command

In this step, you will learn about the purpose of the pwd command in Linux. The pwd command stands for "Print Working Directory" and is used to display the current working directory.

To use the pwd command, simply type pwd in the terminal:

pwd

Example output:

/home/labex/project

The output shows that the current working directory is /home/labex/project. This is the directory where the user is currently located.

The pwd command is useful when you need to know your current location in the file system. It can be used in combination with other commands to perform various tasks, such as navigating to a specific directory or executing commands in the current directory.

Explore the pwd Command in Different Directories

In this step, you will explore the behavior of the pwd command when you navigate to different directories.

First, let's create a new directory and navigate to it:

mkdir ~/project/subdirectory
cd ~/project/subdirectory

Now, run the pwd command again:

pwd

Example output:

/home/labex/project/subdirectory

As you can see, the output of the pwd command has changed to reflect the new current working directory.

Next, let's go back to the parent directory:

cd ..
pwd

Example output:

/home/labex/project

The pwd command now shows that we are back in the parent directory, /home/labex/project.

Finally, let's navigate to the root directory:

cd /
pwd

Example output:

/

The pwd command now shows that we are in the root directory, /.

The pwd command always displays the full path of the current working directory, regardless of where you are in the file system. This makes it a useful tool for keeping track of your location and navigating the file system.

Combine pwd with Other Linux Commands

In this final step, you will learn how to combine the pwd command with other Linux commands to perform more complex tasks.

One common use case is to use pwd to specify the current working directory when executing other commands. For example, let's create a new file in the current directory:

touch $(pwd)/new_file.txt

This will create a new file named new_file.txt in the current working directory, which is /home/labex/project.

You can also use pwd to navigate to a specific directory and then execute a command. For instance, let's navigate to the root directory and list the contents:

cd /
ls -l $(pwd)

Example output:

total 60
drwxr-xr-x   2 root root  4096 Apr 28 06:54 bin
drwxr-xr-x   3 root root  4096 Apr 28 06:54 boot
drwxr-xr-x   5 root root  4096 May  2 06:16 dev
drwxr-xr-x  92 root root  4096 May  2 06:16 etc
drwxr-xr-x   3 root root  4096 Apr 28 06:54 home
lrwxrwxrwx   1 root root    33 Apr 28 06:54 initrd.img -> /boot/initrd.img-5.15.0-60-generic
drwxr-xr-x  20 root root  4096 Apr 28 06:54 lib
...

In this example, we first navigate to the root directory using cd /, and then use pwd to specify the directory to list the contents of using ls -l.

Combining pwd with other commands can be a powerful way to automate tasks and work more efficiently in the Linux command line.

Summary

In this lab, you learned about the purpose of the pwd command in Linux, which stands for "Print Working Directory" and is used to display the current working directory. You explored the behavior of the pwd command when navigating to different directories, observing how the output changes to reflect the current location in the file system. Finally, you learned how to combine the pwd command with other Linux commands to perform various tasks, such as navigating to a specific directory or executing commands in the current directory.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like