Using Files & Commands

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 on Autolab. To get started:

# Download this week's zip file

# Use scp to transfer the zip file to Andrew, as per above:
#
# Non-Windows:
$ scp ~/Downloads/LABNAME.zip ANDREWID@unix.andrew.cmu.edu:~
# or, if you set up the SSH shortcut in the Initial Setup:
$ scp ~/Downloads/LABNAME.zip andrew:~
#
# Windows:
$ scp /mnt/c/Users/USERNAME/Downloads/LABNAME.zip ANDREWID@unix.andrew.cmu.edu:~
# USERNAME is the username you use on your laptop

# Use ssh to log into Andrew.
$ ssh andrew, or ssh ANDREWID@unix.andrew.cmu.edu

# Make a folder to hold your GPI files:
$ mkdir -p ~/private/gpi

# Move the zip file into your GPI directory:
$ mv LABNAME.zip ~/private/gpi

# Use cd to change into your GPI directory:
$ cd ~/private/gpi

# Unzip the lab:
$ unzip LABNAME.zip

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

#
# 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

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:

# 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

Examples

$ pwd
/afs/andrew.cmu.edu/usr10/jezimmer
$ cd private
$ pwd
/afs/andrew.cmu.edu/usr10/jezimmer/private

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

# 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/

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.

$ cat file1
The quick brown fox jumped over the lazy dog.

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.

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
Copyright © 2014, Great Practical Ideas in Computer Science.