GDI Logo

Introduction to the Command Line

Welcome!

Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.

Some "rules":

  • We are here for you!
  • Every question is important
  • Help each other out
  • Have fun

Meet Your Instructor


Maureen McElaney

@mo_mack

Now it's all about you!

  • Tell us who you are.
  • What do you hope to get out of this class?

What we'll be covering in this class

  • Navigating and searching through files and directories
  • Input/Output Redirection
  • Installing Programs
  • Changing file permissions and file ownership

What is a Shell?

A text-based command interpreter


The most common shell is called BASH

For OS X, use a shell via the “Terminal” app

The Shell

The Terminal

The Prompt

Usually shows your username and computer name (hostname).


It indicates that the shell is ready for you to enter a command.

Prompt

The Prompt

Command Structure

$ command -options argument

Don’t forget to hit enter at the end of each command.

Your First Command

echo

Type echo hello to print hello to the screen

Man Pages

man

The man command brings up the manual for the specified command. Use <space> or the arrow keys to page through and press q to exit.

$ man ls

Love the Tab

Tab completion autocompletes commands and filenames.

  • Pressing <tab> once autocompletes a unique instance.
  • Pressing <tab> twice gives you all the options available.
$ cd P

The Current Directory

pwd

(Print Working Directory)


Type it whenever you want to see what directory (folder) you’re in.

pwd

(print working directory)

pwd

Directories

Containers for files or other directories.


Also referred to as "folders".


Nested files and directories can be referenced using paths.

File Paths

A sequence of nested directories, with a file or directory at the end.


Each directory or file is separated by a forward slash "/".


Two Types of Paths


Relative:
Desktop/dougthepug.jpg


Absolute:
/Users/anastasialanz/Desktop/dougthepug.jpg

Unix File Structure

Unix File Structure

In OS X, /home/you becomes /Users/you

File System Structure

Varies between systems, but some common folders:

  • /bin: Basic commands
  • /etc: System config files
  • /tmp: Temporary files
  • /usr: Regular user tools
  • /usr/local: Installed user software
  • /var: Data used by system

Shortcuts

  • Current Directory: (.)
  • Parent Directory: (..)
  • Home Directory: (~)
  • Root Directory: (/)

Change Directory

cd

Use the cd command to change directories.


Expects a file path as an argument.


If no file path is given, it assumes your home directory (~) by default.

cd

(change directory)

cd

There are 2 other ways that we could have gotten to our home directory...what are they?

Clear your Terminal

clear


The clear command clears the contents of the terminal and issues a prompt.

$ clear

Option + L will also clear the terminal.

List Directory

ls

Use the ls command to list the contents of a directory.


Expects a file path as an argument.


If no file path is given, it assumes the current directory by default.

ls

(list directory)

ls

Flags

Some commands have additional options that you can specify by using flags.


Flags are preceded by a hyphen, and are often a single character.

$ ls -a
$ ls -l

ls -a

-a = show hidden files

ls -a

Notice that hidden file names begin with a "."

ls -l

-l = use a “long list” format (shows more verbose output)

ls -l

ls -al

Show hidden files and use a long list format

ls -al

You can combine flags or list them separately separated by spaces. (e.g. “ls -a -l”)

File Names

  • case sensitive
  • any character (besides “/”)
  • file extensions are for user convenience


Examples:

README   .bashrc   index.html    index.html.old    Bugs List.txt

Making and Removing Directories

Use the mkdir command to create a new empty directory.


Use the rmdir command to remove an empty directory.


Use rm -r to remove a non-empty directory
...be careful!


All 3 commands expect the name of a directory as an argument.

mkdir, rmdir

(make directory, remove directory)

mkdir, rmdir

Develop it!

Exercise 1

  1. Change to your home directory desktop
  2. Create a gdi/burlington directory path
  3. Navigate into the gdi/burlington directory
  4. Create a CLI directory
  5. View the contents of the CLI directory
  6. Navigate up two directories
  7. Use the pwd command to verify you are home

Create a File

touch

Use the touch command to create a new file.


The touch command expects the name of your new file as an argument.

touch

(create a file)

touch

Copy a File

cp

Use the cp command to copy a file.


The cp command takes two arguments:

1st argument = the "origin" file

2nd argument = the "destination" file

$ cp resume.txt resume-copy.txt

cp

(copy a file)

$ cp orig dest
cp

Copy a Directory

cp -R

Use the cp -R command to copy a directory.


The cp -R command takes two arguments:

1st argument = the "origin" directory

2nd argument = the "destination" directory

$ cp -R homework old-homework

cp -R

(copy a directory)

$ cp -R orig dest
cp-R

Moving (or renaming) a File/Directory

Use the mv command to move a file or directory.


The mv command takes two arguments:

1st argument = the "origin"

2nd argument = the "destination"

Move a File/Directory

mv orig dest

move file/directory

Rename a File/Directory

mv orig dest

rename file/directory

Remove a File

rm

