As of 2016-02-26, there will be no more posts for this blog. s/blog/pba/
Showing posts with label directory. Show all posts

Have you ever read cds manpage? If you havent, then you should if you dont even know what cd or cd - does. The manpage is of POSIX Programmers Manual, so the content is not only for the Bash, but for every POSIX-compliant shell.

1   cd brings you(r) home

It means literally, like, seriously bringing you home or bringing your home to you.

/path/somewhere $ cd
~ $

I believe many people have discovered this on their own like me. You just happen to run cd without giving any argument, then you wowd for finding out this quick way to get back to home.

I remember once I was talking to someone, who didnt even know about this. Its more amazed for me than for him, I wondered how on Earth this guy had been doing in order to get back to home.

2   cd - for last directory

I dont use this argument - often, but when I need it, itd save a lot of typing time especially you keep switching between two directory.

~ $ cd /path/to/somewhere/deep
/path/to/somewhere/deep $ # do somegthing
/path/to/somewhere/deep $ cd /path/to/other/place/way/way/deep
/path/to/other/place/way/way/deep $ # do something
/path/to/other/place/way/way/deep $ cd -
/path/to/somewhere/deep $ # now we are back
/path/to/somewhere/deep $ cd -
/path/to/other/place/way/way/deep $ # we are back to the other

Note

If you actually have a - directory, use cd ./- to get into it.

3   CDPATH is directory search path

Before I read the manpage, I had never thought of it, even I seemed to have heard of it or seen it. Its like PATH, but only for searching directories when using cd.

~ $ mkdir foobar
~ $ CDPATH=$HOME
~ $ cd /
/ $ cd foobar
/home/login/foobar $ # bang!

You can enter the ~/foobar from anywhere, just by typing cd foobar. Its like you can run foobar from anywhere if its executable and its searchable via PATH.

It can also be auto-completed via bash-completion. On Gentoo, run eselect bashcomp enable base to enable it.

Although I find this could be useful, but I prefer more specific way to change directory, like my own g script for quick directory switch.

4   cd !$ comes in handy

This relies on Bashs History Expansion, so this is not for every shell. But its very useful when the last argument is a directory from previous command, and you need to get into for further actions. !$ is your best friend.

~ $ mkdir long/long/directory
~ $ cd !$
~/long/long/directory $

This is probably my own oldest script and still being used besides .bashrc.

This g script was born at 2007-12-26T03:01:29+0800. (Yep, I am a timestamping nuts) Four years ago, I wrote a post about how to use it, but I doubt anyone has ever wanted to use it.

Its a simple script for quickly switching working directory. You can switch with index or keyword, it also supports Bash completion. There are a lot of script like this around, but I am happy with mine, even it had some issues.

After these years, I finally updated it to adding keyword support which I always wanted. I used to memorize the indexes of those directories, it wasnt a big issue for me. Besides that, a Makefile was written for easy installation.

Other than fixing some syntax and weird scripting (still read weird), its basically the same as you read in that old blog post.

If you havent tried directory switcher, why not try mine and create some issues for me to fix? (I am sure there are plenty to fix)

You can use this to save some frequently used directories and easily switch to one of them by choosing from a list or by Bash completion.

I have been using this script since last year. Until days ago, I finally rewrote it with Bash completion functionality. You can read the source code. Licensed under the MIT License.

Warning

The content in this page may be outdated, please follow the instructions on project website, instead.

1   Usage & Installation

There are two ways to use this script:

  1. Run it as a normal Bash script
  2. Source it and use the Bash function g() inside this g script.

I prefer latter, because you can use it with Bash completion and you dont need to prefix a dot when switching directory. I will explain how to use in either way.

1.1   Bash Script Way

Firstly, download this g script to a place you like, say /path/to/g. You can run /path/to/g -h to see what options you have. Here is a sample:

Commands:
      g dir        : change working directory to dir
      g (-g|g)     : get a list of directories
      g (-a|a)     : add current directory
      g (-a|a) dir : add dir
      g (-r|r)     : remove a directory from list
      g (-h|h)     : show what you are reading right now

Before you can really use this g script, you need to save some directories. Use cd to one of the most used directories. Then, run /path/to/g -a to add current directory to ~/.g_dirs (default file). Once you have added some, run:

. /path/to/g g

That dot at beginning and g letter at tail are very important. You will see a list of directories, which you have just added. Choose one, enter the number and hit Enter key to switch directory.

If you want to remove just run:

/path/to/g -r

The dot above is doing sourcing a Bash script, which is necessary to let directory changing takes effect in Bash shell when you want to do that by running a script.

1.2   Bash Function Way

I assume that you want to use Bash completion with this g script. You need to do few things first:

  1. Install the Bash completion package for your distro. On Fedora, run yum install bash-completion.
  2. Save this script to /path/to/g.
  3. In you home directory, run ln -s /path/to/g .bash_completion. If you want to make it system-wide, switch to /etc/bash_completion.d, then run ln -s /path/to/g .bash_completion as root.
  4. (Optional) Normally, TAB is the key to get the list, but usually you still need to type some more. You can make it to cycle the options in the list. You only need to have TAB: menu-complete in ~/.inputrc. Once again for system-wide, put that in /etc/inputrc.

More about inputrc, I dont want to change the behavior of TAB, so I have:

"\e[6~": menu-complete
#TAB: menu-complete

set completion-ignore-case On

\e[6~ allows me to use PAGEDOWN to cycle options, and TAB still gives me a list.

After you finishing steps above, open another terminal. Type g and hit ENTER key, you should see a help menu like the one in Bash Script Way. If not, there is something wrong.

Then you will need to add some directories, run g -a to add current directory. After you add some, run g again, you should have a list of them. You can choose one from them to switch directory.

Now, type g and TAB or PAGEDOWN (if you have same ~/.inputrc like mine). The directories should be cycling, press ENTER when you get the one you want.

In this way, you run a Bash function not a script. Here is a screenshot of using as a function:

https://lh3.googleusercontent.com/-N0rvG2OrEPA/SPqAbXSrnRI/AAAAAAAABW0/i1ziwB7FjT0/s800/Screenshot-g.png