Breaking

Saturday, 19 May 2018

Powerful CD Command Hacks: Hack 1. Define CD Base Directory Using CDPATH

cd is one of the most frequently used commands during a UNIX session.
The 6 cd command hacks mentioned in this chapter will boost your
productivity instantly and make it easier to navigate the directory
structure from command line.

Hack 1. Define CD Base Directory Using CDPATH

CDPATH

If you are frequently performing cd to subdirectories of a specific parent
directory, you can set the CDPATH to the parent directory and perform
cd to the subdirectories without giving the parent directory path as
explained below.

# pwd
/home/ramesh
# cd mail
-bash: cd: mail: No such file or directory
[Note: The above cd is looking for mail directory under
current directory]
# export CDPATH=/etc
# cd mail
/etc/mail
[Note: The above cd is looking for mail under /etc and not
under current directory]
# pwd
/etc/mail


To make this change permanent, add export CDPATH=/etc to your
~/.bash_profile
Similar to the PATH variable, you can add more than one directory entry
in the CDPATH variable, separating them with : , as shown below.

export CDPATH=.:~:/etc:/var

This hack can be very helpful under the following situations:
• Oracle DBAs frequently working under $ORACLE_HOME, can set
the CDPATH variable to the oracle home
• Unix sysadmins frequently working under /etc, can set the
CDPATH variable to /etc
• Developers frequently working under project directory
/home/projects, can set the CDPATH variable to /home/projects
• End-users frequently accessing the subdirectories under their
home directory, can set the CDPATH variable to ~ (home
directory)

No comments:

Post a Comment