- Define and explain the strengths and weaknesses of GUIs and CLIs
- Learn how to access the terminal and break down its components
- Describe the file system and how it relates to the CLI
- Introduce common commands and list unsafe ones
- Learn how to find more information on using commands
The majority of our interactions with computers is through a graphical user interface, or a GUI. A GUI is a great tool -- it adds a level of user experience that allowed computers to become more popular and mainstream.
The Command Line Interface (CLI) is a text-based way of interacting with your computer, that gives you more abilities than a GUI has, but with a higher learning curve.
The CLI works by typing commands (running programs) into a terminal and the computer executes those commands or gives you a fairly descriptive error regarding why it did not work.
While the GUI is perfect for the average computer user, there are drawbacks for users like us. The CLI offers many benefits:
-
Power/Speed. Most tasks can be completed faster with the cli. Features like tab completion, command history (hitting the up arrow in your terminal), and piping contribute to this.
-
Precision. Each command does only one thing and we can read them and understand what they do before we run them.
-
Repeatability / Scriptability. These commands can be saved and reused by others. What you did during Installfest was run a script that we shared with you!
-
Tools. There are a lot of open source tools that you can use on the cli, you can install them with cli package managers like Advanced Packaging Tool (apt) on Linux or Homebrew on MacOS. Because each tool does one specific thing, it is possible to chain multiple tools together to automate processes.
-
Debugging. The errors are better, the errors that you get from a GUI can be unhelpful while cli errors are generally more thought out and descriptive.
How do we access this text-based interface from our GUI desktop? We run what's
called a terminal application (also often referred to as a terminal emulator).
The default on OSX is Terminal.app. When you open a new Terminal window, the
Terminal app will call a program called a shell.
A shell is a program that takes commands, passes them to the operating system and returns any output or errors. The default shell used by terminal is called Bash or ZSH on MacOS Catalina or later. There are other shells but all operate very similarly.
Let's fire up our terminals and get exploring!
⌘ (Command) + Space, or open Spotlight- type "Terminal"
Enter
- Keep it locked in your dock. Right click on the icon, highlight options, check "Keep in Dock".
When Terminal launches, it will start in your computer's home directory
(whatever you named your computer). Your home directory is denoted by the tilde
~.
The prompt is the shell asking for input; when you see $ <something> in
documentation, it generally means, input this command into a shell.
If you have a terminal open but do not see a prompt, that means that the shell is not ready to receive input.
Typing a random string of characters and hitting enter will produce a message
-bash: <your-random-string>: command not found
What is a command?
A command is a program. Some come built into the shell and provide the basics for interacting with the operating system and some are written by programmers (like you!) to provide further functionality.
pwd will print the current working directory. It shows you a path. This
path shows you where you are currently located in the filesystem. It's like
sending up a flare or homing beacon, where you can see how far you have
'traveled' from the root directory.
You can think of a path as being similar to an address. There are two types of paths: absolute and relative.
Absolute vs Relative
A relative path is similar to giving someone directions to a destination from their current location. Where is General Assembly? Two blocks up 15th street from where you are now.
An example of an absolute path to General Assembly could be 1133 15th St NW, 8th Floor, Washington, DC 20005 or a longitude and latitude(38.9048728, -77.0340283).
An absolute path is comparable to giving someone the direct location from a universally known starting point. On the command line that known starting point is the root directory.
ls- Lists the contents of the current directory. You can see:- the immediate child directories (the directories inside this directory)
- the files in this directory
Bash commands can take flags or options denoted by a dash -
ls -a- list content including hidden files and directories. Hidden files and directories begin with a period, for example,.git.ls -l- list content and give meta information about each item
Directories (folders) can have directories within them, and there can be directories inside those directories, ad infinitum. This creates a tree structure where directories can have many children with many different branches.
We can navigate to other directories relative to the current working directory.
cd some_directory- navigates into a child directory called
some_directory.some_directoryis a child of the current directory.
- navigates into a child directory called
cd ..- navigates into the parent of the current directory
..is shorthand for parent
cdorcd ~will take you back home
Now that we can navigate the file system, let's learn how to create, modify, and delete files and folders.
- Makes a directory (folder) in a location relative to the current directory
- Example:
mkdir directory_name- makes a directory called 'directory_name'`
- creates an empty file in a location relative to the current directory
- A file typically will have a file extension like
.txtwhereas a directory will not. - Example:
touch file.txt- makes a .txt file
- Writes the contents of a file to the console
- This is a quick method to check the contents of a file without opening it in an editor
- Example:
cat readme.md- writes the contents of readme.md to the console
- Deletes a file with no confirmation
- There is no
trashto recover removed files from so use with caution.
- Deletes a directory. Similarly to
rm, no confirmation or trash to recover from, so proceed cautiously.
This command deletes files and folders recursively and by force. It deletes all the contents of folders and can be dangerous, so use cautiously!
sudo -- or "super user do" -- runs the command that follows as the super user
(i.e., 'root' or 'admin'). That means your computer will not prevent you from
running the command and may not even confirm if this is what you actually want
to do. This is of particular concern when the command may have destructive
effects.
Generally, you shouldn't need to run commands with
sudoin this course. If you're not sure, ask an instructor.
In the rm -rf command, the letters r and f are flags. Flags are extra
options you can optionally add to commands. They modify the behavior of the
command.
In the rm -rf example, there are two flags
-r: recursive, delete all nested files and directories-f: force, ignore permissions and do not ask for confirmation
Each command has a different set of flags and arguments it accepts. We can easily view this information without leaving our terminal environment using a command.
The man command, short for manual, allows us to view a manual page for any
installed command.
For example, you can run man rm to view the list of flags that the rm
command accepts.
Since man is also a command, you can even run man man to view more details
about the man command.
In the long term, reduce your reliance on the mouse. Some CLI keyboard shortcuts:
-
⌘KClear the Terminal window -
option arrowMove cursor by word -
letter[TAB]
- autocompletes (case-sensitive)
-
up / down arrow
- cycle command history
| Command | Action |
|---|---|
| pwd | print working directory |
| ls | list content of current directory |
| ls -a | list content of current directory, including hidden files |
| ls -l | list content of current directory with extra details |
| man | manual |
| cd child-directory | change directory |
| cd .. | parent directory |
| cd ~ | home directory |
| mkdir [name] | create a directory |
| touch [name.ext] | create a file - will not overwrite a file if it exists |
| open [name.ext] | opens file |
| clear | empties terminal screen |
| cat [name.ext] | displays content of file in bash (terminal) |
| mv [-options] [path/to/file] [path/to/directory] | move file to specified location |
| mv [-options] [path/to/file] [new name] | renames file or directory |
| cp [path/to/file] [path/to/directory] | copies files to specified directory or file name |
| cp -r [path/to/file] [path/to/directory] | copies files and folders to specified directory or file name |
| rm [path/to/file] | removes file PERMANENTLY! |
| rm -r [path/to/directory] | removes directory PERMANENTLY! |
| rmdir | removes empty directory PERMANENTLY! |
| [name of program] [arguments] | start a program (example: atom [path/to/file]), node [path/to/file] ...) |
| Ctrl + C | abort a program that is currently active in the terminal |
| Ctrl + A | jump to beginning of line |
| Ctrl + E | jump to end of line |
| Ctrl + K | clear the screen |
| tab | autocomplete |
| arrow up | fill line with previously entered command |
| arrow left/right | jump to next/previous word |
| q | end view of text in terminal (with some programs/views) |
| history | see a list of previously typed commands |
| Command | Action |
|---|---|
| git status | get current status for repo |
| git init | initialize a current folder as a repo, i.e. start tracking changes |
| git clone [link] | makes copied remote repo a new local repo |
| git add [path/to/file] | stage file |
| git commit -m ["message"] | commit changes and include a message describing commit |
| git log | display timeline |
| git diff [path/to/file] | show tracked but unstaged changes |
| git push origin master | send changes to remote repository (push to gitHub) |
| git pull upstream master | get latest changes from a remote repository |
| Command | Action |
|---|---|
| npm init | initialize a directory with package.json file for saving dependencies |
| npm install [package-name] | install a package |
| npm i [package-name] | install a package |
| npm install [package-name] --save | install a package and keep a record of it in package.json . --save is not needed/the default in versions 5.0.0 of NPM and up |
| npm install [package-name] -g | install a package as a global terminal command. You may need your administer password/ us sudo to compete a global install |
| npm install | read package.json and install all listed dependencies |
To get your hands dirty, use the commands above to build out the following folders and files. This will be where you store all your work from SEI.
sei
├── Unit1
│ └── Week1
│ └── Monday
├── Unit2
├── Unit3
├── Unit3
|-- projects
└── sandboxPro Tip: Use brackets to substitute paths together. Lets say you want to
create a markdown file (extension .md) for a couple of lessons. One way to do
this would be to run the commands one-by-one:
touch unit1/cli_intro.md
touch unit1/git_intro.md
touch unit1/html.md
touch unit1/css.mdThat would absolutely work, but you could make do it in one command:
touch lessons/cli_intro.md lessons/git_intro.md lessons/html.md lessons/css.mdThat would also work, but we can make it a more succinct command by using substitution:
touch lessons/{cli_intro,git_intro,html,css}.mdThe above command will create the same set of files, but in one short command. It also works with directories! The list of file or directories must be separated by commas with no spaces.
There are MANY more CLI commands to learn, more than we could cover in an entire day. This is intentional and will help grow your meta-developer skills -- belief in your ability to learn new things and teach yourself, as well as improving your ability to Google and sift through resources like MDN, W3 Schools, and Stack Overflow.
The commands we've covered in this lesson will probably account for 80% of your CLI usage. Regardless of how much experience with the command line you have coming in to this class, your next step should be to get really comfortable with it - we're going to spend a lot of time in the command line over the next 12 weeks.
Reduce your reliance on the mouse and get in the habit of using these keyboard shortcuts:
⌘ K Clear the Terminal window
option arrow Move cursor by word
Ctrl A Go to beginning of line
Ctrl E Go to end of line
Ctrl K Kill line to end
Ctrl U Kill line to beginning
Ctrl Y Paste whatever was killed using Ctrl+K or Ctrl+U
cd - toggle previous directory
There are also video tutorials here.




