This is a basic post to show you what we can do with cd command to reduce your time spending at terminal by using alias, tips and some shortcuts. cd(Change Directory) is a basic command which is used to change your working directory from one to other. Here are some productivity tips and some less known tips to you people. Feel free to comment on this.

Example 1: Change working directory to /abc/xyz

cd /abc/xyz

Example 2: Change directory from present working directory to /var/ftp to /var/ftp/pub using absolute path. Share Image

cd /var/ftp/pub

Example 3: Change directory from present working directory /var/ftp to /var/ftp/pub using relative path. To understand below command we should know more about Absolute and Relative path.

cd pub

Example 4: Change working directory to user’s home directory from anywhere in the directory structure.

cd ~

Example 5: One more way to achive above us just cd command as shown below.


Example 6: Sometimes we want to change directory to previous working directory and come back to where we are. We can use “cd -” which will take us to previous working directory. This is vary much useful cd command.

cd –

Example 7: Change working directory to present working directory. This command is of no use because this will change the directory to present directory itself.

cd .

Example 8: Change working directory to parent directory.

cd ..

Example 9: Change working directory to parents parent directory or two levels up in the directory structure.

cd ../..

Exampl 10: How about changing three levels up in the directory.

cd ../../..

And so on how many directory levels up you want to change that many double dots(..) separated by / you can use.

Example 11: I am in /var/ftp folder and I want to change the directory to /etc/samba with single command. How can I do that? Just mix what we learn, ie relative path and above “..” technique.

cd ../../etc/samba

Still did not get relative path and absolute path? You have to read relative vs absolute path difference.

cd command supports wildcards such as ?(for single character), * for multiple character and directory completion.

Example 12: Change the working directory to /var/ftp without mentioning ftp fully.

cd /var/ft?

This will change the directory to /var/ftp if there is no other directory name which starts with ft and have three characters.

Example 13: Change the working directory

cd /var/f*

This will change the directory to /var/ftp, if /var directory contain only one folder which starts with f letter.

Example 14: Change directory to /home/surendra/redhat/ubuntu with out typing all the characters using <tab>

cd /h<tab>/s<tab>/r<tab>/u<tab>

Note: Press tab to complete directory name.

Example 15: We can use simple alias to reduce our time at terminal. Keep these alias in .bashrc(for bash shell) or .profile(for ksh shell) files in user’s home directories.

alias cd1=’cd ..’
alias cd2=’cd ../..’
alias cd3=’cd ../../..’
alias cd4=’cd ../../../..’
alias cd5=’cd ../../../../..’

We can even keep frequently executed cd commands and long path cd commands as alias so that we can reduce the time of typing them.

Example 16: We can even use function to create a new directories and change working directory to new directory with one command. We can use below function which should be kept in ~/.bashrc(for bash shell) and ~/.profile for each user

mdcd () { mkdir -p $1 && cd $1 }

Note: Save the .bashrc file once edited the source it otherwise we have to logout and login to take this effect.

Now we can use mdcd as command to create a directory and change to it.

Example 17: A function for changing the directory and listing the the content.

cdls () { cd $1 && ls }

Example 18: Use CDPATH built-in variable to store multiple directory structures so that we can directly change the director by just using name of sub directory you want to change.

For Oracle admins the main directory to work is /opt/oracle/oradba/oratemp
Now I set CDPATH to /opt/oracle/oradba/oratemp. From now on words you can just do cd oratemp and your cd command will directly take you to /opt/oracle/oradba/oratemp. Keep the below command in ~/.bashrc file and source it.

export CDPATH=/opt/oracle/oradba

Example 19: We can store multiple paths in CDPATH with : as separator. this is similar to PATH variable which stores location to binaries/executables.

export CDPATH=/opt/oracle/oradba:/var/fpt/pub/lead

The following examples are used in shell scripting to store changed directory locations into a stack for future uses.

Example 20: Use pushd command to change the directory and note these changes to a stack. This is similar to cd command.

pushd /var/ftp/pub

Example 21: To go to last working directory we can use popd, which will remove the directory entry from the stack


Example 22: To list all the directories in the stack


Example 23: The shopt is an excellent command used to set shell options. This command will give you full control what options are set and not set. With this command we can make cd to correct any spelling mistakes in the directory structure.

shopt -s cdspell

After this if you try to change directory /var/ftp but typed wrong as /var/fdp this error is taken care once the above option is set.

cd /var/fdp

Please share your thoughts on this.

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.