Linux CD Command with 10 Examples

cd command linux

cd command is one of the most basic and common commands used among Linux users, but this is not limited only to Linux systems and this is also available in operating systems such as Unix, DOS, OS/2, AmigaOS, Windows, ReactOS.

Linux Cd command is used to change the current working directory of a user. This article details about this command, what this command can do and the internals about the command.

There are two types of commands in Linux distributions known as Internal Commands and External Commands.

Internal Commands: These are shell-built in commands and they are loaded into the system at the time of booting and are not in need of separate processes to run these commands.
External Commands: These commands are stored as separate binaries and shell starts separate sub processes to execute these commands

Cd is one of the Internal commands of the shell and no binaries are present for the cd command. This can be verified using below commands.

1) Check and verify your current shell

Shell is the command interpreter of the Unix operating system and it acts as a program that executes other programs

fluser@fluser-virtual-machine:~$ echo $SHELL

As can be seen, the current shell is the bash shell of this user

2) Check the path of cd command binary(if any)

For this purpose, 'which' commands can be used, which is a Unix command used to identify the location of executables, and in this case the location of cd command

fluser@fluser-virtual-machine:~$ which cd

It results in no output. Its because no binary is present on the system for cd command. Still you are able to execute the command. This is because cd is a BASH internal command. Internal commands are built into the shell

3) Verify

Internal command 'type' gives you the information that cd is internal command as below.

fluser@fluser-virtual-machine:~$ type cd
cd is a shell builtin

If you try to access the manual entry for any internal command, you will not see a separate manual page for them.

fluser@fluser-virtual-machine:~$ man cd
No manual entry for cd

To view the full list of internal commands, you can use the help commands

fluser@fluser-virtual-machine:~$ help
GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
These shell commands are defined internally.  Type `help' to see this list.
Type `help name' to find out more about the function `name'.
Use `info bash' to find out more about the shell in general.
Use `man -k' or `info' to find out more about commands not in this list.

