2

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

Is there any command to move down one directory?

  • 12
    How is bash supposed to know which subdirectory you want to cd to? – edwinksl Jun 29 '17 at 15:24
  • 3
    Maybe you need cd - , it will take you to previous directory, in case you are trying to revert from cd .. – sdkks 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.

  • 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

  • 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. – a CVn Jun 29 '17 at 20:31
9

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

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]$ 

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.