The cd command is a bash command in linux is meant for changing the directory in a terminal environment. This post is about a hacking the cd comment.
There is an environment variable called PATH related to the cd command. When you type a command, your system looks through all the directories defined in the PATH environment variable until it finds the command. If it doesn't find the command by the time, it tells you it can't find the file. There's a similar environment variable called CDPATH. Let's use the CDPATH for the tweak.
There is an environment variable called PATH related to the cd command. When you type a command, your system looks through all the directories defined in the PATH environment variable until it finds the command. If it doesn't find the command by the time, it tells you it can't find the file. There's a similar environment variable called CDPATH. Let's use the CDPATH for the tweak.
The basic syntax of cd command is
$ cd somewhere
We can use this CDPATH to avoid typing an entire path of the frequently accessed directories..
Here's how it works.
/home/blogger/Documents/nw/folder1
/home/blogger/Documents/nw/folder2
/home/blogger/Documents/nw/folder3
/home/blogger/Documents/nw/folder1
/home/blogger/Documents/nw/folder2
/home/blogger/Documents/nw/folder3
Here's what you want to do. You want to be able to jump right to the folder2 directory without having to type the entire path. Here's how to set the CDPATH environment variable so that you can do that:
$ export CDPATH=:.:/home/blogger/Documents/nw
This command does not include any of the chapter directories in the CDPATH. That's because you want the CDPATH to define the directory immediately above the directory you want to find. Remember, the system searches the directories listed in the path for the contents you specify on the command line. The argument folder2 is what you're looking for. folder2 exists in the directory /home/blogger/Documents/nw, so that is the directory you want to include in your search CDPATH.
If you need to access the directory folder2, you only need to type this
$ cd folder2
The command should take you immediately to the directory /home/blogger/Documents/nw/folder1
The problem ???
Suppose you also created the following directories according to the same pattern mentioned earlier:
/home/blogger/Documents/internet/folder1
/home/blogger/Documents/internet/folder2
/home/blogger/Documents/internet/folder3
Naturally, you have to add a new path to your CDPATH environment variable:
$ export CDPATH=:.:/home/blogger/Documents/nw:/home/blogger
/Documents/internet
As with the PATH environment variable, you separate different search paths with a colon. The system searches through each path from left to right.
Now issue this command:
As with the PATH environment variable, you separate different search paths with a colon. The system searches through each path from left to right.
Now issue this command:
$ cd folder2
The problem is that, it puts you in the same place as before, /docs/pub//home/blogger/Documents/nw/folder3. That is not what you expected or desired. But it happened because the cd command searches the CDPATH environment variable paths from left to right. It searched the linuxhacks location first and found folder2. So, that's where it assumed you wanted to go.
The Solution
One way to avoid the problem described earlier is to define the CDPATH in this way:
One way to avoid the problem described earlier is to define the CDPATH in this way:
$ export CDPATH=:.:/home/blogger/Documents/
If you want to get to folder of internet, you simply type the following:
$ cd internet/folder2
Another cd Trick
This is one among the simplest of all tricks for cd, yet few people know of it. Suppose you are working in the directory /home/blogger/Documents/nw and then you change to the directory /usr/X11R6/lib/X11/fonts/TrueType. You can jump back to the previous directory simply by typing the following:
$ cd -
This simple command returns you to the last directory you were using before you changed to the current one.
0 comments:
Post a Comment
speak out... itz your time !!!