This tutorial explains Linux “pwd” command, options and its usage with examples.
What is pwd Command?
In Unix-like and some other operating systems, the ‘pwd’ command (print working directory) is used to display the current working directory. It’s a built-in command in certain Unix shells like ‘sh’ and ‘bash’ and can be easily implemented using POSIX C functions like ‘getcwd()’ and ‘getwd()’.
Users who are familiar with MS-DOS or the Windows command prompt may type ‘cd’ alone to view the working directory. However, in Linux and Unix, typing ‘cd’ alone simply returns you to the home directory.
The pwd command is considered as one of the most frequently used commands on Linux, AIX, HP-UX, *BSD, and other UNIX like operating systems along with the ls, and cd commands. It can be used for the following purposes under Apple OS X or UNIX or Linux operating systems:
- Finding the full path to the current directory.
- Storing the full path in a shell variable.
- Verifying the absolute path.
- Confirming the physical path, excluding symbolic links.
Syntax
pwd [OPTION]...
pwd Command Options:
- -L, –logical: Utilize the PWD from the environment, even if it includes symlinks.
- -P, –physical: Avoid all symlinks.
pwd Command Examples
Example 1: Simple usage of pwd
To check the current directory, simply enter:
$ pwd /usr/local/bin
Example 2: Displaying the Logical Current Working Directory
To display the logical current working directory, use:
$ pwd -L
The -L option makes pwd use $PWD from the environment, even if it contains symbolic links. It provides the absolute name of the current directory, possibly with symbolic links.
Example 3: Display the physical current working directory (all symbolic links resolved)
Consider ~/bin/ as a symbolic link:
$ pwd $ ls -l ~/bin/
Sample output:
lrwxrwxrwx 1 abc abc 35 May 13 2012 /home/abc/bin -> /home/abc/realdata/scripts/utils/
Now, navigate to ~/bin/ and confirm the current working directory with pwd:
$ cd ~/bin/ $ pwd
Sample output:
/home/abc/bin
To view the actual physical current working directory and avoid symlink resolution, use:
$ pwd -P
Sample output:
/home/abc/realdata/scripts/utils
Example 4: To see the version of pwd command
To view the version information of the pwd command, use the following command:
$ pwd --version
Sample output:
pwd (GNU coreutils) 8.13 Copyright (C) 2011 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Jim Meyering.
This command provides information about the version of the pwd command and its licensing details.
Example 5: Finding All Locations of the ‘pwd’ Executable
To locate all instances of the “pwd” executable, you can use the following command:
$ type -a pwd
This command will display information about all the locations where the “pwd” executable is stored.
Example 6: To get the help
To get help with the pwd command, use:
/bin/pwd --help
This displays usage information and options:
-L, --logical: Use PWD from the environment, even if it contains symlinks. -P, --physical: Avoid all symlinks. --help: Display help and exit. --version: Output version information and exit.
Note: Please remember that your shell may have its own version of the pwd command, which might offer different options and functionality. To understand the specific capabilities of your shell’s pwd command, refer your shell’s documentation or run info coreutils pwd invocation in your terminal for comprehensive guidance.
Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.
- Check Linux Books
- Apply for Programming Internship
- Practice Programming MCQs
- Check Information Technology Books