Use the rm command to remove a file.


Expects the name of the file you are removing as an argument.

rm

(remove a file)

remove a file

Develop it!

Exercise 2

  1. Create a folder called gdi
  2. Make that folder your current working directory
  3. Create two files: file1.txt, file2.txt
  4. Copy file1.txt and call the copy file3.txt
  5. Create a directory called folder1
  6. Move file1.txt into folder1
  7. List the contents of folder1 without going into it
  8. Rename file1.txt to myfile.txt
  9. Clear your terminal

View a File

cat, more, less

Use the cat command to output (catenate) the contents of a file to the console.


Use the more or less commands to read a file:

  • more allows you to step through the contents of a file a screen at a time
  • less allows you to step backwards or forwards
    • Press q to exit out of this mode

cat

(catenate)

will output the entire file

cat

more

more

less

less

Edit a File

You can use various editors built into bash:

$ vi myfile.txt

$ emacs myfile.txt

$ pico myfile.txt


Or on a Mac, you can open with any desktop app:

$ open -a TextEdit myfile.txt

Or with the default editor:

$ open -t myfile.txt

Search within a File

grep

Use the grep command to search in files.


The grep command outputs only the lines in a file that match a given pattern.


The 1st argument is the pattern to match, and the 2nd, 3rd, and so on are the files to search within.

$ grep pattern file

grep

(search within a file for text that matches a pattern)

grep

Wildcard Matching

Use the * (asterisk) symbol to match anything.


You can use this to search through all of a particular file type.

$ grep hello *.txt

The shell will build a list of all the files that match the non-asterisk part.

Finding Files

find

Use the find command to find files according to name/metadata.


Find all txt files under the current directory:

find . -name '*.txt' -print

find

(finding files)

find

Redirecting Output

By default, input comes from the screen and output goes to the screen.


You can use < and > to redirect input/output.


A practical example: Save results of search in a file:

grep hello *.txt > results.txt

Redirecting Output

redirection

Redirecting Output

Use the echo command to add a new line of text.


Use the > to replace the contents of a file.


Use the >> to add to the end of a file.

Redirecting Output

redirection

Develop it!

Exercise 3

  1. In the gdi directory, output the contents of file2.txt to the terminal
  2. Add a sentence to file2.txt
  3. Add a few more sentences to file2.txt
  4. Search the file for the word of your choice and add the results to file3.txt

Command Line Movement

  • ctrl-a: jump to beginning of line
  • ctrl-e: jump to end of line
  • ctrl-u: clear the line
  • ctrl-l: shortcode for clear
  • ctrl-r: search previous commands
  • ctrl-c: get out of trouble
  • ctrl-d: exit terminal

More Command Line Movement

  • The left/right arrow keys let you edit within a command
  • The up/down arrow keys let you select previous commands
  • tab auto-completes filenames


Lots more, and you can create your own.

Command Line History

Use the history command to see a list of all your previous commands.


Each command will be listed next to a line number.


A few history-related commands:

  • !!: Latest command
  • !568: Command by line #
  • !open: Command matching string

history

history

Develop it!

Exercise 4

  1. Use your up and down arrows to locate a past command with one or more arguments
  2. Jump to the beginning of the line
  3. Jump to the end of the line
  4. Look at your command line history
  5. Begin to type a previous command and experiment with using tab to complete it

curl

A command line tool for getting or sending files using URL syntax.

$ which curl

Download a file with curl

The command below will download a .txt file

$ curl -OL example.com/pugs.txt

head

Shows the first 10 lines of a file.

head command

tail

Shows the last 10 lines of a file.

tail command

wc

(Word count)

This command shows newline count, word count and byte count.

Pipe


The output of one command is the input of another command.


$ head cat.txt | wc

Use this command to chain multiple commands together.

Develop it!

Exercise 5

  1. Download the cat.txt file by running this command:
    $ curl -OL gdibtv.github.io/intro-to-command-line/cat.txt
  2. Use the head command to see the first 10 lines of text.
  3. Use the tail command to see the last 10 lines of text.
  4. Pipe the results of grep cat cat.txt command through wc to count the number of times "cat" appears.
  5. Using the grep man page, find the option for case insensitive matching. Try to find how many times "cat" and "Cat" appear.

Installing Programs

Use Homebrew for MacOs.

Use apt-get for Ubuntu.

cowsay

A program that generates ASCII pictures of cows with a message.

$ brew install cowsay

Extras

Open a URL in the browser with command line

$ open http://girldevelopit.com

Extras

Make your computer talk from the command line

$ say i love the command line

Develop it!

  1. Install cowsay on your environment.
  2. Use the cowsay man page to list the different cowfiles.
  3. Generate 5 different cowsay cowfiles pictures using the - f option.
  4. Open a url of your choice from the command line.
  5. Use the man pages and find a different voice output for the say command.

Free Resources

Thanks!

Maureen McElaney

burlington@girldevelopit.com

@mo_mack