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 hellointo your terminal.
Type it whenever you want to see what directory (folder) you’re in.
(Print Working Directory)
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
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:
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.)
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 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.
-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.
ls command with the
-a flag to see hidden files in addition to the usual output.
ls -la into your terminal.
-h flag to get human readable file sizes.
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
cdwithout an argument
pwd to check your location periodically.
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. 😞)
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.
-p flag to create the full path if non-existent.
rmdir command to remove an empty directory.
rm -r to remove a non-empty directory.
cdto your home directory.
pwdcommand to verify you are home.
cat to output the contents of a file to the console.
more to step through the contents of a file one screen at a time.
less to step backwards or forwards.
/usr/share/misc files using
Can you find your birth month's stone and flower in the
Use the touch command to create a new file.
The touch command expects the name of your new file as an argument.
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
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
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
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.
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
ls > file_list.txt
In order to append to the file instead of overwriting it, use the
>> operator instead.
ls >> file_list.txt
>> 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
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:
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
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:
downarrows to locate a past command with one or more arguments.
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.
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:
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:
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]