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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
06-29-2010, 07:44 PM
|
#1
|
|
Senior Member
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 2,487
Rep:
|
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.
|
|
|
|
06-29-2010, 07:52 PM
|
#2
|
|
Member
Registered: Sep 2006
Location: Canada
Distribution: Gentoo
Posts: 702
Rep:
|
You could make a script to do that ...
The * can achieve part of the functionality ...
Code:
$ cd /*/[directoryname]
|
|
|
1 members found this post helpful.
|
06-29-2010, 07:58 PM
|
#3
|
|
Senior Member
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 2,487
Original Poster
Rep:
|
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 05:16 AM.
|
|
|
|
06-30-2010, 01:36 AM
|
#4
|
|
Member
Registered: Jun 2003
Location: Socorro, New Mexico
Distribution: Debian ("lenny", "squeeze"), Ubuntu ("karmic", "oneiric")
Posts: 214
Rep:
|
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 01:39 AM.
|
|
|
1 members found this post helpful.
|
06-30-2010, 05:58 PM
|
#5
|
|
Member
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora, Arch
Posts: 538
Rep:
|
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.
|
|
|
|
06-30-2010, 06:24 PM
|
#6
|
|
Senior Member
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 2,487
Original Poster
Rep:
|
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.
|
|
|
|
06-30-2010, 06:35 PM
|
#7
|
|
Member
Registered: Sep 2006
Location: Canada
Distribution: Gentoo
Posts: 702
Rep:
|
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.
|
|
|
|
06-30-2010, 08:47 PM
|
#8
|
|
Member
Registered: Aug 2008
Location: Phoenix
Distribution: Arch
Posts: 799
Rep: 
|
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 09:33 PM.
|
|
|
|
06-30-2010, 09:18 PM
|
#9
|
|
Member
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora, Arch
Posts: 538
Rep:
|
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.
|
|
|
|
06-30-2010, 09:58 PM
|
#10
|
|
Member
Registered: Sep 2009
Location: Perth, W.A.
Distribution: Slackware 14, Debian 6, FreeBSD, OpenBSD
Posts: 104
Rep:
|
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.
|
|
|
|
06-30-2010, 10:42 PM
|
#11
|
|
Senior Member
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 2,487
Original Poster
Rep:
|
I'll have a lot of fun trying each solution! Thanks.
|
|
|
|
06-30-2010, 10:54 PM
|
#12
|
|
Member
Registered: Sep 2008
Location: MN
Distribution: Gentoo, Fedora, Suse, Slackware, Debian, CentOS
Posts: 92
Rep:
|
Quote:
Originally Posted by stf92
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
|
|
|
|
06-30-2010, 11:13 PM
|
#13
|
|
Member
Registered: Sep 2008
Location: MN
Distribution: Gentoo, Fedora, Suse, Slackware, Debian, CentOS
Posts: 92
Rep:
|
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.
|
06-30-2010, 11:14 PM
|
#14
|
|
Senior Member
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 2,487
Original Poster
Rep:
|
Quote:
Originally Posted by allanf
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.
|
|
|
|
06-30-2010, 11:21 PM
|
#15
|
|
Senior Member
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 2,487
Original Poster
Rep:
|
I'll do it. Thanks.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:14 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|