Sunteți pe pagina 1din 11

PRACTICAL LINUX FOR CS 10 AND BEYOND

SECTION 1 – THE FILE SYSTEM

VOCAB
 Directory  Program
 Parent Directory  Operating System
 Child Directory  File Path
 Top-Level/Root Directory  Working/Current Directory
 File  Home Directory
 Drive  File Extension
 File System

CURRENT DIRECTORY
The  current  directory  is  the  directory  you’re  currently  working  in.  This  has  a  very  special  meaning within a
terminal, which will be shown later.

In the picture above, the current directory is C:\Music\Music (this is a path, which is explained in the next
section).

FILE SYSTEM AND PATHS


A file path specifies a location (typically a file or directory) on a file system. Different operating systems have
different ways of specifying file paths, but all file paths can fall under two categories: absolute paths and relative
paths.

ABSOLUTE PATHS

1
“A full path or absolute path is a path that points to the same location on one file system regardless of the working
directory  or  combined  paths”  (Wikipedia). This is probably the only type of file path that the average computer
user would encounter in their day-to-day operations. Below are a few examples of absolute file paths on different
operating systems.

C:\Users\John\Documents\Hello.txt is a file path on Microsoft Windows. C: says that the location in


question is on the C drive (the C drive is the top-level directory in this case). Users, John, and Documents are all
directories that you must go through to get to the final location (note that Documents is a child directory of John,
which is a child directory of Users, which is a child of the top-level directory). Hello.txt is the file that the file
path is referring to (which is a child file of Documents). The back slashes (\) simply separate each step in the
path.

/Users/John/Documents/Hello.txt is a file path on Mac OS X. Note that no drive letter is denoted. On Macs,
the root directory is actually just denoted as / (which is a legal file path on its own). In this path, Users, John, and
Documents are all directories and Hello.txt is the file in question (again, note that Documents is a child of
John, which is a child of Users, which is a child of the root directory). Notice that rather than back slashes (\),
forward slashes (/) are used. If you tried to enter in a path on a Mac with back slashes,  it  most  likely  wouldn’t  work  
(though it depends on the context).

/home/John/Documents/Hello.txt is a file path on Linux (though it would also be valid on Mac OS X, and
the Mac OS X sample path would be valid on Linux). It is in exactly the same format as the Mac OS X path.

~/Documents/Hello.txt is a valid file path on either Mac OS X or on Linux. The tilde (~) is a special shortcut
one can use to denote your home directory. Your home directory is typically where all of your personal files are
stored (your documents, images, desktop files, etc.) on the file system. The location of your home directory
depends on what operating system your using (/Users/CurrentUser on Mac OS X or /home/CurrentUser
on Linux) and changes depending on the current user.

RELATIVE PATHS
“A relative path is a path relative to the working directory of the user or application, so the full absolute path will not
have  to  be  given”  (Wikipedia).  Relative paths are much the same on most operating systems, but note that on
Windows you would have to make sure to use back slashes rather than forwards slashes. Below are three common
relative paths.

./Hello.txt is a file path referring to a file Hello.txt located in the current directory. The . at the start of the
path is a shortcut referring to the current directory. On many Linux configurations, this path is equivalent to
Hello.txt (on the lab computers, ./Hello.txt is equivalent to Hello.txt).

./A/B/Hello.txt is a file path referring to a file Hello.txt located in the directory B, which is in the
directory A, which is in the current directory.

../Hello.txt is a file path referring to a file Hello.txt located in the parent directory of the current
directory. The .. at the start of the path is a shortcut referring to the parent directory of the current directory.

../../Hello.txt is a file path referring to a file hello.txt located in the parent directory of the parent directory
of the current directory.

If my current directory is ~/Documents/NotCats/ and I want to refer to


~/Documents/DefinitelyCats/Cat.png I could use the path ../DefinitelyCats/Cat.png.
2
FILE EXTENSIONS
Most files end with a file extension (such as .txt, .cpp, .doc, .docx, etc.), and people speak of them casually,
but many do not know what file extensions actually are.

File extensions are simply a way to note what kind of data a file contains. A .txt file contains plain text, and thus
should be opened with a text editor that supports plain text (gedit, geany, notepad, etc.). A .bmp file contains a
bitmap, and thus should be opened with a program that can display or edit a text file (paint, firefox, etc.). If
you change the extension of a file, you do not actually modify its contents; you just change how those contents are
treated.

Note that file extensions may be ignored. For example, nothing  says  that  you  can’t  open  a  .bmp file with a text
editor.

EXERCISES
1. What is ~ a shortcut for?

2. My current directory is ~/Documents/Programming, what is the parent directory of my current


directory?

3. My current directory is ~/Documents and I want to refer to


