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

SQL for Data Analysis - Udacity

  Entity Relationship Diagrams An  entity relationship diagram  (ERD) is a common way to view data in a database. Below is the ERD for the database we will use from Parch & Posey. These diagrams help you visualize the data you are analyzing including: The names of the tables. The columns in each table. The way the tables work together. You can think of each of the boxes below as a spreadsheet. What to Notice In the Parch & Posey database there are five tables (essentially 5 spreadsheets): web_events accounts orders sales_reps region You can think of each of these tables as an individual spreadsheet. Then the columns in each spreadsheet are listed below the table name. For example, the  region  table has two columns:  id  and  name . Alternatively the  web_events  table has four columns. The "crow's foot" that connects the tables together shows us how the columns in one table relate to the columns in another table. In this first lesson, you will be learning the bas

Impressive - Check if Your Email Address Has Been Hacked - Free,Easy Tutorial

haveibeenpwned.com Data breaches are rampant and many people don't appreciate the scale or frequency with which they occur. A "breach" is an incident where data is inadvertently exposed in a vulnerable system, usually due to insufficient access controls or security weaknesses in the software. How is the legitimacy of a data breach established? Attackers often give "breach" announcements, which are later revealed to be hoaxes. There is a delicate balance to be struck between making data searchable as soon as possible and conducting proper due diligence to confirm the breach's validity. In order to verify the authenticity of a violation, the following steps are normally taken: Has the affected provider made a public statement about the security breach? Does the information stolen in the breach show up in a Google search (i.e., it was simply copied from another source)? Is the structure of the data consistent with what you'd expect to see in a breach? Have