LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 06-29-2010, 08:44 PM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,268

Rep: Reputation: 49
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.
 
Old 06-29-2010, 08:52 PM   #2
dxqcanada
Member
 
Registered: Sep 2006
Location: Canada
Distribution: Gentoo
Posts: 702

Rep: Reputation: 43
You could make a script to do that ...

The * can achieve part of the functionality ...

Code:
$ cd /*/[directoryname]
 
1 members found this post helpful.
Old 06-29-2010, 08:58 PM   #3
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,268

Original Poster
Rep: Reputation: 49
Thanks. I'll get to it. Do you think the algorithm would better be recursive or iterative?

Last edited by stf92; 07-02-2010 at 06:16 AM.
 
Old 06-30-2010, 02:36 AM   #4
pcardout
Member
 
Registered: Jun 2003
Location: Socorro, New Mexico
Distribution: Debian ("lenny", "squeeze"), Linux Mint, XUbuntu
Posts: 218

Rep: Reputation: 24
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.

Last edited by pcardout; 06-30-2010 at 02:39 AM.
 
1 members found this post helpful.
Old 06-30-2010, 06:58 PM   #5
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora, Arch
Posts: 572

Rep: Reputation: 64
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.
 
Old 06-30-2010, 07:24 PM   #6
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,268

Original Poster
Rep: Reputation: 49
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.
 
Old 06-30-2010, 07:35 PM   #7
dxqcanada
Member
 
Registered: Sep 2006
Location: Canada
Distribution: Gentoo
Posts: 702

Rep: Reputation: 43
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.
 
Old 06-30-2010, 09:47 PM   #8
lumak
Member
 
Registered: Aug 2008
Location: Phoenix
Distribution: Arch
Posts: 799
Blog Entries: 32

Rep: Reputation: 109Reputation: 109
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...

Last edited by lumak; 06-30-2010 at 10:33 PM.
 
Old 06-30-2010, 10:18 PM   #9
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora, Arch
Posts: 572

Rep: Reputation: 64
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.
 
Old 06-30-2010, 10:58 PM   #10
padeen
Member
 
Registered: Sep 2009
Location: Perth, W.A.
Distribution: Slackware 14, Debian 7, FreeBSD, OpenBSD
Posts: 182

Rep: Reputation: 35
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.
 
Old 06-30-2010, 11:42 PM   #11
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,268

Original Poster
Rep: Reputation: 49
I'll have a lot of fun trying each solution! Thanks.
 
Old 06-30-2010, 11:54 PM   #12
allanf
Member
 
Registered: Sep 2008
Location: MN
Distribution: Gentoo, Fedora, Suse, Slackware, Debian, CentOS
Posts: 97
Blog Entries: 1

Rep: Reputation: 19
Quote:
Originally Posted by stf92 View Post
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
 
Old 07-01-2010, 12:13 AM   #13
allanf
Member
 
Registered: Sep 2008
Location: MN
Distribution: Gentoo, Fedora, Suse, Slackware, Debian, CentOS
Posts: 97
Blog Entries: 1

Rep: Reputation: 19
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).
 
1 members found this post helpful.
Old 07-01-2010, 12:14 AM   #14
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,268

Original Poster
Rep: Reputation: 49
Quote:
Originally Posted by allanf View Post
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.
 
Old 07-01-2010, 12:21 AM   #15
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,268

Original Poster
Rep: Reputation: 49
I'll do it. Thanks.
 
  


Reply

Tags
bashrc, directory


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Change KDE 4 effects from CLI??? jmite Linux - Newbie 3 11-18-2008 02:52 PM
can only change network with GUI, not CLI learnfast Linux - Newbie 2 05-03-2005 07:30 AM
How do I change my monitor via cli? TheBelush Linux - Newbie 2 03-30-2005 12:07 AM
How can I copy a directory and it's contents with CLI? CyberLord_7 Linux - General 2 03-30-2004 12:35 PM
Change cli login prompt? ipslave Slackware 1 05-06-2003 08:42 AM


All times are GMT -5. The time now is 04:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration