One thing computers do is store a lot of files. For example, my laptop has about 1.1 million files on it, from programmes to run the operating system, to important photos like this
Arwen with beer
We need some way of organsising these files. This is done using directories. And unless you want to get technical, directories are the same as folders.
Essentially, a directory is a virtual container for files. So for
ST2304 I have a directory to keep all of the files in. It has a file
called LearningObjectives.docx
, and a lot other
directories, e.g. Exams
and
Week01 Introduction
. These contain more files,
e.g. Week01 Introduction
contains files like
Lecture1.pdf
.
Because directories can contain directories, we can have a whole
structure directories, which looks like a tree. So for this course I
used Dropbox, and have a Teaching
directory (alongside
other directories for different projects. One is called
Dog Faces
). In the Teaching
directory I have
directories for the different courses, including one called
ST2304
. In there I have the directories for each week’s
material.
Structuring directories like this makes it easier to find files, and also easier to write code that will do something with all of the files in a directory. All computers use directories, even if they are largely hidden from you.
Although there are differences between different operating systems in the details, the basic way directories work is the same. Their structure is like a tree: each directory can have several subdirectories (and each subdirectories is only in one parent directory).
If we want to find a file on a computer, we need to know what
directory it is in. We write this by writing the directories as a
filepath. For example, the file path for this course on my
laptop is /work/bohara/Dropbox/Teaching/ST2304/
. At the
base of the tree are directories like bin/
and
dev/
, which hold files that the operating system needs. It
also has a directory called work/
, which has one
subdirectory: bohara/
.
The /work/bohara/
directory is my “home” when I log onto
my laptop. Everything under this in the tree is specific to me: if I
added an account for Zaphod Beeblebrox, then he would have a
/home/zaphodb/
folder, and could put his files in there. We
can follow the same logic further: Dropbox/Teaching/ST2304/
is the rest of the filepath, and gets us to the directory for this
course. This file is in the Week 2 sub-directory, so this file is
(before I move it to the web!):
/work/bohara/Dropbox/Teaching/ST2304/Week02 One Parameter/Directories.html
Note that we use forward slashes between the directory names. Other operating systems might be different.
So, if I want to (say) copy this file to the directory for next week, I could fire up the command line and do this:
copy /work/bohara/Dropbox/Teaching/ST2304/Week02/Directories.html /work/bohara/Dropbox/Teaching/ST2304/Week03/Directories.html
(modern operating systems have a GUI (“Graphical User Interface”) which hides this from you: you click on a file, use Ctrl-C to copy, navigate to the new folder and use Ctrl-V to paste)
But that is horrible to write. So there are a couple of shortcuts:
One is the use of the home directory. If the computer thinks I am in
my home directory (/work/bohara/
), then if I save a file
from somewhere else, it will save it in my home directory. Some
programmes don’t do this, for example my web browser will, by default,
save a file in /work/bohara/Downloads
. So if I want to copy
a file into my home directory, I could do this:
copy /work/bohara/Dropbox/Teaching/ST2304_2020/Week02/Directories.html Directories.html
The other shortcut is the use of relative paths. If I am in
my home directory, I can use directories relative to that. So, for
example, if I am in my home directory, I can refer to the Dropbox
sub-directory as /work/bohara/Dropbox/
, or more simply as
Dropbox/
. The advantage of this is that if I cange to
another directory that has Dropbox installed, I can still use the same
relative path.
Relative paths can be used like this to got down the directory tree,
but what about up? To do this you can use ../
. So, if I
wanted to go from my home directory to say, Zaphod Beeblebrox’s, I could
use ../zaphodb/
. The ..
takes me up one level,
and then zaphodb/
is his home directory.
(note: this is being written by someone who hasn’t used Windows seriously for over a decade)
Windows uses the basic system described above, but it tries to hide the details. But there are a couple of notable differences:
\
, between directory names.
So on Windows
Dropbox/Teaching/ST2304_2020/Week02/Directories.html
is
Dropbox\\Teaching\\ST2304_2020\\Week02\\Directories.html
Dropbox\\Teaching
is
the same as Dropbox\\TEACHING
C:
. When you add other drives, e.g. an external drive, or a
USB stick, or a network drive, it gets a new letter. For example, NTNU
uses M:
as your home drive on the NTNU system.This means that your file names will look different, e.g. my home
directory would be C:\\Users\\bobhara
.
Linux is based on Unix, so it uses the same file system layout, which
is what I described above. One helpful trick is that you can specify
your home directory with ~
. So if you are in another
directory, you can get to a file with
~/Dropbox/Teaching/ST2304_2020/Week02/Directories.html
Macs use a Unix operating system, so they basically act like Linux (only without the penguins).
If you want to find the full filepath for a particular file on a Mac you should locate the file in Finder:
Finder
Then right-click the file and select ‘Get info’/ ‘Vis info’. In the
example below, you can see that my file is saved at
~/Users/emilygs/Dropbox/ST2304_2020/Week02 One Parameter/Directories.Rmd
.
Result of Get Info
When you are using R, you will be able to access the directory structure in the same way. Above I have been discussing home directories, and R has the same thing, but it’s called a working directory, i.e. the directory you are working in. You can find out what it is with getwd():
getwd()
## [1] "/Users/robertoh/git_repos/ST2304/Week02 One Parameter"
You can see that this is not my home directory. This is because I ran
this code from a different directory. If I want to change my working
directory, I can use setwd()
:
setwd("~/Dropbox/Teaching/ST2304/Week03 Confidence Intervals/")
Which is the directory for the material for week 3. So once I start working on that material, I can use that as my working directory.
I can also do things with the files, like see them, copy them etc.
?files
lists the functions to do that. You may not need
these during the course, but for if you do…
dir()
file.copy("Arwen.JPG", "ABird.JPG") # copy a file
file.remove("ABird.JPG") # remove a file
# Look in Week 3's directory
dir("../../ST2304/Week03 Confidence Intervals/")
file.exists("Directories.html") # Does this file exist?
file.exists("https://www.math.ntnu.no/emner/ST2304/2020v/Week01/test_data.csv") # Yes, web addresses also work!
ST2304Exercise4GroupGenius.docx
is a better name than
Exercise4.docx
. And NEVER use Exercise4FINAL.docx, because
you know what will happen. Although The Fates do have a sense of humour,
it is not always very sophisticated.