Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way in a unix shell (specifically Ubuntu) to change directory into the xth directory that was printed from the ls command? I know you can sort a directory in multiple ways, but using the output from ls to get the xth directory?

An example shell:

$ ls
$ first_dir second_dir third_really_long_and_complex_dir

where I want to move into the third_really_long_and_complex_dir by passing 3 (or 2 in proper array format). I know I could simply copy and paste, but if I'm already using the keyboard, it would be easier to type something like "cdls 2" or something like that if I knew the index.


For anyone still seeing this, I created this question back when I was really new to linux/bash and didn't realise there was such a thing as tab-completion.

share|improve this question

migrated from Nov 9 '12 at 18:49

This question came from our site for professional and enthusiast programmers.

up vote 0 down vote accepted

The main problem with cd in an interactive session is that you generally want to change the current directory of the shell that is processing the command prompt. That means that launching a sub-shell (e.g. a script) would not help, since any cd calls would not affect the parent shell.

Depending on which shell you are using, however, you might be able to define a function to do this. For example in bash:

function cdls() {
    # Save the current state of the nullglob option
    SHOPT=`shopt -p nullglob`

    # Make sure that */ expands to nothing when no directories are present
    shopt -s nullglob

    # Get a list of directories

    # Restore the nullblob option state

    # cd using a zero-based index
    cd "${DIRS[$1]}"

Note that in this example I absolutely refuse to parse the output of ls, for a number of reasons. Instead I let the shell itself retrieve a list of directories (or links to directories)...

That said, I suspect that using this function (or anything to this effect) is a very good way to set yourself up for an enormous mess - like using rm after changing to the wrong directory. File-name auto-completion is dangerous enough already, without forcing yourself to count...

share|improve this answer
This works brilliantly on my system. Thankyou – Benjamin Kaiser Nov 9 '12 at 17:19

I have a function for this:

   function cdi. {
       first=$(( 1 + LPP * PAGE - LPP ))
       last=$(( LPP * PAGE ))
       select cd_into in $(find . -maxdepth 1 -type d -printf "%P\n" |\
             sed -n $first,${last}p);
           if [[ $REPLY == n ]]; then let PAGE=PAGE+1; cdi. $PAGE $LPP ; EXIT=$?
         elif [[ $REPLY == p ]]; then let PAGE=PAGE-1; cdi. $PAGE $LPP ; EXIT=$?
         elif [[ $REPLY == m ]]; then let LPP=LPP*2;   cdi. $PAGE $LPP ; EXIT=$?
         elif [[ $REPLY == l ]]; then let LPP=LPP/2;   cdi. $PAGE $LPP ; EXIT=$?
         elif [[ $REPLY == q || $REPLY == 0 ]];then return 0; fi

           if [ $EXIT -eq 0 ]; then return 0; fi

           if [ -d "$cd_into" ];then
               cd "$cd_into"; return 0; else
               echo "oops! \`$cd_into' is not a folder (?)"
       unset IFS

To use:

$ cdi.
1)      3) .swt             5) tmp              7) .vim             9) .ssh
2) .crack-attack     4) Downloads        6) .Eterm           8) .frozen-bubble  10) .irssi
Which #?:
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .