Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views56 pages

Introduction To Terminal

You can do almost everything using just the terminal. I It can do many tasks faster than using a graphic interface. I It is sometimes the only option (e.g. accessing a client’s server using SSH). I It is universal.

Uploaded by

Leyre Parraga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views56 pages

Introduction To Terminal

You can do almost everything using just the terminal. I It can do many tasks faster than using a graphic interface. I It is sometimes the only option (e.g. accessing a client’s server using SSH). I It is universal.

Uploaded by

Leyre Parraga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

Introduction to Terminal

Computing in Optimization and Statistics: Lecture 1


Jackie Baek

MIT

January 10, 2017


Overview
Introduction & Motivation
Navigation commands
Files
Basic file commands
File path shortcuts
Hidden Files
.bashrc / .bash profile
Redirection
SSH
Simple Pattern Matching
How bash works
Environment Variables
Documentation
Key Takeaways
What is the terminal?
What is the terminal?

I The terminal is a text-based interface to interact with the


computer.
I Alternate names: console, shell, command line, command
prompt
Example

I Say you want to delete all files in a directory that end with
.pyc

$ rm *.pyc

I This is possible to do without the terminal, but it requires


much more effort.
Why should I learn it?

I You can do almost everything using just the terminal.


I It can do many tasks faster than using a graphic interface.
I It is sometimes the only option (e.g. accessing a client’s
server using SSH).
I It is universal.
Terminal Basics

I We will be using a shell called bash: a program that interprets


and processes the commands you input into the terminal.
I The shell is always in a working directory.
I A typical command looks like:

$ command <argument1> <argument2> ...


Basic navigation commands

pwd: prints working directory.


$ pwd
/Users/jackiebaek
Basic navigation commands

pwd: prints working directory.


$ pwd
/Users/jackiebaek

ls: lists directory contents.


$ ls
Applications Movies
Desktop Music
Documents Pictures
Basic navigation commands

pwd: prints working directory.


$ pwd
/Users/jackiebaek

ls: lists directory contents.


$ ls
Applications Movies
Desktop Music
Documents Pictures

cd <directory>: change working directory to new directory.


$ cd Documents
$ pwd
/Users/jackiebaek/Documents
Tab and arrow keys are your friends

I Use tab to autocomplete commands and file paths.


I Use ↑ and ↓ arrow keys to navigate through your command
history.
I Use clear or cmd-k to clear screen.
What is a file?

A file is a container of data (0’s and 1’s).


What is a file?

A file is a container of data (0’s and 1’s).

A file name usually has an extension (e.g. .pdf, .doc, .csv), but
these are just conventions.
What is a file?

A file is a container of data (0’s and 1’s).

A file name usually has an extension (e.g. .pdf, .doc, .csv), but
these are just conventions.

A file is contained in a directory (folder). Files within the same


directory have unique names.
What is a file?

A file is a container of data (0’s and 1’s).

A file name usually has an extension (e.g. .pdf, .doc, .csv), but
these are just conventions.

A file is contained in a directory (folder). Files within the same


directory have unique names.

Every file and directory has a unique location in the file system,
called a path.
I Absolute path:
/Users/jackiebaek/Dropbox/Documents/hello.txt
I Relative path (if my current working directory is
/Users/jackiebaek/Dropbox): Documents/hello.txt
Working with files
Working with files
mkdir directory name: create a new directory.
$ mkdir new directory
Working with files
mkdir directory name: create a new directory.
$ mkdir new directory
touch file: create an empty file.
rm file: delete a file (Careful! Can’t be undone!)
$ touch brand new file.txt
$ rm brand new file.txt
Working with files
mkdir directory name: create a new directory.
$ mkdir new directory
touch file: create an empty file.
rm file: delete a file (Careful! Can’t be undone!)
$ touch brand new file.txt
$ rm brand new file.txt
nano file: edit contents of a file (many other editors exist).
$ nano helloworld.txt
Working with files
mkdir directory name: create a new directory.
$ mkdir new directory
touch file: create an empty file.
rm file: delete a file (Careful! Can’t be undone!)
$ touch brand new file.txt
$ rm brand new file.txt
nano file: edit contents of a file (many other editors exist).
$ nano helloworld.txt
cat file: prints contents of a file.
$ cat helloworld.txt
Hello, World!
Working with files
mkdir directory name: create a new directory.
$ mkdir new directory
touch file: create an empty file.
rm file: delete a file (Careful! Can’t be undone!)
$ touch brand new file.txt
$ rm brand new file.txt
nano file: edit contents of a file (many other editors exist).
$ nano helloworld.txt
cat file: prints contents of a file.
$ cat helloworld.txt
Hello, World!
cp source target: copy.
mv source target: move/rename.
$ cp helloworld.txt helloworld_copy.txt
$ mv helloworld.txt goodbyeworld.txt
File path shortcuts

