{"id":12617,"date":"2019-03-28T03:57:16","date_gmt":"2019-03-28T03:57:16","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=12617"},"modified":"2019-03-28T03:57:16","modified_gmt":"2019-03-28T03:57:16","slug":"15-pwd-print-working-directory-command-examples-in-linux","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/28\/15-pwd-print-working-directory-command-examples-in-linux\/","title":{"rendered":"15 \u2018pwd\u2019 (Print Working Directory) Command Examples in Linux"},"content":{"rendered":"<p>For those working with Linux command Line, command \u2018<b>pwd<\/b>\u2018 is very helpful, which tells where you are \u2013 in which directory, starting from the root (<b>\/<\/b>). Specially for Linux newbies, who may get lost amidst of directories in command Line Interface while navigation, command \u2018<b>pwd<\/b>\u2018 comes to rescue.<\/p>\n<div id=\"attachment_9917\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/11\/pwd-command.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-9917\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/11\/pwd-command.png\" alt=\"Linux pwd Command Examples\" width=\"442\" height=\"317\" aria-describedby=\"caption-attachment-9917\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-9917\" class=\"wp-caption-text\">15 pwd Command Examples<\/p>\n<\/div>\n<h3>What is pwd?<\/h3>\n<p>\u2018<b>pwd<\/b>\u2018 stands for \u2018<b>Print Working Directory<\/b>\u2018. As the name states, command \u2018<b>pwd<\/b>\u2018 prints the current working directory or simply the directory user is, at present. It prints the current directory name with the complete path starting from root (<b>\/<\/b>). This command is built in shell command and is available on most of the shell \u2013 bash, Bourne shell, ksh,zsh, etc.<\/p>\n<h5>Basic syntax of pwd:<\/h5>\n<pre># pwd [OPTION]\r\n<\/pre>\n<h5>Options used with pwd<\/h5>\n<table border=\"0\" cellspacing=\"0\">\n<tbody>\n<tr>\n<td align=\"LEFT\" height=\"21\"><b>\u00a0Options<\/b><\/td>\n<td align=\"LEFT\"><b>\u00a0Description<\/b><\/td>\n<\/tr>\n<tr class=\"alt\">\n<td align=\"LEFT\" height=\"19\">\u00a0-L (logical)<\/td>\n<td align=\"LEFT\">\u00a0Use PWD from environment, even if it contains symbolic links<\/td>\n<\/tr>\n<tr>\n<td align=\"LEFT\" height=\"19\">\u00a0-P (physical)<\/td>\n<td align=\"LEFT\">\u00a0Avoid all symbolic links<\/td>\n<\/tr>\n<tr class=\"alt\">\n<td align=\"LEFT\" height=\"19\">\u00a0\u2013help<\/td>\n<td align=\"LEFT\">\u00a0Display this help and exit<\/td>\n<\/tr>\n<tr>\n<td align=\"LEFT\" height=\"19\">\u00a0\u2013version<\/td>\n<td align=\"LEFT\">\u00a0Output version information and exit<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>If both \u2018<b>-L<\/b>\u2018 and \u2018<b>-P<\/b>\u2018 options are used, option \u2018<b>L<\/b>\u2018 is taken into priority. If no option is specified at the prompt, pwd will avoid all symlinks, i.e., take option \u2018<b>-P<\/b>\u2018 into account.<\/p>\n<p>Exit status of command pwd:<\/p>\n<table border=\"0\" cellspacing=\"0\">\n<tbody>\n<tr class=\"alt\">\n<td align=\"CENTER\" height=\"19\">0<\/td>\n<td align=\"CENTER\">Success<\/td>\n<\/tr>\n<tr>\n<td align=\"CENTER\" height=\"19\">Non-zero<\/td>\n<td align=\"CENTER\">Failure<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>This article aims at providing you a deep insight of Linux command \u2018<strong>pwd<\/strong>\u2018 with practical examples.<\/p>\n<p><strong>1.<\/strong>\u00a0Print your current working directory.<\/p>\n<pre>avi@tecmint:~$ \/bin\/pwd\r\n\r\n\/home\/avi\r\n<\/pre>\n<div id=\"attachment_9905\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/11\/pwd.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-9905\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/11\/pwd.gif\" alt=\"pwd linux command\" width=\"620\" height=\"368\" aria-describedby=\"caption-attachment-9905\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-9905\" class=\"wp-caption-text\">Print Working Directory<\/p>\n<\/div>\n<p><strong>2.<\/strong>\u00a0Create a symbolic link of a folder (say\u00a0<b>\/var\/www\/html<\/b>\u00a0into your home directory as\u00a0<b>htm<\/b>). Move to the newly created directory and print working directory with symbolic links and without symbolic links.<\/p>\n<p>Create a symbolic link of folder \/var\/www\/html as htm in your home directory and move to it.<\/p>\n<pre>avi@tecmint:~$ ln -s \/var\/www\/html\/ htm\r\navi@tecmint:~$ cd htm\r\n<\/pre>\n<div id=\"attachment_9907\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/11\/Create-Symbolic-Link.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-9907\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/11\/Create-Symbolic-Link.gif\" alt=\"Create Symbolic Link\" width=\"620\" height=\"368\" aria-describedby=\"caption-attachment-9907\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-9907\" class=\"wp-caption-text\">Create Symbolic Link<\/p>\n<\/div>\n<p><strong>3.<\/strong>\u00a0Print working directory from environment even if it contains symlinks.<\/p>\n<pre>avi@tecmint:~$ \/bin\/pwd -L\r\n\r\n\/home\/avi\/htm\r\n<\/pre>\n<div id=\"attachment_9908\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/11\/Print-Working-Directory.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-9908\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/11\/Print-Working-Directory.gif\" alt=\"Print Current Working Directory\" width=\"620\" height=\"368\" aria-describedby=\"caption-attachment-9908\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-9908\" class=\"wp-caption-text\">Print Current Working Directory<\/p>\n<\/div>\n<p><strong>4.<\/strong>\u00a0Print actual physical current working directory by resolving all symbolic links.<\/p>\n<pre>avi@tecmint:~$ \/bin\/pwd -P\r\n\r\n\/var\/www\/html\r\n<\/pre>\n<div id=\"attachment_9909\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/11\/Print-Physical-Working-Directory.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-9909\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/11\/Print-Physical-Working-Directory.gif\" alt=\"Print Physical Working Directory\" width=\"620\" height=\"368\" aria-describedby=\"caption-attachment-9909\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-9909\" class=\"wp-caption-text\">Print Physical Working Directory<\/p>\n<\/div>\n<p><strong>5.<\/strong>\u00a0Check if the output of command \u201c<b>pwd<\/b>\u201d and \u201c<b>pwd -P<\/b>\u201d are same or not i.e., if no options are given at run-time does \u201c<b>pwd<\/b>\u201d takes option\u00a0<b>-P<\/b>\u00a0into account or not, automatically.<\/p>\n<pre>avi@tecmint:~$ \/bin\/pwd\r\n\r\n\/var\/www\/html\r\n<\/pre>\n<div id=\"attachment_9910\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/11\/Check-pwd-Output.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-9910\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/11\/Check-pwd-Output.gif\" alt=\"Check pwd Output\" width=\"620\" height=\"368\" aria-describedby=\"caption-attachment-9910\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-9910\" class=\"wp-caption-text\">Check pwd Output<\/p>\n<\/div>\n<p><strong>Result<\/strong>: It\u2019s clear from the above output of example 4 and 5 (both result are same) thus, when no options are specified with command \u201c<b>pwd<\/b>\u201d, it automatically takes option \u201c<b>-P<\/b>\u201d into account.<\/p>\n<p><strong>6.<\/strong>\u00a0Print version of your \u2018pwd\u2019 command.<\/p>\n<pre>avi@tecmint:~$ \/bin\/pwd --version\r\n\r\npwd (GNU coreutils) 8.23\r\nCopyright (C) 2014 Free Software Foundation, Inc.\r\nLicense GPLv3+: GNU GPL version 3 or later &lt;http:\/\/gnu.org\/licenses\/gpl.html&gt;.\r\nThis is free software: you are free to change and redistribute it.\r\nThere is NO WARRANTY, to the extent permitted by law.\r\n\r\nWritten by Jim Meyering.\r\n<\/pre>\n<div id=\"attachment_9911\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/11\/Check-pwd-Version.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-9911\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/11\/Check-pwd-Version.gif\" alt=\"Check pwd Version\" width=\"620\" height=\"368\" aria-describedby=\"caption-attachment-9911\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-9911\" class=\"wp-caption-text\">Check pwd Version<\/p>\n<\/div>\n<p><strong>Note<\/strong>: A \u2018pwd\u2019 command is often used without options and never used with arguments.<\/p>\n<p><strong>Important<\/strong>: You might have noticed that we are executing the above command as \u201c<b>\/bin\/pwd<\/b>\u201d and not \u201c<b>pwd<\/b>\u201d.<\/p>\n<p>So what\u2019s the difference? Well \u201c<b>pwd<\/b>\u201d alone means shell built-in pwd. Your shell may have different version of pwd. Please refer manual. When we are using\u00a0<b>\/bin\/pwd<\/b>, we are calling the binary version of that command. Both the shell and the binary version of command Prints Current Working Directory, though the binary version have more options.<\/p>\n<p><strong>7.<\/strong>\u00a0Print all the locations containing executable named pwd.<\/p>\n<pre>avi@tecmint:~$ type -a pwd\r\n\r\npwd is a shell builtin\r\npwd is \/bin\/pwd\r\n<\/pre>\n<div id=\"attachment_9912\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/11\/Print-Executable-Locations.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-9912\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/11\/Print-Executable-Locations.gif\" alt=\"Print Executable Locations\" width=\"620\" height=\"368\" aria-describedby=\"caption-attachment-9912\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-9912\" class=\"wp-caption-text\">Print Executable Locations<\/p>\n<\/div>\n<p><strong>8.<\/strong>\u00a0Store the value of \u201c<b>pwd<\/b>\u201d command in variable (say\u00a0<b>a<\/b>), and print its value from the variable (important for shell scripting perspective).<\/p>\n<pre>avi@tecmint:~$ a=$(pwd)\r\navi@tecmint:~$ echo \"Current working directory is : $a\"\r\n\r\nCurrent working directory is : \/home\/avi\r\n<\/pre>\n<div id=\"attachment_9913\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/11\/Store-Pwd-Value-in-Variable.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-9913\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/11\/Store-Pwd-Value-in-Variable.gif\" alt=\"Store Pwd Value in Variable\" width=\"620\" height=\"368\" aria-describedby=\"caption-attachment-9913\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-9913\" class=\"wp-caption-text\">Store Pwd Value in Variable<\/p>\n<\/div>\n<p>Alternatively, we can use\u00a0<b>printf<\/b>, in the above example.<\/p>\n<p><strong>9.<\/strong>\u00a0Change current working directory to anything (say\u00a0<b>\/home<\/b>) and display it in command line prompt. Execute a command (say \u2018<b>ls<\/b>\u2018) to verify is everything is\u00a0<b>OK<\/b>.<\/p>\n<pre>avi@tecmint:~$ cd \/home\r\navi@tecmint:~$ PS1='$pwd&gt; '\t\t[Notice single quotes in the example]\r\n&gt; ls\r\n<\/pre>\n<div id=\"attachment_9914\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/11\/Change-Current-Working-Directory.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-9914\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/11\/Change-Current-Working-Directory.gif\" alt=\"Change Current Working Directory\" width=\"620\" height=\"368\" aria-describedby=\"caption-attachment-9914\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-9914\" class=\"wp-caption-text\">Change Current Working Directory<\/p>\n<\/div>\n<p><strong>10.<\/strong>\u00a0Set multi-line command line prompt (say something like below).<\/p>\n<pre>\/home\r\n123#Hello#!\r\n<\/pre>\n<p>And then execute a command (say\u00a0<b>ls<\/b>) to check is everything is\u00a0<b>OK<\/b>.<\/p>\n<pre>avi@tecmint:~$ PS1='\r\n&gt; $PWD\r\n$ 123#Hello#!\r\n$ '\r\n\r\n\/home\r\n123#Hello#!\r\n<\/pre>\n<div id=\"attachment_9915\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/11\/Set-Multi-Commandline-Prompt.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-9915\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/11\/Set-Multi-Commandline-Prompt.gif\" alt=\"Set Multi Commandline Prompt\" width=\"620\" height=\"368\" aria-describedby=\"caption-attachment-9915\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-9915\" class=\"wp-caption-text\">Set Multi Commandline Prompt<\/p>\n<\/div>\n<p><strong>11.<\/strong>\u00a0Check the current working directory and previous working directory in one GO!<\/p>\n<pre>avi@tecmint:~$ echo \u201c$PWD $OLDPWD\u201d\r\n\r\n\/home \/home\/avi\r\n<\/pre>\n<div id=\"attachment_9916\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/11\/Check-Present-Previous-Working-Directory.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-9916\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/11\/Check-Present-Previous-Working-Directory.gif\" alt=\"Check Present Previous Working Directory\" width=\"620\" height=\"368\" aria-describedby=\"caption-attachment-9916\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-9916\" class=\"wp-caption-text\">Check Present Previous Working Directory<\/p>\n<\/div>\n<p><strong>12.<\/strong>\u00a0What is the absolute path (starting from\u00a0<b>\/<\/b>) of the pwd binary file.<\/p>\n<pre>\/bin\/pwd \r\n<\/pre>\n<p><strong>13.<\/strong>\u00a0What is the absolute path (starting from\u00a0<b>\/<\/b>) of the pwd source file.<\/p>\n<pre>\/usr\/include\/pwd.h \r\n<\/pre>\n<p><strong>14.<\/strong>\u00a0Print the absolute path (starting from\u00a0<b>\/<\/b>) of the pwd manual pages file.<\/p>\n<pre>\/usr\/share\/man\/man1\/pwd.1.gz\r\n<\/pre>\n<p><strong>15.<\/strong>\u00a0Write a shell script analyses current directory (say\u00a0<b>tecmint<\/b>) in your home directory. If you are under directory\u00a0<b>tecmint<\/b>\u00a0it output \u201c<b>Well! You are in tecmint directory<\/b>\u201d and then print \u201c<b>Good Bye<\/b>\u201d else create a directory\u00a0<b>tecmint<\/b>under your home directory and ask you to\u00a0<b>cd<\/b>\u00a0to it.<\/p>\n<p>Let\u2019s first create a \u2018tecmint\u2019 directory, under it create a following shell script file with name \u2018pwd.sh\u2019.<\/p>\n<pre>avi@tecmint:~$ mkdir tecmint\r\navi@tecmint:~$ cd tecmint\r\navi@tecmint:~$ nano pwd.sh\r\n<\/pre>\n<p>Next, add the following script to the pwd.sh file.<\/p>\n<pre>#!\/bin\/bash\r\n\r\nx=\"$(pwd)\"\r\nif [ \"$x\" == \"\/home\/$USER\/tecmint\" ]\r\nthen\r\n     {\r\n      echo \"Well you are in tecmint directory\"\r\n      echo \"Good Bye\"\r\n     }\r\nelse\r\n     {\r\n      mkdir \/home\/$USER\/tecmint\r\n      echo \"Created Directory tecmint you may now cd to it\"\r\n     }\r\nfi\r\n<\/pre>\n<p>Give execute permission and run it.<\/p>\n<pre>avi@tecmint:~$ chmod 755 pwd.sh\r\navi@tecmint:~$ .\/pwd.sh\r\n\r\nWell you are in tecmint directory\r\nGood Bye\r\n<\/pre>\n<h3>Conclusion<\/h3>\n<p><b>pwd<\/b>\u00a0is one of the simplest yet most popular and most widely used command. A good command over pwd is basic to use Linux terminal. That\u2019s all for now.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/pwd-command-examples\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>For those working with Linux command Line, command \u2018pwd\u2018 is very helpful, which tells where you are \u2013 in which directory, starting from the root (\/). Specially for Linux newbies, who may get lost amidst of directories in command Line Interface while navigation, command \u2018pwd\u2018 comes to rescue. 15 pwd Command Examples What is pwd? &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/28\/15-pwd-print-working-directory-command-examples-in-linux\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;15 \u2018pwd\u2019 (Print Working Directory) Command Examples in Linux&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-12617","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12617","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=12617"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12617\/revisions"}],"predecessor-version":[{"id":12618,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12617\/revisions\/12618"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=12617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=12617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=12617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}