cd and pwd command examples

15 Practical Examples of ‘cd’ Command in Linux

In Linux ‘cd‘ (Change Directory) command is one of the most important and most widely used command for newbies as well as system administrators. For admins on a headless server, ‘cd‘ is the only way to navigate to a directory to check log, execute a program/application/script and for every other task. For newbie it is among those initial commands they make their hands dirty with.

cd command in linux

Thus, keeping in mind, we here bringing you 15 basic commands of ‘cd‘ using tricks and shortcuts to reduce your efforts on the terminal and save time by using these known tricks.

Tutorial Details
  1. Command Name : cd
  2. Stands for : Change Directory
  3. Availability : All Linux Distribution
  4. Execute On : Command Line
  5. Permission : Access own directory or otherwise assigned.
  6. Level : Basic/Beginners

1. Change from current directory to /usr/local.

avi@tecmint:~$ cd /usr/local

avi@tecmint:/usr/local$ 

2. Change from current directory to /usr/local/lib using absolute path.

avi@tecmint:/usr/local$ cd /usr/local/lib 

avi@tecmint:/usr/local/lib$ 

3. Change from current working directory to /usr/local/lib using relative path.

avi@tecmint:/usr/local$ cd lib 

avi@tecmint:/usr/local/lib$ 

4. (a) Move one directory back from where you are now.

avi@tecmint:/usr/local/lib$ cd - 

/usr/local 
avi@tecmint:/usr/local$ 

4. (b) Change Current directory to parent directory.

avi@tecmint:/usr/local/lib$ cd .. 

avi@tecmint:/usr/local$ 

5. Show last working directory from where we moved (use ‘–‘ switch) as shown.

avi@tecmint:/usr/local$ cd -- 

/home/avi 

6. Move two directory up from where you are now.

avi@tecmint:/usr/local$ cd ../ ../ 

avi@tecmint:/usr$

7. Move to users home directory from anywhere.

avi@tecmint:/usr/local$ cd ~ 

avi@tecmint:~$ 

or

avi@tecmint:/usr/local$ cd 

avi@tecmint:~$ 

8. Change working directory to current working directory (seems no use of in General).

avi@tecmint:~/Downloads$ cd . 
avi@tecmint:~/Downloads$ 

or

avi@tecmint:~/Downloads$ cd ./ 
avi@tecmint:~/Downloads$ 

9. Your present working Directory is “/usr/local/lib/python3.4/dist-packages/ ”, change it to “/home/avi/Desktop/ ”, in one line command, by moving up in the directory till ‘/’ then using absolute path.

avi@tecmint:/usr/local/lib/python3.4/dist-packages$ cd ../../../../../home/avi/Desktop/ 

avi@tecmint:~/Desktop$ 

10. Change from current working directory to /var/www/html without typing in full using TAB.

avi@tecmint:/var/www$ cd /v<TAB>/w<TAB>/h<TAB>

avi@tecmint:/var/www/html$ 

11. Navigate from your current working directory to /etc/v__ _, Oops! You forgot the name of directory and not supposed to use TAB.

avi@tecmint:~$ cd /etc/v* 

avi@tecmint:/etc/vbox$ 

Note: This will move to ‘vbox‘ only if there is only one directory starting with ‘v‘. If more than one directory starting with ‘v‘ exist, and no more criteria is provided in command line, it will move to the first directory starting with ‘v‘, alphabetically as their presence in standard dictionary.

12. You need to navigate to user ‘av‘ (not sure if it is avi or avt) home directory, without using TAB.

avi@tecmint:/etc$ cd /home/av? 

avi@tecmint:~$ 

13. What are pushd and popd in Linux?

Pushd and popd are Linux commands in bash and certain other shell which saves current working directory location to memory and bring to the directory from memory as current working directory, respectively as well as changes directory.

avi@tecmint:~$ pushd /var/www/html 

/var/www/html ~ 
avi@tecmint:/var/www/html$ 

The above command saves the current location to memory and changes to the requested directory. As soon as popd is fired, it fetch the saved directory location from memory and makes it current working directory.

avi@tecmint:/var/www/html$ popd 
~ 
avi@tecmint:~$ 

14. Change to a directory containing white spaces.

avi@tecmint:~$ cd test\ tecmint/ 

avi@tecmint:~/test tecmint$ 

or

avi@tecmint:~$ cd 'test tecmint' 
avi@tecmint:~/test tecmint$ 

or 

avi@tecmint:~$ cd "test tecmint"/ 
avi@tecmint:~/test tecmint$ 

15. Change from current working directory to Downloads and list all its settings in one go.

avi@tecmint:/usr$ cd ~/Downloads && ls

…
.
service_locator_in.xls 
sources.list 
teamviewer_linux_x64.deb 
tor-browser-linux64-3.6.3_en-US.tar.xz 
.
...

15 ‘pwd’ (Print Working Directory) Command Examples in Linux

For those working with Linux command Line, command ‘pwd‘ is very helpful, which tells where you are – 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 ‘pwd‘ comes to rescue.

Linux pwd Command Examples

What is pwd?

pwd‘ stands for ‘Print Working Directory‘. As the name states, command ‘pwd‘ 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 (/). This command is built in shell command and is available on most of the shell – bash, Bourne shell, ksh,zsh, etc.

