Files, programs, and the command line

This is a primer to get you started on a few concepts:

  • What a filesystem is
  • How to run a program from the command line
  • What it means to run a program
  • How the computer knows what program to run
  • How to refer to a file from the command line

The filesystem

All the files and programs on your computer are organized into folders; all these folders are in some other folders all the way down to your hard drive, which we call the root of your filesystem. Every hard drive, USB drive, DVD, and CD-ROM has its own filesystem.

You normally look at the contents of your filesystem via the Finder (on Mac) or the Explorer (on Windows). Open a window there now.


In [34]:
display < img/mac_finder.png




In [35]:
display < img/windows_explorer.png



The Finder / Explorer window opens in some folder, which might be different depending on what computer operating system you're using. But you'll usually have a navigation bar to the left, that will let you go to different places. You see folders, also known as directories, and you might see files too.

One thing that computer OSes like to hide from you is the fact that you have a home directory, where all your personal files and folders should live. This makes it easier for multiple users to use a single computer. You can find your home directory like this:

  • On Mac, select Go -> Home in the menu.
  • On Windows, click on 'Local Drive (C:)', then click on 'Users', then click on your login name.

You'll see that your home directory has several folders in it already, that were created automatically for you when you first made a user account.

Now how can you tell where you are, with respect to the root of your drive?

  • On Mac, select View -> Show Path Bar in the menu.
  • On Windows, look

The Finder / Explorer will also show you where in your computer's filesystem you are. This is called the path - it shows you the path you have to take from the root of your filesystem to the folder you are in.


In [36]:
display < img/mac_path.png




In [37]:
display < img/windows_path.png



Now if you are on Windows, click on that bar and you'll see something surprising.


In [38]:
display < img/windows_realpath.png



This is your real path. The C:\\ is how Windows refers to the root of your filesystem. Also note that, even if your OS is not in English, the path may very well be!

Getting started with the command line

Now that you have a hint of what is going on behind the scenes on your computer, let's dive into the command line. Here is how you get there:

  • On Mac, look for a program called Terminal.app
  • On Windows, look for a program called Powershell

In [39]:
display < img/mac_terminal.png




In [40]:
display < img/windows_powershell.png



By default, these shells open in your home directory. On Windows this is easy to see, but on Mac it is less clear—that is, until you know that this ~ thing is an alias for your home directory.

Components of the command line

The command line consists of a prompt where you type your commands, the commands and arguments that you type, and the output that results from those commands.

The prompt is the thing that looks like

MacBook-Pro:~ tla$

or

PS C:\Users\Tara L Andrews>

You will never need to type the prompt. That means that, if you are noting down what we do in class for future reference, you should not copy this part!

The prompt actually gives you a little bit of information.

  • On Mac, it has the name of the computer, followed by a :, followed by the directory where you are, followed by your username, with $ at the end.
  • On Windows, it has PS for 'PowerShell', followed by the name of the drive (C for most of you), followed by a :, followed by the full path to where you are, with > at the end.

When you type a command, nothing happens until you press the Return key. Some commands have output (more text that appears after you press Return) and others don't. You cannot run another command until the prompt is given again.

NOTE: From this point on in this notebook, you will be running the commands that are run here!

Let's first make sure we are in our home directory by typing cd ~. For most of you this should change nothing, but now you know your first shell command. The 'cd' stands for 'change directory', and what follows is the directory you want to go to.


In [41]:
cd ~



Now let's have a look around. The command to show what is in any particular directory is called ls, which stands for list. Try running it.


In [42]:
ls


644_col.txt				Virtual Machines
Applications				a
CloudStation				anaconda
Desktop					b
DocLocal				beer.pl
DocVC					bifurc.png
Docear					caesar.dot
Documents				caesar.svg
Downloads				caesar.xml
Dropbox					caesar.zip
Finding your files with Python.ipynb	cloud_reinstallation.txt
Github API.ipynb			collatex
Icon?					demo
Library					glassfish3
Movies					jerus_410.txt
Music					jerus_first.txt
Navigating the command line.ipynb	legoland
NetBeansProjects			nltk_data
OxArm32					perl5
Pictures				pubs.bib
Prezi					restored.txt
Projects				scratch
Public					smbat_410.txt
V917					www

