Jump to content

Shell Command for Easy Subdir Navigation


jboy
 Share

Recommended Posts

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]

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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! :lol2:

Edited by jboy
Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...