This page intentionally left blank. ⬇️, ➡️, or spacebar 🛰 to start slidedeck. --- class: middle, center .center[![hacking](/img/hacking.gif)] # Command Line Interface --- # What is Command Line Interface? A **terminal window** is a text-only window in a **graphical user interface** (GUI) that emulates a console. A GUI is a type of human-computer interface (i.e., a system for people to interact with a computer) that uses windows, icons, pull-down menus and a pointer and that can be manipulated by a mouse (and usually to some extent by a keyboard as well). A **command** is an instruction telling a computer to do something, such as launch a program. A **command line interface** (CLI) is an all-text display mode that has a command interpreter running in it and that shows commands, including their arguments, and other data as they are typed in at the keyboard as well as the results of most such commands. A command interpreter, also referred to as a **shell**, is a program whose primary function is to read commands that are typed into a console or terminal window and then execute (i.e., run) them. [source](http://www.linfo.org/terminal_window.html) --- # Why learn this? When working through commands, it may seem strange to learn how to use the command line when you can do everything using the desktop. But here are a few reasons why CLI skills are good to know: 1. You can use command-line tools that have no GUI equivalent. 2. You can build automated tools and run them (and you can't do that in a GUI, either) so the computer can do your job for you. 3. You may have to access computers over a network/internet that either do not have a GUI, or you cannot see them. For example, if you need to talk to a server on your network, you might not be able to get a visual representation of the files on that server and you will have to communicate with this computer "in the dark" using the command line. --- # Access on macOS: Terminal For macOS you'll need to do this: * Hold down the command key and hit the spacebar. * Spotlight Search should appear. * Type: terminal * Click on the Terminal application. It looks like a black box with `>_` inside. Terminal should now be open. --- # Access on Windows: Command Prompt or PowerShell * Click Start. * In "Search programs and files" type: `cmd` (or `powershell`) * Hit Enter. [Command Prompt](https://en.wikipedia.org/wiki/Cmd.exe) The default command line shell installed in Windows. [PowerShell](https://en.wikipedia.org/wiki/PowerShell) PowerShell is a command line shell, scripting language, and task automation program native in Windows. --- # Access on Linux: Terminal For Linux you'll need to do this: * Hit at the same time the following keys: `Control` + `Alt` + `t`. Terminal should now be open. --- # Other options [Running Linux on Windows10](https://docs.microsoft.com/en-us/windows/wsl/install-win10) [gow](https://github.com/bmatzelle/gow) gow is short for Gnu On Windows and installing it will give you access to common UNIX commands, right from the Command Prompt! Recommended. [Cygwin](http://cygwin.com/) Cygwin is a "Linux-like environment" for Windows. [Here](https://www.pcwdld.com/cygwin-cheat-sheet) is a cheat sheet for getting started. Mac: [iTerm2](https://iterm2.com/) Another option instead of the Terminal with some bonus configuration features. --- # A note about directories ### Directory is another word for what the GUI represents as a "folder." These phrases may be used interchangably. .right[![](/img/old-laptop.jpg)] --- # A note about getting stuck ### **Command is taking too long?** ctrl+c (wrap up and stop doing this) or ctrl+z (stop doing this *now*) ### **Stuck in a text file?** `:q!` ### **Stuck, confused, and panicking?** close the window ??? `vi`, a text editor that lives inside command line programs, used for quickly editing or reading files. If some documentation is very big, it might overwhelm the screen by opening `vi` -- the only way out is `:q` for "quit". Sometimes you need to use an exclamation point to show that you really mean it... `:q!`. --- # A note about drag-and-drop ### When using the command line on your computer, you can "drag and drop" anything from a folder into the terminal and work with the file there. --- class: middle, center # Let's practice some commands! .right[![](/img/mac-portrait.jpg)] --- # Show current directory ## Mac/Linux: `pwd` ## Windows: `ch` or `chdir` --- # Change current directory ## Mac/Linux: `cd` Type/the-path-you-want/here ## Windows: `cd` Type\the-path-you-want\here It's important to note whether your path starts with a slash or not. Bonus: `..` will take you "up" one level. ??? Whether the path you are typing in has a slash in front of it or not is significant -- it determines whether or not the computer should look for that path within the context of the folder you are in, or within the context of your entire machine. The slash in front lets the computer know you are setting your path from anywhere, and without the slash, it is within the context of the folder you are in. If you are on the Desktop and want to go to a folder on the desktop, you can just type the name of that folder OR you can drag the folder into the CLI, which will show you the FULL path (and will work no matter what folder you are in). --- # List **this** directory's contents ## Mac/Linux: `ls` ## Windows: `dir` --- # List **that** directory's contents ## Mac/Linux: `ls Type/the-path-you-want/here` ## Windows: `dir Type\the-path-you-want\here` Hint: You can drag and drop a folder into the CLI window. --- # See all files, subfolder and files in subfolders ## Windows: `tree /f` This is natively only available in Windows, but Mac users can download and install `tree`. If Mac users have Homebrew installed, they can use `brew install tree` --- # Create a directory ## Mac/Linux: `mkdir name-of-directory` ## Windows: `mkdir name-of-directory` --- # Create a file ## Mac/Linux: `touch example.txt` ## Windows: `type nul > example.txt` ??? This works well when making things like textfiles, because your operating system is smart enough to know that you want to make a textfile just by adding the .txt extension. Your computer is not smart enough to know that you want a video file if you try to make a video in this way. --- # Open a file ## Mac/Linux: `open name-of-file` ## Windows: `name-of-file` --- # Copy a file ## Mac/Linux: ## `cp name-of-file.txt name-of-copy.txt` ## Windows: ## `copy name-of-file.txt name-of-copy.txt` --- # Move or rename a file ## Mac/Linux: ## `mv name-of-file.txt new-name.txt` ## Windows: ## `move name-of-file.txt new-name.txt` ## `ren name-of-file.txt new-name.txt` --- # A note about deleting ### ⚠️ ⚠️ ⚠️ It's dangerous! ⚠️ ⚠️ ⚠️ ### When you delete something on the command line, it deletes it instantly and forever. It does not put it in the Recycle Bin or Trash. --- # Removing empty directories ## Mac/Linux: `rmdir name-of-directory` ## Windows: `rmdir name-of-directory` If you try to remove a non-empty directory with `rmdir`, you will get an error. --- # Removing files ## Mac/Linux: `rm -i name-of-file.txt` ## Windows: `del name-of-file.txt` Mac/Linux users: The `-i` flag is optional but it helps prevent accidental deletion -- it will prompt you to confirm (type `y` or `n`). It will read the first letter, so any time of affirmative-sounding word in English will work. Note that typing "yikes" will also work and delete the file. 😅 --- # Additional Resources - [awesome-shell](https://github.com/alebcay/awesome-shell) - [Command Line Cheat Sheet](https://www.git-tower.com/blog/posts/command-line-cheat-sheet) - [Command Prompt Cheat Sheet](http://simplyadvanced.net/blog/cheat-sheet-for-windows-command-prompt/) - [Cygwin Cheat Sheet](https://www.pcwdld.com/cygwin-cheat-sheet) - [Software Carpentry Course](http://swcarpentry.github.io/shell-novice/) - [Command Line Mystery Game](https://github.com/veltman/clmystery) - [Command Line Scavenger Hunt](https://digiprez.com/cli-scavenger-hunt) - [ExplainShell.com](https://explainshell.com/) - [Command Line Crash Course](https://learnpythonthehardway.org/book/appendixa.html) - [Script Ahoy](http://dd388.github.io/crals/) - [the sourcecaster](https://datapraxis.github.io/sourcecaster/) - [AMIA Digipres101 presentation](https://docs.google.com/presentation/d/1c3xhHbgpF3KQVwY9P_VpECZbwCTqwdQFh1DiBjWDue0/edit#slide=id.p) --- # Learning more - [Scripting (Bash)](/presentations/bash-scripting.html) - [Git101](/presentations/git.html) - [Fixity](/presentations/fixity.html) [Home](/)