Tuesday, July 17, 2012

23 Linux cd command examples

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.

Example1: Change working directory to /abc/xyz

cd /abc/xyz

Example2: Change directory from present working directory to /var/ftp to /var/ftp/pub using absolute path.
cd /var/ftp/pub

Example3: Change directory from present working directory /var/ftp to /var/ftp/pub using relative path.

cd pub

Click here more about Absolute and Relative path.

Example4: Change working directory to user’s home directory from anywhere in the directory structure


cd ~


Example5: Sometimes we want to change directory to previous working directory which we changed from. And revert to previous working directory we can use same command. This is vary much useful cd command.

cd -

Example6: Change working directory to parent directory.

cd ..

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

cd .

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

cd ../..

Exampl9: How about changing three levels up in the directory.
cd ../../..

And so on how many directories levels up you want to change that many you can do.

Example10: I am in /var/ftp folder and I want to change the directory to /etc/samba with single command. How can I do that?

cd ../../etc/samba

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

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

cd /var/ft?

This will change the directory to /var/ftp

Example12: 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.

Example13: Change directory to /home/surendra/redhat/ubuntu with out typing all the characters using

cd /h/s/r/u

Note: Press tab to complete directory name.

Example14: 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.

Example15: We can even use function to create a new directorie 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.

Example16: A function for changing the directory and listing the the content.

cdls () { cd $1 && ls }

Example17: 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

Example18: 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.

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

pushd /var/ftp/pub

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


Example21: To list all the directories in the stack


Example23: 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.