A star (*) next to a name means that the command is disabled.

 job_spec [&]                            history [-c] [-d offset] [n] or hist>
 (( expression ))                        if COMMANDS; then COMMANDS; [ elif C>
 . filename [arguments]                  jobs [-lnprs] [jobspec ...] or jobs >
 :                                       kill [-s sigspec | -n signum | -sigs>
 [ arg... ]                              let arg [arg ...]
 [[ expression ]]                        local [option] name[=value] ...
 alias [-p] [name[=value] ... ]          logout [n]
 bg [job_spec ...]                       mapfile [-n count] [-O origin] [-s c>
 bind [-lpsvPSVX] [-m keymap] [-f file>  popd [-n] [+N | -N]
 break [n]                               printf [-v var] format [arguments]
 builtin [shell-builtin [arg ...]]       pushd [-n] [+N | -N | dir]
 caller [expr]                           pwd [-LP]
 case WORD in [PATTERN [| PATTERN]...)>  read [-ers] [-a array] [-d delim] [->
 cd [-L|[-P [-e]] [-@]] [dir]            readarray [-n count] [-O origin] [-s>
 command [-pVv] command [arg ...]        readonly [-aAf] [name[=value] ...] o>
 compgen [-abcdefgjksuv] [-o option]  >  return [n]
 complete [-abcdefgjksuv] [-pr] [-DE] >  select NAME [in WORDS ... ;] do COMM>
 compopt [-o|+o option] [-DE] [name ..>  set [-abefhkmnptuvxBCHP] [-o option->
 continue [n]                            shift [n]
 coproc [NAME] command [redirections]    shopt [-pqsu] [-o] [optname ...]
 declare [-aAfFgilnrtux] [-p] [name[=v>  source filename [arguments]
 dirs [-clpv] [+N] [-N]                  suspend [-f]
 disown [-h] [-ar] [jobspec ...]         test [expr]
 echo [-neE] [arg ...]                   time [-p] pipeline
 enable [-a] [-dnps] [-f filename] [na>  times
 eval [arg ...]                          trap [-lp] [[arg] signal_spec ...]
 exec [-cl] [-a name] [command [argume>  true
 exit [n]                                type [-afptP] name [name ...]
 export [-fn] [name[=value] ...] or ex>  typeset [-aAfFgilrtux] [-p] name[=va>
 false                                   ulimit [-SHabcdefilmnpqrstuvxT] [lim>
 fc [-e ename] [-lnr] [first] [last] o>  umask [-p] [-S] [mode]
 fg [job_spec]                           unalias [-a] name [name ...]
 for NAME [in WORDS ... ] ; do COMMAND>  unset [-f] [-v] [-n] [name ...]
 for (( exp1; exp2; exp3 )); do COMMAN>  until COMMANDS; do COMMANDS; done
 function name { COMMANDS ; } or name >  variables - Names and meanings of so>
 getopts optstring name [arg]            wait [-n] [id ...]
 hash [-lr] [-p pathname] [-dt] [name >  while COMMANDS; do COMMANDS; done
 help [-dms] [pattern ...]               { COMMANDS ; }

Why is cd an Internal command?

Whenever a process is created by BASH, it is executed in a subshell of BASH (child process of current BASH process). The new process makes changes and provides the outputs (if required) and no property of this subshell is returned to the parent when the process dies. Note that cd command changes the current working directory of the shell. If cd were an external command, it would have changed the current working directory of the subshell, returning nothing to the parent shell. And hence, the PWD of the shell will never change. All the commands that make changes to the environment of the shell, must be implemented as internal command. we could never achieve what we require by making cd an external command.

cd Command Usage

1) Using absolute path - Change the current directory

Here we are changing the current directory using an absolute path ( here in example '/etc/cron.d' is the path)

fluser@fluser-virtual-machine:~$ cd /etc/cron.d
fluser@fluser-virtual-machine:/etc/cron.d$ pwd

2) Using relative path - Change the current directory

Here we are changing the current directory using a relative path (relative to where you are located now)

fluser@fluser-virtual-machine:/etc/cron.d$ pwd
fluser@fluser-virtual-machine:/etc/cron.d$ cd ../network/
fluser@fluser-virtual-machine:/etc/network$ pwd

3) Switch back to the previous directory where you were working earlier

This can be used to go back to your previous working directory

fluser@fluser-virtual-machine:/etc/network$ cd -

4) Change current directory to the parent directory

This can be used to go back one directory along the directory tree

fluser@fluser-virtual-machine:/etc/cron.d$ cd ..
fluser@fluser-virtual-machine:/etc$ pwd

5) Go back two directories

This command takes you back two directories of your directory tree

fluser@fluser-virtual-machine:~/Downloads$ pwd
fluser@fluser-virtual-machine:~/Downloads$ cd ../../
fluser@fluser-virtual-machine:/home$ pwd

6) Go to current user's home directory

This is usually the /home/{user} directory and the current working directory changes to that location

fluser@fluser-virtual-machine:/etc$ cd ~
fluser@fluser-virtual-machine:~$ pwd

7) Go to the home directory of another user

This can be only if you are root or if you have root privileges. Similar to above command, this command takes you to another user's home directory

root@fluser-virtual-machine:/etc$ cd ~testuser
root@fluser-virtual-machine:~$ pwd

8) Change working directory to current working directory

This command takes you to the current working directory. This seems no use of in general as no change will be done to your current working directory

fluser@fluser-virtual-machine:~$ pwd
fluser@fluser-virtual-machine:~$ cd .
fluser@fluser-virtual-machine:~$ pwd

9) Go to root directory

Root directory if Unix system is / and this command takes you to this location

fluser@fluser-virtual-machine:/home$ cd /
fluser@fluser-virtual-machine:/$ pwd

10) Create a directory and move into that directory

You can perform 2 actions from a single command as below.This commands creates the directory 'test' and changes the current working directory to 'test'

fluser@fluser-virtual-machine:~/Documents$ pwd
fluser@fluser-virtual-machine:~/Documents$ mkdir test && cd $_
fluser@fluser-virtual-machine:~/Documents/test$ pwd

Thanks for reading and please leave your suggestions on the below comment section.

Read Also:

Raghu 11:09 am


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

All comments are subject to moderation.