teaching-materials.org/cli
Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
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.
cal into your terminal and press enter.echo hello into your terminal and press enter.say hello into your terminal and press enter.echo and say commands.Many commands take one or more arguments, which come after the command, and give detail about what the command should do.
For example, echo takes an argument representing the text to be repeated.
echo "This is an argument."
manThe man command brings up the manual for the specified command. Press the space bar or the arrow keys to page through and press q to exit.
man echo
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.
pwd(Print Working Directory)
Type it whenever you want to see what directory (folder) you’re in.
pwd(Print Working Directory)
Also referred to as "folders".
A directory is a container for files or other directories.
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.
Nested files and directories can be referenced using paths.
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.pngThe 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.
cd
...~-Bonus: Drag a folder into the terminal to show its path.
(Doesn't quite work in Windows.)
Tab completion autocompletes commands and filenames.
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.
ls
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)
ls -lSetting 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.
ls -la
cd & lsPlay with the cd and ls commands.
. shortcut.. shortcut~ shortcutcd without an argumentUse pwd to check your location periodically.
Use mkdir 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.
mkdir
Use rmdir to remove an empty directory.
Use rm -r to remove a non-empty directory.
rmdir
cd to your home directory.pwd command to verify you are home.
Use touch to create a new file.
The touch touch command expects the name of your new file as an argument.
touch
Use cp to copy a file.
The cp command takes two arguments:
cp resume.txt resume-copy.txt
Use cp -R to copy a whole directory and all files in it.
cpcp origin destination
cp -Rcp -R origin destination
Use mv to move a file or directory.
The mv command takes two arguments:
If the destination is a filename, the file will be renamed.
mv origin destination
mv orig dest
Use rm to remove a file.
The rm command takes the name of the file you are removing as an argument.
rm
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.
Use open to open a file or directory in its default app—the equivalent of double-clicking it.
(Sadly, this does not work in Windows. 😞)
Pass the path of the file or directory name as the argument.
You can use various editors built into bash, including vi, emacs, nano, and pico.
Enter the editor command and the file path:
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
cat, more, and less to read different text files on your computer. (Mac/Linux users, try exploring /usr/share/misc.)pico or nano to add a few sentences to file2.txt, then exit and save.open to open file2.txt in your favorite text editor. (Windows/Linux users, your text editor may have a built-in command, like atom.)
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.
Though you can do a lot with GUI tools for Git, there are some functions that still require the command line.
git pull upstream master
Check out Try Git for an intro, or watch for our next GDI Git workshop.
Build tools process your code to make it more efficient or to automate repeated tasks.
For example, you can use tools like webpack to combine multiple JS files into one "minified" file.
webpack ./src/index.js dist/bundle.js
Static Site Generators like Jekyll build websites using templates, avoiding duplicated html.
jekyll build
When working on a website locally, you can run a simple server program on your computer so you can browse the site over http instead of the file protocol.
This example uses a built-in function of Python:
python -m simpleHTTPServer
Most build tools also include a local server function.
To stop a running server, press Ctrl + C.
You can write or find scripts to batch process files.
for example, this open source script parses PDF bank statements and converts the data to a format that can be imported into banking software:
perl chase-bank-PDF-to-QIF.pl -oChase2018.qif ~/statements/*.pdf
These "other" computers might be:
ssh gdi@192.168.0.23
Or it could be a very complicated doorbell alternative
Different processes have different ways of exiting back to the prompt. If you're stuck, try one of these:
q:q:qcommand not foundIf you receive a command not found error message, check for typos!
Otherwise, 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]"