~/Documents/Programming/cpp/Awesome/Awesome.cpp, what is the shortest file path I could use
to do this?

4. I want to refer to the file Hello.txt which is on my Desktop on my Windows computer. On Windows,
files on the Desktop are stored in the Desktop folder in your home directory (C:\Users\John in this
case). How can I refer to it with an absolute path?

5. My current directory is ~/Documents/Programming/cpp/Awesome and I want to refer to


~/Documents/Programming/Java/Hello World/Hello World.txt, what is the shortest file
path I could use to do this?

6. My current directory is ~/Documents/Programming, and I want to refer to /home/Bill/Documents,


what is the shortest file path I could use to do this?

7. My current directory is /home/John/Documents/Programming/web and I want to refer to the root


directory, what is the shortest file path I could use to do this?

3
SECTION 2 – TERMINAL BASICS

VOCAB
 Terminal
 Program Output
 Error
 Syntax
 Delimiter

WHAT IS THE TERMINAL?


The terminal is the home base of most programmers. From the terminal, a programmer can accomplish all of his or
her tasks with relative ease, and impress anyone who is looking over their shoulder.

Many of you have likely seen a Windows Command Prompt at some time in your life, which is what can be referred
to as the windows terminal (picture below).

You will be using the Linux Terminal in CS 10 and beyond, and this part will attempt to teach you how to use it.

HOW TO GET ONE OPEN


The simplest way to get a terminal open that is guaranteed to work on all of the school computers, no matter their
configuration, is to simply right click on the desktop and select Open Terminal (make sure not to click on any
icons, just click on a blank area on the desktop).

4
PROMPT
In the picture above, only the prompt is displayed. The prompt is very useful, it displays your current directory
along with some other useful information  which  you  don’t  need  to  worry  about  for  now. The prompt will appear
every time you enter a command (commands are covered below).

The current directory is displayed on a line of its own in your prompt (in the picture above the current directory is
~). Your username is displayed in the second line before the @ symbol (in the picture above, the username is
dlang004). The hostname of the computer you are currently on is displayed after the @ symbol (in the picture
above  the  current  computer’s  hostname  is  well).  The  $ symbol  doesn’t  mean  anything  significant  and  will  never  
change.

INTRODUCTION TO COMMANDS
To do anything of interest within the terminal you must enter commands. These commands are actually programs
on your computer (this is a half-truth  but  it’s  true  enough  for  now).

Examples speak a thousand words so take a look at the commands ls and cd.

ls is a command which lists all of the files and directories in your current directory. So if my current directory was
my home folder and I typed ls, I would be shown all of the files and directories in my home folder (but not the
child directories and files of those directories). Note: Hit enter to submit a command.

5
Note in the picture above, some items have a / after them, others have a * after them. If an item has a / after it then
that item is a directory (and conversely, if an item does not have a / after it then that item is a file). If an item has a
* after it then it is executable meaning you can try to run it as a program.

cd is a program that allows you to change your current directory. The syntax is cd [Directory to Move
To]. So for example, to change your current directory to ~/Programming/cpp you could write cd
~/Programming/cpp, or if your current directory is ~/Programming already you could type cd cpp.

Typically you would use cd and ls repeatedly to reach your destination, look at the screenshot below for an
example of this.

6
If you have tried navigating around your file system with cd and ls already you most likely noticed that moving
into directories with spaces in their name is difficult. For example, if you tried to move into a directory named CS
10 by typing in cd CS 10, an error message CS: no such file or directory would have likely appeared
(unless of course you had a CS directory in the same directory).

Whenever  you  give  a  command  a  file  path,  you  must  “escape”  any  spaces  in  the  path.  You can do this a couple
different ways. One is to use back slashes: just add a back slash before any space, for example
cd Assignment\ 8. The other method is to wrap the path in quotes: for example, cd  “Assignment  8”.
Which  method  you  use  is  completely  up  to  you  and  you  don’t  need  to  stick  to  one. It’s  even  possible  to  mix  them  in  
a single path (try cd  ~/”Programming  Folder”/CS\ 10/”Assignment  5  – Remake”)

Try typing the first part of a directory name and then hitting tab. Experiment with this, it is a
powerful feature of the Linux Terminal called tab completion. To not take advantage of it is
very foolish.

EXERCISES
1. Find the SI Test directory on your computer (if you downloaded it per the instructions it should be in
~/Desktop) and write down all of the files in the directory labeled secret (you have to look for it!).

HOW TO MANIPULATE

7
Now that you can move around our computer you need to be able to actually do something with the files you
encounter. The most basic three things you can do with your files and directories are edit/create them, delete
them, and organize them. The five commands you will use to do each of these things are geany, rm, mkdir,
rmdir, and mv.