Basic syntax of pwd:
# pwd [OPTION]
Options used with pwd
 Options  Description
 -L (logical)  Use PWD from environment, even if it contains symbolic links
 -P (physical)  Avoid all symbolic links
 –help  Display this help and exit
 –version  Output version information and exit

If both ‘-L‘ and ‘-P‘ options are used, option ‘L‘ is taken into priority. If no option is specified at the prompt, pwd will avoid all symlinks, i.e., take option ‘-P‘ into account.

Exit status of command pwd:

0 Success
Non-zero Failure

This article aims at providing you a deep insight of Linux command ‘pwd‘ with practical examples.

1. Print your current working directory.

avi@tecmint:~$ /bin/pwd

/home/avi

pwd linux command

2. Create a symbolic link of a folder (say /var/www/html into your home directory as htm). Move to the newly created directory and print working directory with symbolic links and without symbolic links.

Create a symbolic link of folder /var/www/html as htm in your home directory and move to it.

avi@tecmint:~$ ln -s /var/www/html/ htm
avi@tecmint:~$ cd htm

Create Symbolic Link

3. Print working directory from environment even if it contains symlinks.

avi@tecmint:~$ /bin/pwd -L

/home/avi/htm

Print Current Working Directory

4. Print actual physical current working directory by resolving all symbolic links.

avi@tecmint:~$ /bin/pwd -P

/var/www/html

Print Physical Working Directory

5. Check if the output of command “pwd” and “pwd -P” are same or not i.e., if no options are given at run-time does “pwd” takes option -P into account or not, automatically.

avi@tecmint:~$ /bin/pwd

/var/www/html

Check pwd Output

Result: It’s clear from the above output of example 4 and 5 (both result are same) thus, when no options are specified with command “pwd”, it automatically takes option “-P” into account.

6. Print version of your ‘pwd’ command.

avi@tecmint:~$ /bin/pwd --version

pwd (GNU coreutils) 8.23
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
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.

Check pwd Version

Note: A ‘pwd’ command is often used without options and never used with arguments.

Important: You might have noticed that we are executing the above command as “/bin/pwd” and not “pwd”.

So what’s the difference? Well “pwd” alone means shell built-in pwd. Your shell may have different version of pwd. Please refer manual. When we are using /bin/pwd, 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.

7. Print all the locations containing executable named pwd.

avi@tecmint:~$ type -a pwd

pwd is a shell builtin
pwd is /bin/pwd

Print Executable Locations

8. Store the value of “pwd” command in variable (say a), and print its value from the variable (important for shell scripting perspective).

avi@tecmint:~$ a=$(pwd)
avi@tecmint:~$ echo "Current working directory is : $a"

Current working directory is : /home/avi

Store Pwd Value in Variable

Alternatively, we can use printf, in the above example.

9. Change current working directory to anything (say /home) and display it in command line prompt. Execute a command (say ‘ls‘) to verify is everything is OK.

avi@tecmint:~$ cd /home
avi@tecmint:~$ PS1='$pwd> '		[Notice single quotes in the example]
> ls

Change Current Working Directory

10. Set multi-line command line prompt (say something like below).

/home
123#Hello#!

And then execute a command (say ls) to check is everything is OK.

avi@tecmint:~$ PS1='
> $PWD
$ 123#Hello#!
$ '

/home
123#Hello#!

Set Multi Commandline Prompt

11. Check the current working directory and previous working directory in one GO!

avi@tecmint:~$ echo “$PWD $OLDPWD”

/home /home/avi

Check Present Previous Working Directory

12. What is the absolute path (starting from /) of the pwd binary file.

/bin/pwd 

13. What is the absolute path (starting from /) of the pwd source file.

/usr/include/pwd.h 

14. Print the absolute path (starting from /) of the pwd manual pages file.

/usr/share/man/man1/pwd.1.gz

15. Write a shell script analyses current directory (say tecmint) in your home directory. If you are under directorytecmint it output “Well! You are in tecmint directory” and then print “Good Bye” else create a directory tecmintunder your home directory and ask you to cd to it.

Let’s first create a ‘tecmint’ directory, under it create a following shell script file with name ‘pwd.sh’.

avi@tecmint:~$ mkdir tecmint
avi@tecmint:~$ cd tecmint
avi@tecmint:~$ nano pwd.sh

Next, add the following script to the pwd.sh file.

#!/bin/bash

x="$(pwd)"
if [ "$x" == "/home/$USER/tecmint" ]
then
     {
      echo "Well you are in tecmint directory"
      echo "Good Bye"
     }
else
     {
      mkdir /home/$USER/tecmint
      echo "Created Directory tecmint you may now cd to it"
     }
fi

Give execute permission and run it.

avi@tecmint:~$ chmod 755 pwd.sh
avi@tecmint:~$ ./pwd.sh

Well you are in tecmint directory
Good Bye

Conclusion

pwd is one of the simplest yet most popular and most widely used command. A good command over pwd is basic to use Linux terminal. That’s all for now.

Advertisements

Posted on June 15, 2015, in Linux (Ubuntu/CentOS). Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: