Executive Summary (TL;DR)
- Use
pwd
to know where you are (or look between the :
and the $
in your prompt).
- Use
cd
to change directory elsewhere. Unlike in Windows, cd
must always be followed by a space; commands like cd/
and cd..
will not work, but cd /
and cd ..
will.
Knowing Where You Are
Way 1: Run pwd
.
What directory you're currently in is shell-specific (and more generally, application-specific). You could be running one shell where you are in one directory, and another shell where you are in another directory.
To know what directory you are currently in, run:
pwd
For example (from a shell I happen to have open now, on my machine):
ek@Kip:~$ pwd
/home/ek
This tells me that I am in the directory /home/ek
. /home
is (somewhat confusingly) the directory that contains human users' home directories (it is not itself any user's home directory). The human user username
has home directory /home/username
.
The root
user does not have a home directory in /home
. Instead, root
's home directory is /root
. This is not to be confused with the root directory, which is /
.
Way 2: Examine your prompt.
The default configuration for your prompt is that it tells you what directory you are currently in. Your prompt is the text that appears to notify you that the shell is ready to accept a command.
In the example you showed us in your question, your prompt is:
christy@ubuntu:~$
christy
is your username. The @
character separates it from ubuntu
, your computer's host name. (If you're running a live CD, its hostname will be ubuntu
, and some people choose that as their computer's name during installation, too.)
The :
character separates it from the name of the directory you are currently in. In this case, that is represented by ~
. ~
is shorthand for your home directory. When you're not in your home directory, you should see a full directory name. For example:
ek@Kip:~$ cd /var/log
ek@Kip:/var/log$
(To finish up what each part of the prompt means: For prompts that follow this convention, either a $
or a #
character appears. $
, as in this case, means it's a normal user shell. A #
character would mean it is a root
shell.)
Changing Where You Are
Why your "cd" command did not work.
To change directory, use the cd
command.
You ran cd/
. This does not work because you did not actually run the cd
command. In Ubuntu and other Unix-like operating systems (actually, in Unix-style shells like bash
, the shell you are using), the name of a command is considered to end only at a space or the end of the line.
So unlike in the Command Prompt in Windows (where cd\
is interpreted the same as cd \
and cd..
is interpreted as the same as cd ..
), in bash
(the shell you're using in Ubuntu), cd/
is not a valid command. You must use cd /
.
cd/
means "the entry in the current directory whose name is cd
and which is also a directory." Whether or not such a subdirectory exists, it cannot be run as a command, so either way you'll get an error (though what error you get will differ):
ek@Kip:~$ cd/
bash: cd/: No such file or directory
ek@Kip:~$ mkdir cd
ek@Kip:~$ cd/
bash: cd/: Is a directory
Moving to Your Home Directory
To go to your home directory, run cd
by itself without any arguments:
cd
Some people prefer to name their home directory explicitly. You can use its full name, or
cd ~
But cd
by itself is sufficient.
Moving Up One Directory
To change directory to the current directory's parent directory (i.e., the directory that contains it), use:
cd ..
..
represents the current directory's parent directory. If you are in the root directory (/
), there is an exception: ..
just represents /
. So running cd ..
moves up one directory when run anywhere but /
; when run in /
, you stay in /
.
Every directory contains a ..
entry. They also all contain a .
entry which refers to the current directory itself. It's not very to run cd .
though. You always stay where you are.
Absolute and Relative Paths
If you cd
to something that does not start with a /
, then it tries to go to that directory within the current directory.
For example, if you ran
cd var/log
Then it would try to go into the var
subdirectory of where you currently are, and into the log
subdirectory of that. Unless both exist, and in those places, that cd
command will fail (and you will remain where you were originally).
If you want to go to the log
subdirectory of the var
subdirectory of the root directory, run this instead:
cd /var/log
Only if you are currently in /
is cd var/log
equivalent to cd /var/log
.
A path that starts with /
is absolute. The way it is resolved does not depend on where you currently are.
The path ~
is absolute too, because it is (essentially) shorthand for $HOME
. HOME
is an environment variable, and the expression $HOME
expands to the full, absolute path of the current user's home directory.
~
notation can also be used to represent another user's home directory. If you write ~username
, this represents username
's home directory.
Getting Back to Where You Were Before
The directory you are currently in is stored in the PWD
environment variable. The last directory you were in is stored in the OLDPWD
environment variable. You can view OLDPWD
the same way you'd view any environment variable:
echo $OLDPWD
Because this information is stored, cd
is able to have a special, quick and easy way to get back to where you were before. As pst007x says, to go to the last directory you were in, run:
cd -
If there was never any earlier directory you were in in that shell, then the OLDPWD
environment variable will not have been set, and trying to run cd -
will result in an error (and you'll stay where you are):
ek@Kip:~$ cd -
bash: cd: OLDPWD not set
Changing Where You Are Like A Boss
pushd
and popd
: The Directory Stack
Imagine a stack of directory names. By a stack, I mean something where when you add something it goes on the top, and you can only remove one thing at a time, and the thing you remove is always what's currently on the top.
You can push any directory dir
onto the stack by running:
pushd dir
If you want to push the current directory to the top of the stack, you can use .
(which, as explained above, always represents the current directory):
pushd .
Then you can go about your business, changing directories as much as you like. When you want to go back to the last directory you pushed onto the stack with pushd
, run:
popd
This goes to that directory, and also pops it off the stack. Now the stack is one directory shorter. If that was the only directory on the stack, then the stack is now empty.
You can have a stack of size greater than 1. That is, you can use separate pushd
commands to push multiple directories onto the stack. Each subsequent popd
command will go to (and pop off, i.e., remove from, the stack) the most recent pushed directory not yet popped.
Here's an example:
ek@Kip:~$ cd /etc/apt/sources.list.d
ek@Kip:/etc/apt/sources.list.d$ pushd .
/etc/apt/sources.list.d /etc/apt/sources.list.d
ek@Kip:/etc/apt/sources.list.d$ pushd /home/ek
~ /etc/apt/sources.list.d /etc/apt/sources.list.d
ek@Kip:~$ cd /var/log/apt
ek@Kip:/var/log/apt$ pushd .
/var/log/apt /var/log/apt /etc/apt/sources.list.d /etc/apt/sources.list.d
ek@Kip:/var/log/apt$ cd
ek@Kip:~$ popd
/var/log/apt /etc/apt/sources.list.d /etc/apt/sources.list.d
ek@Kip:/var/log/apt$ popd
/etc/apt/sources.list.d /etc/apt/sources.list.d
ek@Kip:/etc/apt/sources.list.d$ cd /
ek@Kip:/$ pushd usr
/usr / /etc/apt/sources.list.d
ek@Kip:/usr$ cd
ek@Kip:~$ popd
/ /etc/apt/sources.list.d
ek@Kip:/$ popd
/etc/apt/sources.list.d
ek@Kip:/etc/apt/sources.list.d$ popd
bash: popd: directory stack empty
ek@Kip:/etc/apt/sources.list.d$
pushd
and popd
are used much less frequently than cd
. They also have other, even more advanced uses. Run help pushd
and help popd
for details.
Learning More and Accessing Documentation
cd
and pwd
have more advanced uses too. To learn about them, run help cd
and man pwd
.
help
is for shell builtins. man
is for standalone commands--commands that run as separate processes and exist as separate executables. The type
command reveals if a command is a shell builtin or not and, if not, where the program it runs is located. For example, you could run type help
, type type
, or type man
.