Part 1. Navigating the file system
The file system is a hierarchical arrangement of files, grouped into
directories. The hierarchy forms a
tree of the sort you
learned about in CSCI 151. Each file and directory has a name and we can refer
to any file in the file system by giving a path through the tree, starting
at the root directory and using /
to separate the directories along the
path.
Here are some example paths.
/
The root directory itself/bin/bash
The programbash
(this is the shell we’re using right now!) which lives inside the directorybin
which lives inside the root directory/usr/bin/wget
The programwget
(this program will let us download files from the internet) which lives in the directory/usr/bin
Naming files by starting at the root /
all the time is time-consuming. Each
program has a notion of its current directory. We can also name files by
giving a path from the current directory.
Here are some examples of relative paths.
paper.pdf
The filepaper.pdf
inside the current directoryMusic/swift.mp3
The fileswift.mp3
inside theMusic
directory inside the current directory
The terminal you opened launched the bash
shell and it should be currently
running. You should see a prompt in the terminal that looks like
user@mcnulty:~$
(yours will have a different username and hostname—the name of
the computer). From here on out, the prompt will be specified only as $
. You
should not type the $
character when entering input.
Bash has several commands for navigating the file system and listing the contents. The most commonly used commands are
-
cd
change current directory;$ cd /usr/bin
changes the current directory to/usr/bin
; with no arguments,cd
changes the current directory to your home directory -
ls
lists the contents of a directory;$ ls /tmp
lists the contents of the directory for temporary files,/tmp
; with no arguments,ls
lists the contents of the current directory -
pwd
prints the current directory (the name stands for “print working directory” where “working directory” just means the current directory).By convention, files and directories that start with a period are hidden from directory listings. If you want to see all files, including hidden ones, use
$ ls -a
.Every directory has two directory entries named
.
and..
. The single period,.
, is the entry for the directory itself. Two periods,..
, is the entry for the parent directory.Perhaps the most common use of
..
is to change to the parent directory using$ cd ..
. For example, after changing to a directory like$ cd foo/bar/
, I can return to thefoo
directory by running$ cd ..
.
Your task
Create a file named task1.txt
using a text editor. To do so, click the Show Applications
button in the bottom left corner of the desktop, and then open
Text Editor
.
You may also use a command-line editor like emacs or vim. Both are available on mcnulty
. To open them, you can open terminal and simply type emacs {filename}
or vim {filename}
.
Using the shell commands cd
and ls
, find two different files or
directories in each of the following directories
/
/usr
/usr/bin
/etc
Write the names of the 8 different files (and which directory they’re in) in
task1.txt
. Save this file in your home directory for now. We’re going to
move it later.
The shell expands a tilde, ~
, into the path for your home directory. Thus, you can, for example, change back to your home directory using $ cd ~
. You probably want to do so now.
You may construct paths relative to your home directory by starting the path with ~/
. For example, the shell will expand ~/foo/bar
into the path to the foo/bar
file or directory inside your home directory.
Note that the shell treats an unquoted ~
differently than a quoted tilde, '~'
or "~"
. Run the command $ echo ~ '~' "~"
to see the difference.
Use $ ls ~
to print the contents of your home directory. You should see task1.txt
in that directory listing.
Use $ cat ~/task1.txt
to print the contents of the file to the terminal.
Return to your home directory by running $ cd
(with no arguments) or
equivalently $ cd ~
.