4

I know the command for moving up one directory is cd ..

Is there any command to move down one directory?

2
  • 12
    How is bash supposed to know which subdirectory you want to cd to? – edwinksl Jun 29 '17 at 15:24
  • 4
    Maybe you need cd - , it will take you to previous directory, in case you are trying to revert from cd .. – user311189 Jun 29 '17 at 16:12
6

The way by which you want to go down by one directory is not possible because there is only one parent directory which is denoted by .., but there can be multiple directories inside a directory and thus you have to mention the one you mean. You have to use cd example to move down by one onto the directory example.

But there is a shortcut: use Tab completion for this. To move down by one into the example directory, you could enter cd e and press Tab and if example is the only directory beginning with e the shell will automatically complete the command to cd example. If there are others beginning with e, double Tab will list matches, and you can type ex or whatever is needed and press Tab again and so on.

11
  • 1
    And if there's only one subdirectory, TAB will automatically fill it in even if you don't type any letters. – Barmar Jun 29 '17 at 19:43
  • 3
    Tab won't type the wrong directory. It will only expand to as much is unique, then you have to enter more characters. If you press Tab again it will show all the possible completions. – Barmar Jun 29 '17 at 19:46
  • Perhaps what is really meant by the question is how to get back to the current directory before the cd ... – Peter Mortensen Jun 29 '17 at 21:41
  • @Barmar Yes, you're right. I think Smit is thinking of Windows, which does have that behaviour. – wjandrea Jun 29 '17 at 22:13
  • @Barmar "tab will type wrong directory "mean it will type something other directory with same first chars which OP doesn't want if there are multiple directories. – noone Jun 30 '17 at 3:39
13

The main difference between moving up a directory, and moving down a directory, in the directory tree is:

  • Moving up - there is only one option, hence the command doesn't need to mention the folder name:

    cd ..

  • Moving down - there might be several sub-directories, hence you first need to choose sub-directory, to change directory down into example-dir the command is:

    cd example-dir

1
  • Well; .. is the name of the directory. It's just the name of the parent directory, which is present in every directory in the file system. So technically you are naming the directory that you want to move into to cd. – user Jun 29 '17 at 20:31
12

In older versions of bash (e.g: 4.3) you could do cd * and it would go to the first sub directory it found:

$ ls
dir1/   dir2/   dir3/

$ cd *

Now we are in dir1/.

2
  • 1
    cd * works perfectly for all those nested directories that only have one sub. (In Ubuntu 16.04.6 LTS at least) Thanks! – WinEunuuchs2Unix Sep 19 '20 at 22:13
  • This needs to be the accepted answer. – MadmanLee Mar 10 at 18:52
2

You can move up cause because there is one directory but when we talk about going down there can be more than one that is why we have ls we will list all the available directory and then we change directory one down in which we needed to go by the command cd examle1.

$ ls
dir1 dir2 sir3
$ cd dir1
$ cd ..
$ cd dir2

And so on.

0

This doesn't answer exactly the question that was asked, but as was pointed out by someone else, that question didn't really make sense anyway.

But here is the answer to a nearby question. To go back to the directory from whence you came, use cd -

[mike@gwydion Work]$ cd ..
[mike@gwydion ~]$ cd -
/home/mike/Work
[mike@gwydion Work]$ 

If you want to save a directory for later reference, do some stuff in between in a bunch of other directories, and then go back, use pushd and popd:

[mike@gwydion Work]$ pushd .
~/Work ~/Work
[mike@gwydion Work]$ cd ~/Temp/
[mike@gwydion Temp]$ run some command

[mike@gwydion Temp]$ cd ~/SourceCode/
[mike@gwydion SourceCode]$ run some command
[mike@gwydion SourceCode]$ popd
~/Work
[mike@gwydion Work]$ popd
bash: popd: directory stack empty
[mike@gwydion Work]$ 
0

It's annoying have to type out long sub-directory names with spaces. So I created a little function in ~/.bashrc that automatically changes down one sub-directory level if there is only one sub-directory. If there is more than one sub-directory then zenity is used to pick one to change to.

cdd () {

    OFS="$IFS"                                  # Save Input File Separator
    IFS=$'\n'
    # Bash array of directories names
    dirs=( $(find . -maxdepth 1 -type d | grep './' | cut -c 3- | sort) )

    # If no directories exit
    if [ "${#dirs}" -eq 0 ] ; then
        echo "No subdirectories"
        IFS="$OFS"                              # Restore Input File Separator
        return
    fi

    # Only one directory then change to it and exit
    if [ "${#dirs[@]}" -eq 1 ] ; then
        cd "${dirs[0]}"
        IFS="$OFS"                              # Restore Input File Separator
        return
    fi

    # Zenity to pick one of the directories
    result=$(zenity --list --width=600 --height=400 --title="down" \
             --column="Sub-Directories" "${dirs[@]}" \
             2> >(grep -v 'GtkDialog' >&2))     # Suppress Gtk annoying message
    IFS="$OFS"                                  # Restore Input File Separator
             
    if [ ! -z "$result" ] ; then                # Did they select an item?
        cd "$result"
    fi
}

Copy above code into your ~/.bashrc file and then open a terminal and type cdd to use it.

If terminal is already open, you can resource your revised ~/.bashrc file with:

source ~/.bashrc

or

. ~/.bashrc

Usage

When you call cdd and there is more than one sub-directory you need to pick one:

cdd.png

In this screen if you click OK button it is equivalent to:

cd Seven\ Mary\ Three

or:

cd "Seven Mary Three"

When you call cdd and there is only one sub-directory it automatically changes to it:

rick@alien:~/Music/Seven Mary Three$ cdd
rick@alien:~/Music/Seven Mary Three/American Standard$ 

When you call cdd and there are no sub-directories an error is displayed:

rick@alien:~/Music/Seven Mary Three/American Standard$ cdd
No subdirectories
rick@alien:~/Music/Seven Mary Three/American Standard$ 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.