• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
sharadchhetri

sharadchhetri

Tutorials On Linux, Unix & Open Source

  • Home
  • Linux Commands
  • Resources
    • Learn Linux
  • My WordPress plugins

print working directory ( pwd , PWD , OLDPWD ) in Linux / Unix

April 6, 2014 by Sharad Chhetri Leave a Comment

When we work in terminal or command line, sometime we want to know in which current directory we are working. And sometimes we also want to know in which previous directory I worked.These requirement are simple but are very important for system administration.

Information of working directory is also required in scripting. When system administrator do troubleshooting, they also require the information of working directory.

To Print Current Working Directory

Using shell builtin command:

To print the working directory in Linux/Unix, we can use shell builtin command.
(Use type command to know which command type it is – Internal or External command)

pwd

Example: The pwd command output shows the absolute path i.e /home/linux

linux@tuxworld:~$ pwd
/home/linux
linux@tuxworld:~$

Using shell variable :

We can also use shell variable to find the current working directory.Use the below given shell variable

echo $PWD

Example : The output from command echo $PWD, shows path /root

root@tuxworld:~# echo $PWD
/root
root@tuxworld:~# 

To Print Previous Working Directory

To print Previous Working Directory, we will use shell variable command as given below

echo $OLDPWD

Example: In this example, the output of echo $OLDPWD shown the previous working directory. And the output of pwd command shows current working directory.

root@tuxworld:~# echo $OLDPWD
/var/www
root@tuxworld:~# 
root@tuxworld:~# pwd
/root
root@tuxworld:~#

Note: When we use cd command with option – (hyphen) means cd - command. It is similar to running the command cd $OLDPWD .

Share this:

  • Twitter
  • Facebook
  • More
  • Print
  • Email
  • LinkedIn
  • Reddit
  • Tumblr
  • Pinterest
  • Pocket
  • Telegram
  • WhatsApp
  • Mastodon

Related posts:

  1. Unix / Linux : How to print duplicate lines from file
  2. print new line character in Unix / Linux
  3. SCP file and directory in linux and Unix like operating system
  4. cron job not working : troubleshooting steps
  5. shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
  6. awk command to print columns from file
  7. How to print particular line number by using sed command
  8. How to rename and move the directory and files in linux
  9. Linux command : To create new directory/folder
  10. What is Linux/Unix Internal And External Command

Filed Under: Linux, Linux Commands Tagged With: linux command

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Our Social Media Presence

  • Facebook
  • GitHub
  • Twitter

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Linux Commands

Temporarily change the shell after login

There are unfinished transactions remaining. You might consider running yum-complete-transaction

man command not found in CentOS and Red Hat

How to install libxml2 and libxslt packages on CentOS 6.5

Learn tar command and know about its precaution

grep command to remove commented lines

How To Find Absolute Full Path Of Command On Linux / Unix : which command

Explore 70+ Articles On Linux Commands

Always Useful Tips And Tricks

Error: Could not stat() command file ‘/var/lib/nagios3/rw/nagios.cmd’!

Change default editor of crontab in Ubuntu

How to change hostname in Ubuntu 12.04 and 12.10 without system restart

shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory

Keep logs of user after sudo su – : Secondary Logging

WordPress host IP changed not able to open wp-admin and site page looks scattered

How to page scroll up/down in Linux terminal

Explore 90+ Article On "Linux Tips And Tricks"

You Might Like These Articles!

Internal External Command

What is Linux/Unix Internal And External Command

Linux basic command

Linux Basic Commands For Every Beginner

simplecodesyntax wordpress plugin

SimpleCodeSyntax : My Another WordPress Plugin

Install Nginx

How To Install Nginx On Ubuntu 22.04 LTS

Install Latest Git package in Ubuntu Operating System

How To Always Install Latest Git Package In Ubuntu Operating System

Bash script for installing VirtualBox on Ubuntu 22.04 LTS Desktop

Install VirtualBox On Ubuntu 22.04 LTS Desktop (Bash Script)

Copyright © 2023 ยท
The material in this site cannot be republished either online or offline, without our permission but Schools and Colleges can do in their Private Network
Proudly Blogging From Bharat.

  • Contact
  • About Me
  • My WordPress plugins
  • Privacy Policy