teaching-materials.org/cli
Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
Some "rules"
You might use the command line to...
These are all pretty much the same thing.
A text-based command interpreter.
The most common shell is "bash".
For Mac OS or Linux, use the "Terminal" application.
For Windows, use Git Bash.
(Download here if you haven't installed yet.)
Usually shows your username and computer name.
Indicates that the terminal is ready for a command.
Indicates your current spot in the terminal.
Shows you where the stuff you type will go.
echo hello
into your terminal.pwd
Type it whenever you want to see what directory (folder) you’re in.
pwd
(Print Working Directory)
The clear
command clears the contents of the terminal and issues a prompt.
This is good for removing previous output that is unnecessary to the task at hand.
Feel free to use this whenever things get too cluttered.
Also referred to as "folders".
A container for files or other directories.
Nested files and directories can be referenced using paths
.
The set of all folders, taken together, makes up your entire file system.
This system is organized into a kind of upside down tree.
At the very top of the tree is the root folder.
Each directory or file is separated by a forward slash "/"
There are two kinds of paths:
Desktop/the_project/overview.txt
/Users/jane/Desktop/logo.png
The cd
command changes the current working directory.
It expects a file path as an "argument".
If no file path is given, it assumes your home directory by default.
.
..
~
-
Bonus: Drag a folder into the terminal to show its path.
(Doesn't quite work in Windows.)
The ls
command lists the contents of a directory.
It expects a file path as an "argument".
If no file path is given, it assumes the current directory by default.
The ls
command accepts several option flags.
A flag is a special argument that is used to set an option for the command.
These are commonly a hyphen followed by a single character (e.g. -g
)
Setting the -l
flag on the ls
command causes it to provide more verbose (long) output.
Filenames that begin with a period are hidden from normal output.
e.g. ".bashrc"
Use the ls
command with the -a
flag to see hidden files in addition to the usual output.
Type ls -la
into your terminal.
Use the -h
flag to get human readable file sizes.
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
Tab completion autocompletes commands and filenames.
$ cd P
Play with the cd
and ls
commands.
.
shortcut..
shortcut~
shortcutcd
without an argumentUse pwd
to check your location periodically.
Use the open
command to open a file or directory in its default app.
Pass the path of the file or directory name as the argument.
It does the equivalent of double-clicking the item.
(Sadly, this does not work in Windows. 😞)
Use the mkdir
command to create a new empty directory.
Pass the path of the directory name as the first argument.
If the base of the path doesn't already exist, the command will fail.
Use the -p
flag to create the full path if non-existent.
Use the rmdir
command to remove an empty directory.
Use rm -r
to remove a non-empty directory.
cd
to your home directory.girl/develop
directory path.girl/develop
directory.it
directory.pwd
command to verify you are home.girl/develop/it
path.Use cat
to output the contents of a file to the console.
Use more
to step through the contents of a file one screen at a time.
Use less
to step backwards or forwards.
Explore the /usr/share/misc
files using cat
, more
, and less
Can you find your birth month's stone and flower in the birthtoken
file?
touch
Use the touch command to create a new file.
The touch command expects the name of your new file as an argument.
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 origin destination
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 origin destination
Use the mv command to move a file or directory.
The mv command takes two arguments:
1st argument = the "origin"
2nd argument = the "destination"
mv origin destination
mv orig dest
rm
Use the rm command to remove a file.
Expects the name of the file you are removing as an argument.
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
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
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.
find
Use the find command to find files according to name/metadata.
Find all txt files under the current directory:
find . -name '*.txt' -print
Most commands display their results to a mechanism called the standard output.
By default, this directs its content to the display.
The standard output can be redirected to a file using the >
operator.
ls > file_list.txt
In order to append to the file instead of overwriting it, use the >>
operator instead.
ls >> file_list.txt
Both the >
and >>
operator will create the file if it doesn't exist.
Whenever commands accept keyboard input, it's likely they are really just drawing input from a mechanism called standard input.
By default, this is set to keyboard input.
The input to a command can be redirected to a file by using the <
operator.
sort < file_list.txt
Both input and output can be redirected at the same time.
sort < file_list.txt > sorted_file_list.txt
Check out the man pages for the following:
sort
uniq
grep
head
tail
fmt
pr
tr
sed
awk
The "|" character can be used to allow commands to communicate during execution.
Pipes are placed between commands.
A pipe will cause the output of the left command to be used as the input of the right command.
ls -l | grep "myfile.txt" du | sort -nr
Use the ls
and grep
commands to print out only the files in your home directory that contain the word "bash".
ls -a | grep bash
Bonus for Macs: Hold the option
key and click to move the cursor.
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:
up
and down
arrows to locate a past command with one or more arguments.date
command.!
.time !!
.Let's revisit the use cases from the beginning of class and go into more detail.
python -m simpleHTTPServer
Run this from the directory you want to serve.
Press Ctrl + C
to exit.
Check out Try Git for an intro, or watch for our next GDI Git workshop.
Installation instructions from an actual open source project:
These "other" computers might be:
ssh gdi@192.168.0.23
tail -f /var/log/wifi.log
Different processes have different ways of exiting back to the prompt. If you're stuck, try one of these:
q
:q
:q
You may need to install the software that uses the command.
Try searching online for:
how to install [command-name-here] on [Mac/Windows/Linux]
cal
say hello