LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Fast change directory for the cli. (https://www.linuxquestions.org/questions/linux-general-1/fast-change-directory-for-the-cli-817078/)

stf92 06-29-2010 07:44 PM

Fast change directory for the cli.
 
Linux kernel 2.6, GNU (Slackware 12.0).

Hi:

In the old days of M$-DOS, there was the NCD (Norton Change Directory) utility. Anything of the sort in Linux? Explanatory note: you typed out the name of the directory you wanted to go to, that is, last element of the dir path.
And you were, ipso facto, in that dir. If that was the only one by that name, good. When not, and if that wasn't the intended dir, you typed the same command again and you were in a second dir of that name. If this was the intended dir, good. And so on.
It simply maintained a data base with the whole tree, and updated it when invoked from a newly created dir o by means of an option, NCD/R.

Regards.

dxqcanada 06-29-2010 07:52 PM

You could make a script to do that ...

The * can achieve part of the functionality ...

Code:

$ cd /*/[directoryname]

stf92 06-29-2010 07:58 PM

Thanks. I'll get to it. Do you think the algorithm would better be recursive or iterative?

pcardout 06-30-2010 01:36 AM

Using CDPATH to achieve the NCD functionality
 
dxqcanada sez:

Quote:

You could make a script to do that ...

The * can achieve part of the functionality ..
.

Nice trick! Two comments:

1) You can't do it in a script if by script you mean:
Code:

#!/bin/bash
# NCD.SH (Script to emulate NCD)
cd */$1
pwd

As this suffers from the newbie "gotcha" that, though your script did in fact go to the right directory, when it exits you are in a different shell and still in your original directory. Here's how you CAN do it.
It gives your standard cd command more mojo.

2) To your ~/.bashrc file add:
Code:

# This little beauty (CDPATH) allows you to type cd postit and go right to
# /home/joebob/favdirectory/work/hotprojects/postit.
export CDPATH=".:~:~/myfavdirectory:~/favdirectory/work:~/favdirectory/kids:~/favdirectory/work/hotprojects"

When you type cd postit it will find postit even if it is buried
in /home/joebob/favdirectory/work/hotprojects.

KenJackson 06-30-2010 05:58 PM

Most bash users probably approach this a little different: tab expansion.

To change to favdirectory/work/hotprojects, I would type

f<tab>w<tab>h<tab><enter>

The tab character fills in the remaining part of the directory for me. But if there are two directories or files that start with f, nothing would happen, so I would press the a and try again, etc.

If I forget how it's spelled, I press tab a couple more times until it displays all my choices.

stf92 06-30-2010 06:24 PM

Unfortunately, I create directories in such a way that 6-element paths and up is normal usage for me. Worsened by my having to begin by the name of the partition as the first element. Also, I'm a lazy typer. The n-c-d key sequence is extremely fast to type and then, thumb over the space bar and just a few chars.

It is because I was tired of pressing the TAB key that I posted these letters. And pcardout's idea could work fine for the most visited paths. But falls short of my idea. I did find a program named ytree on the web. But it's not a hundred per cent cli-ish. Well, thanks for your time.

dxqcanada 06-30-2010 06:35 PM

I did not mean that that particular command would be the script ...

If there isn't already a tool that can do this ... a script could be written to do it.

lumak 06-30-2010 08:47 PM

I would think that it would be best to write a small script that had your most visited directories and would try those after the current directory is processed. OR something that searched the output of the find command...

find ${DIRLIST} -type d -name $SEARCH 2> /dev/null

then something that selects the directory to cd to.

you can modify $SEARCH to '*'${SEARCH}'*' if you wanted to get any directory containing the pattern. I have issues sometimes when bash tries to interpret the * so I quote it.

If your DIRLIST is too broad or has too many sub directories, then you will get a lot of directories you probably don't want. So you might want to limit that to a few key places that you are working with a lot.

If you want to do it windows style, use "-iname" for a case insensitive search.


Hrmmms actually, I think I found nearly the exact command to place in a script with very minimal typing.

Code:

#!/bin/bash
SEARCH=$1
DIRLIST="$HOME /some/share"
FINDLIST=$(find $DIRLIST -type d -iname \*${SEARCH}\* 2> /dev/null)
echo Press Ctrl-C after pressing enter to exit at the directory you pressed enter on.
for CUR in $FINDLIST; do
echo cd $CUR ?
read
cd $CUR
done

that was fun.

EDIT YET AGAIN:
I think it actually has to be a function that you source to your bash profile so that you remain in the directory after you cd to it...

KenJackson 06-30-2010 09:18 PM

For years I've been doing something like what others in this thread have suggested.
In my ~/.bash_profile file I have this line:
Code:

alias prj=". ~/bin/prj"
And ~/bin/prj has this general form:
Code:

case "$1" in
    "")  echo "Usage: prj PROJECT" ;;
    a)        cd ~/prj/alpha      ;;
    e)        cd ~/prj/echo-stuff ;;
    s)        cd ~/prj/scripts    ;;
    *) test -d ~/prj/"$1"  &&  cd ~/prj/"$1"  ||  echo " CAN'T FIND $1"
esac

So from any directory I can enter prj a and I am instantly transported to my ~/prj/alpha directory.

You could extend this in obvious ways.

padeen 06-30-2010 09:58 PM

You might get similar behaviour to what you want with a combination of CDPATH and the shopt option 'cdspell'. I have some of my $HOME subdirectories in CDPATH and I can just start to type their name then a <TAB> and jump straight to them. shopt -c cdspell tells Bash to correct small spelling errors automatically.

What this won't give you is a second attempt in the event the first suggestion is not the dir you're looking for, but you might find this suggestion is good enough.

stf92 06-30-2010 10:42 PM

I'll have a lot of fun trying each solution! Thanks.

allanf 06-30-2010 10:54 PM

Quote:

Originally Posted by stf92 (Post 4018820)
Linux kernel 2.6, GNU (Slackware 12.0).

Hi:

In the old days of M$-DOS, there was the NCD (Norton Change Directory) utility. Anything of the sort in Linux? Explanatory note: you typed out the name of the directory you wanted to go to, that is, last element of the dir path.
And you were, ipso facto, in that dir. If that was the only one by that name, good. When not, and if that wasn't the intended dir, you typed the same command again and you were in a second dir of that name. If this was the intended dir, good. And so on.
It simply maintained a data base with the whole tree, and updated it when invoked from a newly created dir o by means of an option, NCD/R.

Regards.

There are the "pushd",and "popd".
The environment variable "CDPATH".
Also cd -, cd-3, cd +2, etc

allanf 06-30-2010 11:13 PM

If this need is to move around within a software project. Look into vim (gvim is the gui enabled version) and the "ctags" program. It allows the move to the definition under the cursor (or from a command) and has return to the previous location (multiple returns).

stf92 06-30-2010 11:14 PM

Quote:

Originally Posted by allanf (Post 4020036)
There are the "pushd",and "popd".
The environment variable "CDPATH".
Also cd -, cd-3, cd +2, etc

Yes. I see pushd, popd in bash man. I learned about CDPATH from another poster. cd - I used it. Didn't know about cd -N. I think here's stuff to make a pretty beateuful program. Definitely bash deserves a careful study of its manual and no arguments about lengthiness are valid. It one wants (I) to write bash scripts well then... the ultimate source of knowledge is the manual. Thanks for your post and regards.

stf92 06-30-2010 11:21 PM

I'll do it. Thanks.


All times are GMT -5. The time now is 12:12 AM.