I once made a useful script for a similar purpose, with a function fcd for find-n-cd.
fcd foo and 3 things may happen:
- No such directory foo is found, then it returns with
"no such directory " $1
- One file is found: It tests whether it is a directory (or just a file) and if so, it cds there.
- Multiple files are found - then a selection is presented, where you just type the number of the selection (or a special number for return).
It is similar in that it doesn't need you to type the whole path, but you have call the function explicitly.
# find cd. For input foo, find all directories .../.../foo
# GPLv3 Stefan Wagner (2010, 2012)
# doesn't handle blanks in directory names gracefully.
list=$(locate $1 | egrep "/$1$")
count=$(echo $list | wc -w )
case $count in
echo "unknown directory: "$1 && return
# could search for partial matches Doc => Documentation
if [[ -d "$list" ]]; then
echo "not a directory: $1"
select directory in $list "/exit/"
if [[ "$directory" = "/exit/" ]]; then break; fi
if [[ -d "$directory" ]]; then
echo "not a directory: "$1
You have to
source the function (
. fcd.sh) and can't call it as script, because
cd would else only happen in the context of the script, and after finishing you would happen to be back in your starting dir immediately.
Since it works with
locate, it is pretty fast in finding directories (but not always up to date).
It doesn't handle blanks in directory names gracefully. If you have an elegant solution for the problem I would be happy.