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.
Labs starter files are distributed through a git repository. To get started:
# Use ssh to log into Andrew.
$ ssh andrew, or ssh ANDREWID@unix.andrew.cmu.edu
# Clone the GPI repo:
$ git clone https://github.com/cmugpi/gpi-labs.git ~/private/gpi-labs
# Use cd to change into your GPI directory:
$ cd ~/private/gpi-labs
# When you're done with the lab, scp it back to your computer so that
# you can submit it on Autolab:
#
# Non-Windows:
$ scp ANDREWID@unix.andrew.cmu.edu:~/path/to/handin.zip ~/Downloads/
# or, if you set up the SSH shortcut in the Initial Setup:
$ scp andrew:~/path/to/handin.zip ~/Downloads/
#
# Windows:
$ scp ANDREWID@unix.andrew.cmu.edu:~/path/to/handin.zip /mnt/c/Users/USERNAME/Downloads/
# USERNAME is the username you use on your laptop
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
school/slides.pdf
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.
#
# Remember: replace 'andrew' with 'ANDREWID@unix.andrew.cmu.edu'
# if you use the latter when SSH'ing
#
$ scp school/notes.txt andrew:notes_sept_2.txt
# Copies school/notes.txt from your computer to Andrew and renames it
$ scp andrew:~/private/myfile.txt projects/
# Copies ~/private/myfile.txt from Andrew to your computer and puts it
# in the projects directory.
$ scp -r school/projects andrew:~/private/
# Copies the whole school/projects folder to Andrew and places it in
# the ~/private/ folder
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.
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 directoryThis tells you what directory you are currently in
cd
- change directoryThis lets you change into a different directory.
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
cd ..
from this directory: you’ll end up
back in /
!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:
# Some example commands:
$ ls
$ mkdir my-folder
$ rm useless-file
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.
# If the program is in your current directory, you can use
# the '.' shortcut for the current directory:
$ ./command-in-current-directory
# Similarly, if the program is in the parent directory, you can use '..'
$ ../command-in-parent-directory
$ pwd
/afs/andrew.cmu.edu/usr10/jezimmer
$ cd private
$ pwd
/afs/andrew.cmu.edu/usr10/jezimmer/private
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 filesThe 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 filesWhile 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.
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”.
# Contents of the current folder
$ ls
file1 folder1
# Contents of the current folder, including hidden files
$ ls -a
.hidden-file file1 folder1
# The -l flag tells ls to give you more information
$ ls -l
total 2
-rw-r--r-- 1 jezimmer 0 Aug 17 18:20 file1
drwxr-xr-x 2 jezimmer 2048 Aug 17 18:21 folder1
# If you specify a path, ls will print in that path instead of the
# current working directory
$ ls folder1
file2
# List multiple levels of folders
$ tree
.
├── folder1/
│ ├── bar.txt
│ └── foo.txt
├── folder2/
│ └── not-hidden
└── folder3/
3 directories, 3 files
# Tree also permits -a for listing hidden files
$ tree -a
.
├── folder1/
│ ├── bar.txt
│ └── foo.txt
├── folder2/
│ ├── .hidden
│ └── not-hidden
└── folder3/
There are many commands you can use to work with files on UNIX. Here are some of the more common ones.
cat <filename>
- print filesTo quickly dump the contents of a file to the console, use cat
.
$ cat file1
The quick brown fox jumped over the lazy dog.
less <filename>
- display and scroll through filesThe 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.
cp <source> <destination>
- copy files# Copy existing file to new file
$ cp file1 file3
$ ls
file1 file3 folder1/
# The contents of the new file are the same
$ cat file3
The quick brown fox jumped over the lazy dog.
# You can also use the -r option to copy directorys
$ cp folder1 folder2
$ ls
file1 file3 folder1/ folder2/
mv <source> <destination>
- move and rename files# Moving a file "into" another file is how you rename files
$ mv file1 file4
$ ls
file3 file4 folder1/ folder2/
# Move file into directory
$ mv file3 folder1
$ tree
.
├── folder1/
│ ├── file2
│ └── file3
├── folder2/
│ └── file2
└── file4
2 directories, 4 files
rm <filename>
- PERMANENTLY delete files$ rm file4
$ ls
folder1/ folder2/
# use the -r flag to recursively remove a directory
$ rm -r folder2/
$ ls
folder1/
mkdir <directory>
- make directories$ mkdir folder3
$ tree
.
├── folder1/
│ ├── file2
│ └── file3
└── folder3/
2 directories, 2 files
touch <file>
- create an empty file$ touch file1
$ ls
file1