Skip to main content

12 Different Ways To Use pwd command

NAME
pwd - print name of current/working directory.
It prints the path of the working directory starting from root(/). 

SYNOPSIS
pwd [option]...

DESCRIPTION
Print the full filename of the current working directory.
It has 2 flags and it takes no arguments.
-L, --logical
use pwd from environment, even if it contains symbolic links.

-P, --physical
avoid all symbolic links.

If both -L and -P options are used, -L is taken into priority. And if no option is specified at the command prompt, pwd will avoid all symlinks, so -P is assumed.


--help 
display this help and exit

--version
output version information and exit

Returns zero unless an invalid option is given or the current directory cannot be read.
Therefore, 0 = success.
Non-zero(any character that is not zero) = failure.

NOTE: your shell may have its own version of pwd, which usually supersedes the version described here. So we'll use /bin/pwd instead of pwd alone.
Reason:
pwd alone means the shell built-in pwd and your shell may have a different version of pwd.
When we use /bin/pwd we are calling the binary version of that command. 
Both versions of the command print the current working directory though the binary version has many options.
Please refer to your shell's documentation for details about the options it supports.

1. Print your current working directory
 /bin/pwd  

2. Create a symbolic link of a folder. e.g /var/www/html as htm. Move to the newly created directory and print working directory with and without symbolic links.
 ln -s /var/www/html/ htm  

 cd htm

3. Print working directory from environment even if it has symbolic links:
 /bin/pwd -L  

4. Print actual physical working directory without any symbolic links:
 /bin/pwd -P  

To delete or remove symbolic links, you need to have write permissions on the directory that contains the symlink. When you remove a symlink, the file it points to is not affected.
To delete a symlink, invoke the rm command followed by the symlink name.
5. Print version of your pwd command:
 /bin/pwd --version  

6. Print all the locations containing executable named pwd.
 type -a pwd  

7. Store the value of pwd command in a variable, (x) and print its value from the variable. This is important for shell scripting.
Alternatively we can use printf
 x=$(pwd)  

 echo "Current working directory is : $x"  

8. Check the previous and current working directory at the same time.
 echo "$PWD $OLDPWD"  

9. The absolute path starting from root(/) of the pwd binary file.
 /bin/pwd  

10. The absolute path starting from root(/) of the pwd source file.
 /usr/include/pwd.h  

11. The absolute path starting from root(/) of the pwd manual pages file.
 /usr/share/man/man1/pwd.1.gz  

12. Write a shell script that analyses your current working directory in your home directory. If you are under directory A it will output "You are in directory A" else create directory A under your home directory and ask you to cd into it.

First create directory A and under it create a shell script file with name pwd.sh
 mkdir eneri  
 cd eneri  
 sudo nano pwd.sh  
Now add the following script to the pwd.sh file.
 #!/bin/bash  
 x="$(pwd)"  
 if [ "$x" == "/home/$USER/eneri" ]  
 then  
 {  
   echo "You are now in eneri directory"  
 }  
 else  
 {  
   mkdir /home/$USER/eneri  
   echo "Created Directory eneri you may now cd into it"  
 }  
 fi  
Give execute permission and run it.
 sudo chmod 755 pwd.sh 
 
./pwd.sh  

One important concept to understand is that the shell has a notion of a default location in which any file operations will take place. This is its working directory. 

If you try to create new files or directories, view existing files, or even delete them, the shell will assume you’re looking for them in the current working directory unless you take steps to specify otherwise. 

So it’s quite important to keep an idea of what directory the shell is “in” at any given time, after all, deleting files from the wrong directory could be disastrous. 

If you’re ever in any doubt, the pwd command will tell you exactly what the current working directory is.

Comments

Post a Comment

Your input is valued. Please type something....

Popular posts from this blog

Fun Terminal Commands Every Linux User Should Try

Accessing Maps from the terminal with MapSCII Requirements Telnet installed Internet Connection Firewall is disabled You can do this on Linux, Unix, Mac OS X or Windows with an app like PuTTY or the Windows 10 Linux bash shell or any Os that supports telnet. Open terminal and write the command below. telnet mapscii.me Hit enter and you're ready to browse and enjoy MapSCII. Navigate using the keyboard or mouse. Use the following keys on your keyboard Arrow keys: up, down, right, left A to zoom in Z to zoom out C toggles ASCII mode on/off You can also click and drag and hold on the map with your cursor. If your connection dropped without a reason, reconnect with telnet -E mapscii.me and use only cursors, A and Z to navigate. The Mapscii project is open source and you can install it locally if you'd like. Check out their project here on GitHub . The Dancing ASCII Party Parrot Requirements Curl installed Internet

What Is The Linux Kernel? How To Check Your Version

INTRODUCTION. There are so many Linux distributions but the one thing they all have in common is the Linux Kernel. A lot of people talk about the Linux kernel but most of them don't really know what it does. Each operating system  uses a kernel without which you can't have a computer that actually works. You may see and interact with a lot of different software but it is the kernel underneath that does much of the less glamorous work. In this article, we're going to look at Linux kernel and why it is needed. What Exactly is the Linux Kernel? The Linux Kernel is the main component of a Linux operating System and is the core interface between a computer's hardware and its processes. It communicates between the two,managing resources as efficiently as possible. It is technically incorrect to refer to Linux as a complete operating system. Linux actually refers specifically to the kernel which was named after its founder Linus Torvalds. It is named the kernel b

How To Watch Star Wars in Terminal (Windows, Linux, Mac OS)

Star Wars is one of the most popular Sci-Fi movie movie franchise in the history of cinema.  If you are one of its hard core fans, perhaps you won't mind watching it again.  But it won't be HD or BlueRay edition. In terms of graphics, it's nowhere close to what you would expect but still fun nonetheless. Instead, it will be in ASCII format. You can watch it on Windows, Mac Os and Linux or any OS that supports Telnet. I'll also show you a few tricks that will make it ore colourful. Telnet is a network protocol used to connect to another host or someone else's computer. On Windows Telnet is disable by default, so you need to enable it in the control panel. If you don't have telnet, you can install it using the following command in your command line then press enter. pkgmgr /iu:"TelnetClient" If asked for administrator permissions, enter your password and confirm access. Restart the command prompt and then type in the following