Navigating the File System

Let’s Look at OSX’s Directory Structure

First, Some Definitions

What’s a directory? →

More Definitions

What’s a folder? →

Even More Definitions

What’s a pathname? →

Some Structure

Each operating system organizes their files and directories differently. OSX’s file structure looks like this (it’s similar to other UNIX based file systems):

More About the User’s Directory

It’s worth noting the directories nested under Users. They should seem familiar to you.

And Now… To Pathnames!

What’s a Pathname Again?

A pathname is the general form of the name of a file or directory; it specifies a unique location in a file system.

Path Separator

A path separator is a character that’s used to join together each directory in a pathname that contains nested directories (for example, Desktop was located under Users and username… there was a character that separated each directory).

What character represents the path separator on OSX (we just saw this)?

Absolute vs Relative Paths

We’ll look at this a little bit more in the next class.

Some Special Paths

There are shortcuts to represent specific paths:

The following paths are relative to the directory that you’re in:

Let’s See Some Pathnames Through Finder

Now That We Know About Pathnames, We Can Navigate the File System Using the Commandline! (EXCITED YET!?)

ls

List all of the files (and directories) in the current directory. →

Think: list (we just saw this!).

$ ls
Applications	Documents   ....

ls pathname

List all of the files (and directories) in the directory specified by the pathname. →

$ ls Desktop
cuny-first.png		fractal mountains

ls -l

The -l flag gives detailed output about each file in the directory →

Think: l for long version.

$ ls -l
drwx------   3 bree  staff   102 Nov  1 15:08 Applications
drwx------+  7 bree  staff   238 Jan 28 22:46 Desktop

ls -a

The -a flag lists all files, including hidden ones →

Think: a for all.

$ ls -a
.			.config			Dropbox

ls -al

You can combine flags by placing them one after the other. This outputs a detailed list of all files. →

$ ls -al
drwxr-xr-x+ 67 bree   staff   2278 Jan 30 02:07 .
drwxr-xr-x   5 root  admin    170 Aug 13  2011 ..
-rw-r--r--@  1 bree   staff  24580 Jan 30 02:19 .DS_Store
-rw-r--r--   1 bree   staff   3459 Mar 21  2011 .RData

ls -t

The -t flag sorts by time. →

$ ls -lt
total 0
drwxr-xr-x   2 bree  staff    68 Jan 30 02:07 mtec1003
drwx------@ 19 bree  staff   646 Jan 29 21:21 Dropbox
drwxr-xr-x   9 bree  staff   306 Jan 29 19:31 projects

Combining Flags and Arguments

You can use multiple flags… and combine them with an argument as well. What do you think this does?

$ ls -alt Desktop

It lists all of the files in Desktop (if you’re in your home folder), ordered by time, including hidden files and showing extra information.

drwxr-xr-x+ 40 bree  staff    1360 Jan 29 01:12 ..
-rw-r--r--@  1 bree  staff    6148 Jan 25 01:46 .DS_Store
drwx------+  6 bree  staff     204 Jan 24 08:26 .
-rw-r--r--@  1 bree  staff  245447 Jan 23 23:15 cuny-first.png
drwxr-xr-x  16 bree  staff     544 Oct 28 18:46 fractal mountains
-rw-r--r--   1 bree  staff       0 Aug 31 22:41 .localized

pwd

Shows the directory that you’re currently in.

Think: print working directory.

$ pwd
/Users/bree

hostname

Prints out the name of your computer →

$ hostname
walsh-9

mkdir

Creates a directory with the name of the argument supplied. →

Think: make directory

One argument is required: the name of the directory to create.

$ mkdir my_animated_gifs

mkdir dir1/dir2/dir3

This attempts to create 3 directories nested within eachother. →

Forward slash (/) shows that a directory is within the directory preceding it. dir1/dir2 means dir2 in dir1.

However, nested directories don’t work as an argument for mkdir. (Unless…)

$ mkdir dir1/dir2
mkdir: dir1: No such file or directory

mkdir -p dir1/dir2

The -p flag allows you to create multiple directories nested within eachother. →

mkdir -p dir1/dir2
$ ls
dir1
$ ls dir1
dir2

cd

Changes current directory to the directory specified in the argument. →

Think: change directory

One argument is required: the name of the directory to change to.

$ cd Desktop
$ pwd
/Users/bree/Desktop

A Reminder About Special Paths

Using Special Paths

Let’s use special paths with cd →

$ pwd
/Users/bree
$ cd ../
$ pwd
/Users
$ ls
Shared	bree
$ cd ~
$ pwd
/Users/bree
$ cd /
$ pwd
/

Back to Directory

Pass - (dash) as an argument to cd to go back to the directory you just changed from. →

$ pwd
/Users/bree
$ cd /tmp
$ pwd
/tmp
$ cd -
/Users/bree

rmdir

Removes a directory with the name of the argument supplied. →

Think: remove directory

One argument is required: the name of the directory to remove.

$ mkdir foo
$ ls
foo
$ rmdir foo
$ ls

pushd/popd

Change to directory, but save current one for later. →

pushd/popd Continued

$ pwd
/Volumes
$ pushd ~/Desktop/
~/Desktop /Volumes
$ pushd /Applications/
/Applications ~/Desktop /Volumes
$ popd
~/Desktop /Volumes
$ pwd
/Users/bree/Desktop
$ popd
/Volumes

Activity: Drills!

Entering commands flash cards x 10 (use set 2)

We’ll do this together, then try downloading it yourself:

  1. Download drills.py to the home directory
  2. Type python drills.py
  3. When prompted for a number, enter 2
  4. CTRL-C quits

Lab

Navigating the File System

Activity: Drills!

Entering commands flash cards x 10 (use set 12)

We’ll do this together, then try downloading it yourself:

  1. Download drills.py to the home directory
  2. Type python drills.py
  3. When prompted for a number, enter 12
  4. CTRL-C quits