Files and commands are the backbone of a getting work done on a terminal.
Knowing how to effectively manipulate them is key, so we’ll need to learn the
commands that make manipulating these files possible.
Working with Labs
Labs starter files are distributed through a git repository. To get started:
Remotely Transfer Files (scp)
SCP (for “secure copy”) is a program for copying files from one machine to
another. It uses the same authentication and provides the same security as
ssh. scp will ask for passwords if they are needed for authentication.
To use scp from your terminal (i.e. Terminal.app or iTerm), use the syntax:
scp [-r] <source> <destination>
where <source> and <destination> are one of
the path to a local file, like school/slides.pdf
the path to a file on a server (a “remote” file), like
andrew:~/private/myfile.txt. Note the andrew: specifies the remote server,
and everything after just specifies a file as if you were on that server. If
you used a different name than andrew when setting up ssh in the initial
setup, use that instead.
The optional -r flag signifies that a copy should be done recursively, i.e.
that files and folders should be copied.
Examples
Tip: Examples of command line snippets often begin with $. This symbol is
there for historical reasons to signify that what follows is a shell command.
It’s implied that you don’t type the $ as a part of the command.
MobaXterm comes with a built in SCP client. You should be able to copy files
between your computer and a remote host using the side panel on the left for
transferring files. If this doesn’t work, MobaXterm also supports a rudimentary
scp command line interface, using the same syntax as used for OS X and Linux.
Directories (pwd, cd)
On most systems that use a command line, there’s something called your “current
working directory.” The current working directory is used as the default
directory for many commands if you don’t specify a directory.
There are two commands commonly used to work with the current working directory:
pwd - print working directory
This tells you what directory you are currently in
cd - change directory
This lets you change into a different directory.
Important Directory Names
Some directories are more important than others, so they’re given some shorter
names.
~ – the home directory
~andrewid – the home directory of user “andrewid”
. – the current directory
.. – the parent directory (the directory right above the current one)
/ – the root directory
This is the folder that contains everything.
It has no parent. Try running cd .. from this directory: you’ll end up
back in /!
Note: pwd and . are not the same thing. pwd is a command which
when run prints out the full path of the current directory. . (when used as
a directory) is not a command. It’s merely a shortcut that can be used instead
of typing out an entire directory name.
Running Commands
Commands can be run in a number of ways. Most types of commands you’ll encounter
are “global commands.” This means you can run these commands no matter what
directory you’re in. These commands can be run just by typing their name:
Not every command is available globally. In this case, you have to provide the
path to the command’s program file in order to run it.
Examples
Tip: Lines that don’t begin with $ when $’s are present in a code block
usually mean that those lines are the output from running a particular command.
Listing Files (ls, tree)
One of the most common things you want to do at the command prompt is list the
files in the current directory.
ls [path] - listing files
The program ls allows you to list files and folders within a directory. It can
be passed many different options (or “flags”) that control the output it gives.
tree [path] - recursively listing files
While ls can show you all the files in a folder, it’s much nicer to use tree
when you want to see the contents of folder multiple levels deep.
Hidden Files
ls doesn’t include all files in it’s listing; some of them are “hidden”. To
show hidden files, include the -a flag, which stands for “all”.
Examples
Tip: The # is a comment character in bash (to be discussed later!).
These lines are purely annotations.
Managing Files (cat, less, cp, mv, rm, mkdir)
There are many commands you can use to work with files on UNIX. Here are some of
the more common ones.
cat <filename> - print files
To quickly dump the contents of a file to the console, use cat.
less <filename> - display and scroll through files
The program less is useful if you want to view the contents of a long file
that doesn’t entirely fit on one screen. To exit less after running it, press
q.
You can do tons of other things in less, but one useful thing is to be able to
search. You can search with /banana to find all instances of “banana” in the
file.