We’ve learned how to use pwd
to find our current
location within our file system. We’ve also learned how to use
cd
to change locations and ls
to list the
contents of a directory. Now we’re going to learn some additional
commands for moving around within our file system.
Use the commands we’ve learned so far to navigate to the
CF_Shell/untrimmed_fastq
directory, if you’re not already
there.
$ cd
$ cd CF_Shell
$ cd untrimmed_fastq
What if we want to move back up and out of this directory and to our
top level directory? Can we type cd CF_Shell
? Try it and
see what happens.
$ cd CF_Shell
-bash: cd: CF_Shell: No such file or directory
Your computer looked for a directory or file called
CF_Shell
within the directory you were already in. It
didn’t know you wanted to look at a directory level above the one you
were located in.
We have a special command to tell the computer to move us back or up one directory level.
$ cd ..
Now we can use pwd
to make sure that we are in the
directory we intended to navigate to, and ls
to check that
the contents of the directory are correct.
$ pwd
/home/workshop/<username>/CF_Shell
$ ls
sra_metadata untrimmed_fastq
From this output, we can see that ..
did indeed take us
back one level in our file system.
You can chain these together as well. Let’s demonstrate this idea.
First we’ll return to the untrimmed_fastq
directory, and
then we’ll chain two ..
’s together, to bring us back to our
home directory:
$ cd untrimmed_fastq/
$ cd ../../
We’ve moved up two directories from untrimmed_fastq
.
Where are we? What are the expected contents of this location?
By default, the ls
commands lists the contents of the
working directory (i.e. the directory you are in). You can always find
the directory you are in using the pwd
command. However,
you can also give ls
the names of other directories to
view. Navigate to your home directory if you are not already there.
$ cd
Then enter the command:
$ ls CF_Shell
sra_metadata untrimmed_fastq
This will list the contents of the CF_Shell
directory
without you needing to navigate there.
The cd
command works in a similar way.
Try entering:
$ cd
$ cd CF_Shell/untrimmed_fastq
This will take you to the untrimmed_fastq
directory
without having to go through the intermediate directory.
The cd
command takes an argument which is a directory
name. Directories can be specified using either a relative path
or a full absolute path. The directories on the computer are
arranged into a hierarchy. The full path tells you where a directory is
in that hierarchy. Navigate to the home directory, then enter the
pwd
command.
$ cd
$ pwd
You will see:
/home/workshop/<username>
This is the full name of your home directory. This tells you that you
are in a directory named with your username. This is inside a directory
workshop
, which sits inside a directory called
home
which sits inside the very top directory in the
hierarchy. The very top of the hierarchy is a directory called
/
which is usually referred to as the root
directory. There will be more on root
and
home
in the next section.
Now enter the following command:
$ cd /home/workshop/<username>/CF_Shell/.hidden
This jumps forward multiple levels to the .hidden
directory. Now go back to the home directory.
$ cd
You can also navigate to the .hidden
directory
using:
$ cd CF_Shell/.hidden
These two commands have the same effect, they both take us to the
.hidden
directory. The first uses the absolute path, giving
the full address from the root directory. The second uses a relative
path, giving only the address from the working directory, currently
‘home’. A full path always starts with a /
. A relative path
does not.
A relative path is like getting directions from someone on the street. They tell you to “go right at the stop sign, and then turn left on Main Street”. That works great if you’re standing there together, but not so well if you’re trying to tell someone how to get there from another country. A full path is like GPS coordinates. It tells you exactly where something is no matter where you are right now.
You can usually use either a full path or a relative path depending on what is most convenient. If we are in the far inside an unknown set of nested directories, it is often more convenient to enter the full path to a new location. If our current working directory is relatively close to our intended location, it is more convenient to enter the relative path, since it involves less typing.
Over time, it will become easier for you to keep a mental note of the structure of the directories that you are using and how to quickly navigate amongst them.
Using the filesystem diagram below, if pwd
displays
/Users/thing
, what will ls ../backup
display?
../backup: No such file or directory
2012-12-01 2013-01-08 2013-01-27
2012-12-01/ 2013-01-08/ 2013-01-27/
original pnas_final pnas_sub
backup
in
/Users
.Users/thing/backup
, but with
..
we asked for one level further up.-F
to display /
at the end of the directory
names.../backup
refers to
/Users/backup
.