. is current directory.

.. is parent directory.
I ../file.txt references a file named file.txt in the parent
directory.
File path shortcuts

. is current directory.

.. is parent directory.
I ../file.txt references a file named file.txt in the parent
directory.

∼ is home.
I expands to /Users/<username> (or wherever home is on that
machine).
I ∼/Documents → /Users/jackiebaek/Documents
I The command cd (without any arguments) takes you to ∼.
Hidden Files

I Files that start with a dot (.) are called hidden files.
I Used for storing preferences, config, settings.
I Use ls -a to list all files.

$ ls
github_notes.md presentation scripts

$ ls -a
. .git github_notes.md scripts
.. .gitignore presentation
.bashrc / .bash profile

I There is a hidden file in ∼ directory called .bashrc or


.bash profile.
I This file is a bash script that runs at the beginning of each
session (i.e. when you open the terminal).
.bashrc / .bash profile

I There is a hidden file in ∼ directory called .bashrc or


.bash profile.
I This file is a bash script that runs at the beginning of each
session (i.e. when you open the terminal).
I This file can be used to set variables or to declare aliases.
I alias new command=command

$ alias athena="ssh [email protected]"


Redirection

> redirects output to a file, overwriting if file already exists.


$ ls > out.txt

>> redirects output to a file, appending if file already exists.


$ python fetch_data.py >> output.csv
Redirection

> redirects output to a file, overwriting if file already exists.


$ ls > out.txt

>> redirects output to a file, appending if file already exists.


$ python fetch_data.py >> output.csv

< uses contents of file as STDIN (standard input) to the


command.
$ python process_stuff.py < input.txt
Secure Shell (SSH)
I Sometimes we need to work on a remote machine.
I We need more computing power than just our local machine.
I We need to access data from a client’s server.
I Can use SSH to securely access the terminal for the remote
machine.
Secure Shell (SSH)
I Sometimes we need to work on a remote machine.
I We need more computing power than just our local machine.
I We need to access data from a client’s server.
I Can use SSH to securely access the terminal for the remote
machine.

$ ssh [email protected]
Secure Shell (SSH)
I Sometimes we need to work on a remote machine.
I We need more computing power than just our local machine.
I We need to access data from a client’s server.
I Can use SSH to securely access the terminal for the remote
machine.

$ ssh [email protected]

Password:
Secure Shell (SSH)
I Sometimes we need to work on a remote machine.
I We need more computing power than just our local machine.
I We need to access data from a client’s server.
I Can use SSH to securely access the terminal for the remote
machine.

$ ssh [email protected]

Password:

Welcome to Ubuntu 14.04.5 LTS


...
Last login: Tue Aug 30 10:11:49 2016 from howe-and-ser-...

baek@howe-and-ser-moving:~$

Use logout to exit SSH session.


Secure Copy (scp)

Can transfer files between local and remote machines using the scp
command on your local machine.

Move my file.txt from local machine to remote home directory.


$ scp my_file.txt [email protected]:~

Move remote file.txt from remote to local machine.


$ scp [email protected]:~/remote_file.txt .
Simple Pattern Matching (Globbing)
I Match [multiple] filenames with wildcard characters.
I Similar to regular expressions, but slightly different syntax.
Simple Pattern Matching (Globbing)
I Match [multiple] filenames with wildcard characters.
I Similar to regular expressions, but slightly different syntax.
Example:
$ ls
a1.txt a2.pdf apple.txt bar.pdf
Simple Pattern Matching (Globbing)
I Match [multiple] filenames with wildcard characters.
I Similar to regular expressions, but slightly different syntax.
Example:
$ ls
a1.txt a2.pdf apple.txt bar.pdf

