Using the Command Line to manipulate the filesystem.
- View files within a directory.
- Create a directory (folder) in which to store a project.
- Create subdirectories to organize a project.
- Create files to contain project information.
- Edit and delete files.
- Navigate within a project.
- Get help from user manuals.
You're probably used to creating files and folders using your operating system's "explorer" program. The common explorer programs found on each OS are:
- Mac: Finder
- Windows: Explorer
- Ubuntu: Nautilus
On unix-like systems (sometimes refered to as *nix or POSIX), all your files are stored in your home folder. On Windows, your files are usually stored in "My Documents".
You might see ~
or $HOME
used to refer to your home folder on unix-like
systems.
Open your file explorer application. By default, new windows open in your home directory. Note the files you see there.
Now, open your terminal emulator. By default, new sessions start in your home
directory as well. How can we tell? Let's inspect the files in our current
directory with the ls
command (mnemonic: "list"). You should see the same
files and directories in your explorer and your terminal.
Directories (and subdirectories) are usually represented as "folders" in your explorer application. They're just convenient ways of grouping files together. Paths are string (textual) representations of your current place in the file hiearchy (or "tree").
Examine this path: /Users/berzelba
. The leading slash (/
) is the root of the
file system. The next part Users
indicates that there is a directory, or
folder, with the name "Users" inside the root of the file system. Within the
"Users" directory is another directory named berzelba
. The string
/Users/berzelba
is an absolute path to the home directory for the current user
on my machine.
An absolute path shows the unique location of the files and directories within the system's file system. No other files can have this path.
Note: It sometimes helps to think of the path as the actual full name of the file. This is especially true when we are moving or renaming files from the command line.
Absolute paths:
- Always start with a leading slash, '/'
- Are relative to the root directory of the file system.
The root directory the top level directory in the tree structure that makes up your project or file system. When we talk about absolute paths, the paths are relative to the topmost path of the file system.
In your terminal sessions, figure out what directory you are in by using the
pwd
command (mnemonic: "print working directory"). What is the topmost
directory in this path? What is the root of the file system?
Open up the Finder and go to the root directory of your filesystem. There are
several ways to accomplish this, but I prefer the ⌘ + ⇧ + g
shortcut in
Finder. Examine the contents of the root directory.
Now, back in your terminal, change your current working directory to the root
of the file system using the cd
command (mnemonic: "change directory"). What
happens if we type cd
and then press the "Return" or "Enter" key? Let's wait
before we answer that question.
In order to change to the root directory, we type cd /
. In this case, we're
giving the cd
command an absolute path. How do we know it's an absolute path?
Confirm you're in the root directory by using the pwd
command.
In order to get back to your home directory, you can type cd ~
, or cd $HOME
,
or even just cd
. Try it!
Relative paths are paths described with respect to the current working directory. What command do we use to figure out our current working directory?
So if I'm in my home directory and I see a folder projects
(how would I see
that?), I can guess the full path to projects
is /Users/JWeeks/projects
.
However, since I'm already in the home folder, the ls
command tells me only
unique part of the name needed to distinguish it from sibling directories.
Sibling directories are directories that exist "beside" other directories,
instead of within them ("child directories") or above them ("parent
directories").
This "partial path" is a relative path. It's pretty useful, since I can use it
to change into the directory by using cd projects
. So, in addition to taking
absolute paths, cd
can take relative paths to navigate the file system. Neat!
There are two special relative directories.
..
represents the parent directory..
represents the current directory.
So, in order to navigate to a grandparent (parent of a parent) directory from the current directory, what command would we use?
Now that we're back in the home directory, let's create a place to store all the work we're going to do in this WDI. Naming can often be difficult, but simple names are best. Let's create a subdirectory named "wdi". Always use lowercase names for your directories and files.
Before issuing the command in the terminal, bring your explorer window into
focus, the one with the home directory open. Resize and position it so that it
is visible along with the terminal. Now, in the terminal, execute mkdir wdi
to create the directory (mnemonic: "make directory").
Now, what folder are you in? Did it change by making the new directory?
Create the following directories as well. They should be subdirectories of
wdi
.
trainings
diagnostics
projects
tmp
challenges
practices
studies
Verify that your directory structure resembles the following:
~/wdi
├── challenges
├── diagnostics
├── practices
├── projects
├── studies
├── tmp
└── trainings
Let's use the tmp
directory to play around. tmp
directories are
conventionally used to store files that can be safely deleted. You should never
put anything in them that you want to keep.
Move into the tmp
directory inside wdi
. Create a new file using the touch
command. Let's make a blank text file: touch name.txt
.
Next open that file in atom: atom name.txt
. Write your name inside this file
and save it. On a Mac, the shortcut to save a file is ⌘ + s
. You can also
search for the save command (indeed, any command) using the command palette.
Try it by bringing up the palette with ⌘ + ⇧ + p
. Then search for "Save".
Lets quickly take a peek inside the file we jsut created in our terminal.
Type cat name.text
.
Now type open name.txt
.
See what happened? The file name.txt
opens in whatever default application
your computer uses for that type of file.
Let's say that we didn't mean to call the file name.txt
, how would we go about
editing the file name to correct it?
How do we get rid of this file? We'll use the rm
command. But first: a
warning. This is a very dangerous command. Do not ever press enter after typing
rm
without being certain you're targeting the right file. There's no way
to recover files deleted with rm
. The same advice goes double with r
and f
flags. Flags are switches that occur after dashes when issuing commands.
rm -rf <file_or_directory> # VERY DANGEROUS!
Sometimes I forget how to use certain commands on the command line. My first
step, even before Googling, is to read the command's user manual. You can access
the user manual for a command using man <name_of_command>
in your terminal.
So, to figure out how to use rm
, you would executre man rm
. Do so now. You
should see something like this:
NAME
rm, unlink -- remove directory entries
SYNOPSIS
rm [-dfiPRrvW] file ...
unlink file
...
Read the manual entry for rm
. When you're done reading, you can quit by
pressing q
. Delete the name.txt
file you created in the previous exercise.
Type the command, then check with a colleague before pressing issuing the
command.
Let's use your new skills to create a reference sheet.
Create a cheat sheet of the unix commands that we've used so far. I like to these kinds of files for reference. Describe each command in your own words. Don't just cut and paste from some definition on the web!
- All content is licensed under a CCBYNCSA 4.0 license.
- All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact [email protected].