jboy Posted June 7, 2005 Report Share Posted June 7, 2005 I just came across this great shell command tip for easy subdirectory navigation in a terminal, so I thought I'd post it. Here's the source link It lists and numbers all subdirectories at your current location and you just enter the number to cd to that choice. It's like a menu for subdir navigation. alias dnf='OPTIONS=$(ls -aF |grep /$) ; select s in $OPTIONS; do cd $PWD/$s ; break ; done ' The author suggests adding this command to .bashrc There is also a version if you don't want to show hidden subdirs: alias dn='OPTIONS=$(ls -F |grep /$) ; select s in $OPTIONS; do cd $PWD/$s ; break ; done ' A great example of the flexibility and power of shell commands! [moved from Terminal Shell Commands, etc by spinynorman] Quote Link to comment Share on other sites More sharing options...
ianw1974 Posted June 7, 2005 Report Share Posted June 7, 2005 It worked mostly for me. It was only when I had directories that had words with spaces that it failed. What it did would put two entries, one for the first word, and then one for the second. Eg: I have a folder called "Linux Mandriva". Number 10 would be Linux Number 11 would be Mandriva If you have single worded directories, it works fine. I'll have to rename my directory structures for the others for it to work completely for me! :P Quote Link to comment Share on other sites More sharing options...
jboy Posted June 7, 2005 Author Report Share Posted June 7, 2005 It worked mostly for me. It was only when I had directories that had words with spaces that it failed. What it did would put two entries, one for the first word, and then one for the second. Well, I pulled out the old shell programming book and here is a big kludge, but it works (at least for spaces in directory names). alias dnf='OPTIONS=$(ls -aF | tr " " \000 | grep /$) ; select s in $OPTIONS; do cd "`echo $PWD/$s | tr \000 " "`" ; break ; done ' and (version that doesn't show hidden subdirs) alias dn='OPTIONS=$(ls -F | tr " " \000 | grep /$) ; select s in $OPTIONS; do cd "`echo $PWD/$s | tr \000 " "`" ; break ; done ' The kludge is to substitute Octal char \000 for spaces in the directory names display, then to translate \000 back to a space for the cd command. You just have to realize that a \000 char displays instead of a space for those dir names with embedded spaces. Hope that helps. Perhaps someone more experienced in shell programming can suggest a more elegant solution. Quote Link to comment Share on other sites More sharing options...
scarecrow Posted June 8, 2005 Report Share Posted June 8, 2005 What about the fine "shell command" named mc? :D Quote Link to comment Share on other sites More sharing options...
jboy Posted June 8, 2005 Author Report Share Posted June 8, 2005 (edited) What about the fine "shell command" named mc? No doubt - mc is an absolute, very versatile gem! I also like the Midnight Commander View Profile in Konqueror. This dnf alias is just kind of a toy, but I like it - I thought the author was very creative in coming up with it. I guess it's the CLI-geek in me coming through! Edited June 8, 2005 by jboy Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.