$ echo a*
a1.txt a2.pdf apple.txt
Simple Pattern Matching (Globbing)
I Match [multiple] filenames with wildcard characters.
I Similar to regular expressions, but slightly different syntax.
Example:
$ ls
a1.txt a2.pdf apple.txt bar.pdf

$ echo a*
a1.txt a2.pdf apple.txt

$ echo a[0-9]*
a1.txt a2.pdf
Simple Pattern Matching (Globbing)
I Match [multiple] filenames with wildcard characters.
I Similar to regular expressions, but slightly different syntax.
Example:
$ ls
a1.txt a2.pdf apple.txt bar.pdf

$ echo a*
a1.txt a2.pdf apple.txt

$ echo a[0-9]*
a1.txt a2.pdf

$ echo *.pdf
a2.pdf bar.pdf
Simple Pattern Matching (Globbing)

Figure: Source: Wikipedia


Simple Pattern Matching (Globbing)

Figure: Source: Wikipedia

Remove all files that end with .pyc


$ rm *.pyc
Simple Pattern Matching (Globbing)

Figure: Source: Wikipedia

Remove all files that end with .pyc


$ rm *.pyc

Copy all files that has ”dog” in its name to the animal/ directory.
$ cp *dog* animal/
How bash works
How bash works

I Bash is a programming language.


I Can set variables, use for loops, if statements, comments, etc.
How bash works

I Bash is a programming language.


I Can set variables, use for loops, if statements, comments, etc.
I There are several special ”environment” variables (i.e.
$PATH, $HOME, $USER, etc.) that many programs rely on.
How bash works con’t
What happens when you type in a command, say pwd?
How bash works con’t
What happens when you type in a command, say pwd?
I Bash runs the program called pwd.
I Where is this program?
I Usually under a directory called bin, which stands for binary.
How bash works con’t
What happens when you type in a command, say pwd?
I Bash runs the program called pwd.
I Where is this program?
I Usually under a directory called bin, which stands for binary.
I When you type in a command, bash looks for a program with
that name under the directories listed in the $PATH
environment variable.
How bash works con’t
What happens when you type in a command, say pwd?
I Bash runs the program called pwd.
I Where is this program?
I Usually under a directory called bin, which stands for binary.
I When you type in a command, bash looks for a program with
that name under the directories listed in the $PATH
environment variable.

$ echo $PATH

/Users/jackiebaek/.local/bin:/Users/jackiebaek/.cabal/bin:/
Applications/ghc-7.10.3.app/Contents/bin:/usr/local/bin:
/usr/bin:/bin:/usr/sbin:/sbin:/usr/texbin

I $PATH contains is a list of directories separated by :


I Bash looks into each of these directories to look for the
program pwd.
Documentation
I To look up documentation for a particular command, use
‘man command‘. (man = manual)
$ man mkdir

NAME
mkdir -- make directories

SYNOPSIS
mkdir [-pv] [-m mode] directory_name ...

DESCRIPTION
The mkdir utility creates the directories named as operands
...

I d for down, u for up, q to quit.


I Commands can have required and/or optional arguments.
I Optional arguments usually come first, and are indicated by a
hyphen (-). These are called flags.
Key Takeaways
Key Takeaways

I Basic commands: ls, cd, pwd, cat, cp, mv, rm, mkdir
Key Takeaways

I Basic commands: ls, cd, pwd, cat, cp, mv, rm, mkdir
I Google is your friend.
Key Takeaways

I Basic commands: ls, cd, pwd, cat, cp, mv, rm, mkdir
I Google is your friend.
I So is tab for autocomplete, arrow keys for history.
Key Takeaways

I Basic commands: ls, cd, pwd, cat, cp, mv, rm, mkdir
I Google is your friend.
I So is tab for autocomplete, arrow keys for history.
I Be careful with rm.
Key Takeaways

I Basic commands: ls, cd, pwd, cat, cp, mv, rm, mkdir
I Google is your friend.
I So is tab for autocomplete, arrow keys for history.
I Be careful with rm.
I Getting comfortable with the terminal can be daunting at
first, but it has the potential to greatly boost your efficiency!
Thank you!

You might also like