geany can be used to edit and create any text based file (so code files, text files, etc.). The syntax for geany is
geany [File to Open] (if  you  specify  a  file  that  doesn’t  exist,  geany will create it for you). So for example, if
you wanted to open the file Hello.txt in your current directory you could enter geany Hello.txt. Once you
type that in, the geany editor  will  appear  with  that  file  opened  and  you  can  do  whatever  you’d  like  to  the  file  
through  it.  Once  you’re  done,  save  the  file  and  close  the  editor  (until  you  close  the  editor  you  won’t  be  able  to  do  
anything in your terminal). Note that geany is also useful for simply viewing the contents of a file. Nothing says
you must edit the file.

If you ever want to delete a file that you’ve  created,  you  can  use rm. The syntax is rm [File to Delete]. For
example, to delete the file Hello.txt in your current directory, type rm Hello.txt. When prompted, type in y
(yes) or n (no) to confirm deletion,  experiment  with  files  you’ve  created  with  geany.

If you want to delete a directory, you can use rmdir. The syntax is rmdir [Directory to Delete]. What you
must know is that rmdir only works on empty directories (directories without any sub files or directories), so you
will have to make sure to delete the contents of the directory first.

If you want to create a directory, you can use mkdir. The syntax is mkdir [Name of Directory].

If you want to move or rename a file or directory, you can use mv. The syntax is different than the other commands
you’ve  learned  in  that  it  accepts  two  command line arguments. The syntax is as follows: mv [Name of
Directory or File to Rename] [New Name of Directory or File]. For example, if you wanted to
rename the file foo bar.txt (note the space) to bar.txt, you could simply write mv foo\ bar.txt
bar.txt (assuming the file is in your current directory). Now you can see why escaping spaces is so important, if
you didn’t  escape  the  space  in  foo bar.txt you could end up getting some very strange results, or an error
would occur. If you wanted to move the file bar.txt (not rename it) into the directory Bar you could simply
write mv bar.txt Bar.

EXERCISES
2. Create a text file named Hello World.txt on your desktop and write Hello World in the file.
3. Create a directory named Hello World Project on your desktop and move the text file you created
into that directory.
4. Delete the directory (and the file).

8
SECTION 3 – FLAGS AND MAN PAGES

WHAT IS A FLAG?
A flag, also known as a switch or an option, is a special signal to a program that you want it to do something in
particular. For example, you may have used the g++ compiler already by typing g++ main.cpp which compiles
the code in main.cpp and places the compiled executable in a file called a.out. Now what if you wanted the
compiler to place the compiled executable in a file called main rather than a.out? You could use a flag to specify
this by typing g++ -o main main.cpp. In this command, -o is the flag and after the flag is the name of the
output file.

SO WHAT FLAGS CAN YOU USE AND HOW DO YOU USE THEM?
Now that is a tricky question to answer. There is no real standard, so every command accepts different flags, and
oftentimes they are in different formats. Fortunately, every command in Linux has its functionality documented in
files called man pages (short for manual pages). These man pages list everything from what the command does and
the flags it accepts to examples of its use and the names of the programmers who created it.

Below is the man page for rm. To display this page on your terminal, simply type in man rm (man is the command
that opens up the man page for a given command, try typing man man).

9
To navigate through the man page, you can use your down and up arrows or the page up and page down keys. To
exit the man page, hit q.

In the above man page the flags are listed under OPTIONS (remember option and flag are synonymous). You will
notice that none  of  these  flags  require  additional  data,  which  means  you’d  just  write  the  flags  and not any
additional information like you would with g++’s –o.

Also notice how two different versions of the some of the flags exist (like -f and --force). Oftentimes you will be
able to choose between a long form (preceded by two dashes) and a short form (preceded by one dash like in the
g++ example). They have exactly the same functionality, so it is up to your preference as to which one to use.

Using man pages can be a difficult and painful process because many of the pages are poorly formatted, but it gets
easier over time, thus try to complete the following exercises to get some practice.

EXERCISES
1. Briefly describe the functionality of the following commands: cat, tree, and clear. Hint: what
information do man pages contain?

10
2. Create a directory and create 10 files in it, then create a directory inside that directory and create another
10 files in it. Note that there is a way to do this in only a few commands. I do not want you to create each
file one by one (hint: try the command touch a b c). Call me over to show me you did it.
3. Now delete the directory you just created and its contents with a single command. You must figure out how
to do this with a single command, do not delete the files one by one. Note that what you are trying to do is
delete the directory recursively, which means you need to find a way to delete a directory in such a way
that all of it's child directories and files will be deleted first.
4. Figure out how to shred a file, then create a file and shred it.

11

S-ar putea să vă placă și