Introduction to the Terminal
Overview:
- Examine files and directories (
pwd, ls
)
- Make and remove files and directories
- Files:
touch [filepath]
, rm [filepath]
- Directories:
mkdir [dir name]
, rmdir [path to empty dir]
- Naivigating through directories:
cd [dir path]
- Copy and move files:
cp [filepath] [destination path]
, mv [filepath] [destination path]
- Copy and paste file contents:
- Copy:
pbcopy < [filepath]
- Paste:
pbpaste > [destination filepath]
Vocab:
- Terminal - Program for interacting with the command line
- Shell / Command Line - Low level, text-based interface for the computer
- Bash - A type of shell found on Unix systems
- Directory - folder
- command - instructions for a computer to execute via the command line
- argument - info needed to run a certain command
- flag - an optional arugment, usually prefixed by - or --
Anatomy of a command
$ mkdir -p Turing/Week01
$ [command] [flag] [argument]
Why use the command line?
- Speed / Efficiency
- Sometimes you really need it
- Computer competency (it make you look good)
Get the terminal open:
On a mac:
- Press
command + spacebar
to open Spotlight
- Type
terminal
- Press
enter
Checking out File Structure
-
pwd
- print working directory
- Check the current directory you're in
-
ls
- list
- List the items in the directory (both files and other directories)
- Flags:
-l
gives more detail about the items
-a
reveals hidden files
Your Turn
- In the terminal, figure out the name of your root directory (base directory that the terminal starts in)
- Figure out what files / directories are in the root directory
Making and Removing Directories and Files
mkdir [dir name]
- make a new directory
touch [file name / file path]
- create a new file
-
rm [file path]
- remove (delete) file (permanantly)
-
Flags:
-R
remove a directory and its contents
-i
require confirmation before removing
rmdir [directory name]
- remove empty directory
Navigating through directories
-
cd [directory path]
- change directory
- To move up a level (out of a directory):
cd ..
- To get to root directory:
cd ~
- Separate folders by
/
- Use
tab
to autocomplete
Give it a try
- Navigate to the root directory
- List its contents
- Make a new directory
- Make another directory inside that directory
- Make three more nested directories
- Delete them one by one
Adding files / directories from your working directory
-
Chain things together with
/
-
Works for
cd
, ls
, and touch
-
Need
-p
flag for mkdir
Copy and Move Files / Directories
-
cp [source path] [destination path]
- make copy of file / directory and pastes in the destination
-
mv [source path] [destination path]
- move file / directory from source to destination
Give it a shot
- Make three nested directories
- Make a .txt file in each directory
- Copy the file from the top directory into the second one
- Move the file in the bottom directory into the top one
- Extra: try renaming the files using
mv
or cp
Copy and paste file contents
pbcopy < [file path]
- copy file contents
pbpaste > [destination file path]
- paste copied file contents
Try it out
- Open a file you made in the last exercises in Sublime (try it in the command line)
- Add some text and save it
- Copy the file's contents
- Paste the contents into a file in another directory
- Open the edited file in Sublime to make sure it worked
Learn More
- Keep practicing in the terminal!
- Use cheatsheets
-
Use
info [command]
or man [command]
to get more information on commands
- type
q
to exit these menus
- Check out the advanced topics at the bottom of the lesson plans
Review
- What's the difference between
mv
and cp
?
- How do we move between files?
- What does
mkdir
do?
- How do you make a file?
- What is a flag? Give an example.
- What is a shell?
- How do we remove a file?
- How do we remove a directory?