I know the command for moving up one directory is cd ..
Is there any command to move down one directory?
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
..
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
.
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/
.
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.
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.
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]$
cdd
is a small bash script that's a power tool for the cd
command. Visit the link below for a one minute video and copy the code below if you like what you see.
cdd - a Change Directory (cd) Power Tool 🔗
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
When you call cdd
and there is more than one sub-directory you need to pick one:
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$
cd
to?cd -
, it will take you to previous directory, in case you are trying to revert fromcd ..