Data in Trees

Structuring and Navigating Files

Terminology

File

A named container that stores data – such as text, images, or programs – on a computer disk. The file name usually ends in an extension (.txt, .png, .pdf) that indicates the format.

Example

127.txt
Golden Klaxons
3000?

Directory (or folder)

A named container that stores a set of files or other folders.

How should we organize our cards into directories?

Boardwork.

How can we specify the location of a file/directory in the file system?

Path

A description of a file or directory’s location in the file system, written as a sequence of folder names that leads to it, separated by /.

Absolute path: Starts from the root of the file system, indicated by /. Example: /Documents/Schoolwork/HW1.pdf

How can we specify the location of a file/directory in the file system?

Path

A description of a file or directory’s location in the file system, written as a sequence of folder names that leads to it, separated by /.

Absolute path: Starts from the root of the file system, indicated by /. Example: /Documents/Schoolwork/HW1.pdf

Relative path: Starts from the current directory and shows how to reach a target. .. means “go up one level”. Example if you’re in Documents, Schoolwork/HW1.pdf

I’m getting tired of always typing the trunk of the tree…

Home directory

A special designated directory in a file system that serves as a starting point for commonly-used paths.

Written as ~, a shorthand for its absolute path.

Demo: Stat DataHub

The Command Line (aka Terminal)

Getting around a file system

We will need to . . .

  1. Know where we are
  2. Know what’s around us
  3. Change where we are
  4. See what’s in a file

. . . all without pointing and clicking.

UNIX1 Commands

pwd

Returns your present working directory.

ls

Lists the contents of your present working directory.

cd

Changes your directory to the path provided (after a space). Examples:

  • cd Homework: (relative) only takes you into the Homework directory if you’re in Documents.
  • cd /Document/Homework: (absolute) takes you into the Homework directory regardless of your pwd.

cat

Prints to the screen the contents of the file at the path provided. Example: cat 127.txt.

UNIX Commands

  1. Know where we are: pwd
  2. Know what’s around us: ls
  3. Change where we are: cd
  4. See what’s in a file cat

There are many more, like . . .

  • Make a new directory: mkdir
  • Copy a file: cp
  • Remove a file: rm

Practice: Stat DataHub

Practice on a sample of our index cards on a cloud-hosted machine that has a directory containing sample of our index card files.

Login Here:

https://stat.datahub.berkeley.edu/hub/user-redirect/git-pull?repo=https%3A%2F%2Fgithub.com%2Fberkeley-stat133%2Fdata-in-trees&urlpath=tree%2Fdata-in-trees%2F&branch=main

Practice

10:00

Using only the command line (not the file browser).

  1. What are the names of the files stored in ~/data-in-trees/red/stat/101/1/?
  2. What is the absolute path of the file named 014.txt?
  3. What is the relative path of the file named 014.txt from ~/data-in-trees/red/non-stat?
  4. What is the relative path of the directory at ~/data-in-trees/green/stat/ from ~/data-in-trees/red/non-stat?
  5. What is the song of the summer in ~/data-in-trees/red/non-stat/101/1/003.txt?

Solutions

Solution 1

What are the names of the files stored in ~/data-in-trees/red/stat/101/1/?1

One way is to move into the directory, then list its contents:

cd ~/data-in-trees/red/stat/101/1/
ls
012.txt  014.txt

Or list the contents without moving, by giving ls the path directly:

ls ~/data-in-trees/red/stat/101/1/
012.txt  014.txt

Solution 2

What is the absolute path of the file named 014.txt?

An absolute path starts from the root /. Searching the tree, 014.txt lives inside red/stat/101/1/, so relative to home it is:

~/data-in-trees/red/stat/101/1/014.txt

Remember ~ is shorthand for home’s absolute path. On DataHub home is /home/jovyan, so the fully-written absolute path is:

/home/jovyan/data-in-trees/red/stat/101/1/014.txt

You can confirm the home path by running pwd after cd ~.

Solution 3

What is the relative path of the file named 014.txt from ~/data-in-trees/red/non-stat?

From red/non-stat, step up one level (..) to red, then descend into stat/101/1/:

../stat/101/1/014.txt

Check it from that starting directory by printing the file at the relative path:

cat ../stat/101/1/014.txt
lebi tirar mas fotos
5200

Solution 4

What is the relative path of the directory at ~/data-in-trees/green/stat/ from ~/data-in-trees/red/non-stat?

From red/non-stat, step up two levels: .. reaches red, and ../.. reaches data-in-trees. Then descend into green/stat:

../../green/stat

The two colors (red, green) sit at the same level, so any path between them must first climb up to their shared parent, data-in-trees.

Check it by walking the path yourself. First confirm where you are:

pwd
/home/jovyan/data-in-trees/red/non-stat

Then follow the relative path and confirm you arrived:

cd ../../green/stat
pwd
/home/jovyan/data-in-trees/green/stat

Solution 5

What is the song of the summer in ~/data-in-trees/red/non-stat/101/1/003.txt?

Print the file’s contents with cat:

cat ~/data-in-trees/red/non-stat/101/1/003.txt

which shows:

lebi tirar mas fotos
5200

So the song is lebi tirar mas fotos (the second line, 5200, is the accompanying count).

Alternatively, cd into the file’s directory first, then cat just the bare file name:

cd ~/data-in-trees/red/non-stat/101/1/
cat 003.txt
lebi tirar mas fotos
5200

For Next Time

  1. Fill out the Stat 133 Welcome Survey
  2. Work on the first questions from PS 1