If you are on Windows, what you get will look more like this:

PS C:\Users\Tara L Andrews> ls


    Verzeichnis: C:\Users\Tara L Andrews


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----        23.02.2016     21:18            .oracle_jre_usage
d-r--        23.02.2016     20:40            Contacts
d-r--        23.02.2016     20:40            Desktop
d-r--        23.02.2016     21:11            Documents
d-r--        23.02.2016     21:16            Downloads
d----        23.02.2016     21:24            exist
d-r--        23.02.2016     20:40            Favorites
d-r--        23.02.2016     20:40            Links
d-r--        23.02.2016     20:40            Music
d-r--        23.02.2016     20:40            Pictures
d-r--        23.02.2016     20:40            Saved Games
d-r--        23.02.2016     20:40            Searches
d-r--        23.02.2016     20:40            Videos


PS C:\Users\Tara L Andrews>

Now go into your documents folder and look around.


In [43]:
cd Documents
ls


- Getting Started Tips -
1 Catalogue of Armenian works before 17c.pdf
140109_RI-application-V2.5_DV_TLA.docx
2013 HS
2014 FS
2014 HS
2015 FS
2015 HS
2016 FS
[...]
x70_ticket.pdf
yapc15
yapc_09_slides.key
yapc_slides.key
yapctalk.txt
yuma_directions.pdf
zh_ausweis_tla.pdf

How does this compare to what you see in the Finder / Explorer window, if you click on the Documents folder?

Another important command, which tells you where you are at any given time, is pwd. This means print working directory. Try it now and see what you get.


In [44]:
pwd


/Users/tla/Documents

If ever you get lost on the command line, pwd will always help you find your way.

File paths and path notations

By now you will have noticed that I've mentioned the path a few times, and that it seems to have something to do with this thing that pwd prints out. (And, most annoyingly, that it looks different on Mac and Windows.) The bit of text that you get from pwd is what is called path notation, and it is very important that you learn it if you want to do anything with your own digital data. Here are some rules:

  • The / (or \\ on Windows) separates folder names. So Desktop/Video means "the thing called Video inside the Desktop folder".
  • The / all by itself refers to the base of your hard drive (usually "Macintosh HD" or "C:\".)
  • The ~ refers to your home folder.
  • These things can be combined; ~/Documents means "the Documents folder in my home folder."
  • The . means "the current working directory", i.e. what you would get if you ran the command pwd.
  • The .. means "one directory back" - if pwd gives you /Users/tla, then .. means /Users.
  • If the path does not start with a . or a / or a ~, then it will be assumed to start with a ./, that is, "start from the current working directory."

Let's wander around a bit.


In [45]:
cd ~
pwd


/Users/tla

In [46]:
cd ..
pwd


/Users

In [47]:
ls


Guest	Shared	tla

In [48]:
cd Desktop
pwd


bash: cd: Desktop: No such file or directory
/Users

In [49]:
cd tla/Desktop




In [50]:
pwd


/Users/tla/Desktop

In [51]:
cd Documents


bash: cd: Documents: No such file or directory

In [52]:
pwd


/Users/tla/Desktop

In [53]:
cd ../Documents
pwd


/Users/tla/Documents

In [54]:
cd DH2015
pwd


/Users/tla/Documents/DH2015

In [55]:
cd ../../Desktop
pwd


/Users/tla/Desktop

In [56]:
ls testpage.pdf


testpage.pdf

In [57]:
ls ESTS


11-kapali-shastri.jpg	dbschema.png		person_annotation.png
ChronicleME.png		droll5.jpg		person_display.png
Hermeneutics-Study.jpg	florilegium_page.png	place_annotation.png
annotation_def.png	models_annotation.key	place_annotation1.png
annotatorjs.png		neo4j_misident.png	place_display.png
baldwin_ms.jpg		neo4j_person.png	social-graph.png
bzan00449_030r_029v.jpg	neo4j_shot.png		transmission.png
cidoc_crm_vangogh.jpg	people_annotation.png	vangogh_edition.png

In [58]:
cd /Users
pwd


/Users

In [59]:
cd /Users/tla
pwd


/Users/tla

Command line arguments

So far we have learned three commands: cd, ls, and pwd. These are useful for navigation, but we can run a lot more commands once we learn them, and have a need for them!

What are we doing, exactly?

  • First word is the command
  • All other words are the arguments
  • Words are separated by spaces

cd is a command that expects an argument: the name of the directory you want to go to. But what if the name has a space in it?


In [60]:
cd ~/Documents/2016 FS


bash: cd: /Users/tla/Documents/2016: No such file or directory

What happened there??

Well, I have a folder called 2015 HS in my Documents folder, and I tried to go there. But since the command line works with arguments, and since arguments are separated by space, it thought I was saying "Change to the Documents/2015 folder, and then HS, whatever that means." And it gave me an error, because I don't have a folder called 2015 in my Documents folder.

You can get around this. How you get around it depends on whether you're on Windows or not. One way to get around it that should work both places is like this:


In [61]:
cd ~/Documents/"2016 FS"
ls


01 Using the command line.ipynb		absenzenerfassung2016_v1_01_xl.xls
DD_session1.txt				activities.docx
DD_software				activities.md
DD_software.zip				dhbenelux_tlaedit.docx
DD_syllabus.docx			idp_support.docx
DHd2016_reg.pdf				idp_support.pdf
ISR_intro.docx				img
P-2_finance_Unterprojekt-2015-UniBE.pdf	paris_talk
TT_homework1.docx			paris_talk.key
TT_syllabus.docx			thomas_recletter.pdf
WP07-SCALEUP-Jan16.ppt

In [63]:
ls DD_software img paris_talk.key thisdoesnotexist


ls: thisdoesnotexist: No such file or directory
paris_talk.key

DD_software:
Mac OS X
Windows 32bit
Windows 64bit
mac_install_Taras-MacBook.local_Feb-24-100531-2016_Conflict.zip
oxygen_student_license.txt
windows_install_Taras-MacBook.local_Feb-24-100531-2016_Conflict.zip

img:
mac_finder.png		windows_explorer.png	windows_realpath.png
mac_path.png		windows_path.png
mac_terminal.png	windows_powershell.png

Paths and program execution

As well as a shell command like what we have been using, you can also give the name of a program as the first word on a command line. Now that you have Python installed we can try that.


In [64]:
python -V


Python 3.4.4

Here we commanded the program python to run, and gave it a single argument, which happens to mean "Print out your version number and then quit". Which is exactly what happened.

So can you run any program, just like that? Not quite. Let's try to run one of our files.


In [65]:
idp_support.pdf


bash: idp_support.pdf: command not found

If you're on Windows, the error will look something like this:

exist : Die Benennung "exist" wurde nicht als Name eines Cmdlet, einer Funktion,
ausführbaren Programms erkannt. Überprüfen Sie die Schreibweise des Namens, oder
enthalten), und wiederholen Sie den Vorgang.
In Zeile:1 Zeichen:1
+ exist
+ ~~~~~
    + CategoryInfo          : ObjectNotFound: (exist:String) [], CommandNotFound
    + FullyQualifiedErrorId : CommandNotFoundException

In both cases, the key here is where it says the file was not found. So how does the computer know where to look? The answer lies in something, confusingly enough, also called the PATH.

You can see your PATH as follows:

  • On Mac, type echo $PATH
  • On Windows, type echo $Env:Path

In [66]:
echo $PATH


/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/texbin

What you see is a list of directory paths. When you type a command, the computer looks in each of these directories to see if it can find a program with the name you have given as the first word of that command. This means that sometimes, when you install software, you have to change the PATH. We'll talk about how to do that, when necessary, as term goes on.


In [ ]: