What the What?! — A quick list of Terminal Commands
Working through the first week of the Andela Bootcamp has been like trying to swallow 3 different language dictionaries a day. We work through assignments using Python, create versions of them using Git, then test them on Terminal (or Command Prompt depending on your computer).
So much language, so different syntax! So much What the What!!?!?!
To avoid drowning in Google searches, I’ve put together a quick reference guide to the basic Terminal commands we’ve been using. I’ll update the list once a week as I continue to learn more. I hope this will be useful to other beginners! Let me know if you see any errors or have other commands that should be added here. Thanks!
What is a Terminal?
The Terminal app (on Unix-based computers) or Command Prompt (on Windows) might seem like a dark, scary portal used by hoodie-wearing evil hackers to infiltrate the Matrix or whatever, but it’s actually not as alien as it looks once you get to know it.
The terminal’s signature black box with green letters is basically an interface that allows the user to enter text-based commands to the computer. The computer receives these commands, runs them, and returns an output back to the user. It’s a faster, more direct way of communicating with your computer. Open terminal or command prompt on your computer and play around with the commands below to get a feel for what they do.
Basic Terminal Commands
Hint: developers use the term “directory” a lot…from my understanding it’s basically a fancy way of saying folder.
- $ pwd: stands for present working directory, allows you to see which folder you’re currently working in.
- $ cd: change directory — let’s you enter a directory e.g:
$ cd ~/desktop
- $ cd ~ takes you back to the Home folder. While you could just type “cd” by itself to go back to the Home folder, adding the tilde “~” sign (shorthand for “current”) will ensure you go back to the most current session.
- $ cd .. takes you to the folder above the one you’re currently in.
- $ cd - takes you to back to the directory you were in before the last time you issued the cd command.
- $ ls: list — shows the contents of the current folder
- $ ls -a: reveals hidden files
- $ mkdir: make a new directory (folder) e.g:
$ mkdir my_folder
- $ touch: make a new file e.g:
$ touch my_file.py
- You can enter a folder and create new sub-folders and files in one command e.g.:
$ cd desktop/; mkdir MyFolder; cd MyFolder; touch my_file.py
# enter desktop folder, create MyFolder, enter MyFolder, create my_file.py
- $ open: to open a file e.g:
$ open ~/Desktop/my_file.py
# you will need to include the full path and name of the file/folder you want to open.
Rather than typing out the full path name, an easier way to get the file path is to drag and drop the actual file into the terminal. The terminal will then autocomplete the file path and file name.
- To open multiple files simultaneously, put a space between the different file names e.g:
$ open ~/Desktop/my_file.py other_file.py
- To open a file using a specific application e.g.:
$ open -a “Sublime Text.app” ~/Desktop/my_file.py
- $ rm -Rf: to delete a file. “rm” means remove, “R” is a recursive flag telling the computer to delete all the contents, “f” means force so only use it if you’re very sure it won’t delete stuff you may need. If unsure, you can leave it out. E.g:
$ rm -Rf /Users/Rehema/Desktop/MyFile.py
- $ cp: to copy a file or folder e.g:
$ cp ~/Desktop/my_file.py ~/Documents
# copy the file from it's current location (Desktop) to it's new location (Documents).
$ cp -R ~/Desktop/MyFolder /Documents
# copy the folder. The R flag tells it to copy recursively i.e. each subfolder and subfile found within the main folder you’re copying.
- $ mv: to move a file or folder e.g:
$ mv ~Desktop/my_file.py ~/Documents
$ mv ~/Desktop/MyFolder /Documents
- To copy or move multiple documents in one go:
$ cp ~/Desktop/*.py ~/Documents
# this will move all .py files on your desktop to your documents folder.
- Renaming files: you can move a file to the same location but change its name e.g:
$ mv ~/Desktop/my_file.py ~/Desktop/my_file1.py
- $ clear: clears all the text on the terminal so that you have a clean sheet to work with, like turning the page on a book. You can still see all your previous work simply by scrolling up.
- To run a program in terminal:
$ python my_file.py
- When using the unittest library to conduct tests in the terminal:
# to see what’s inside unittest and help you know the name of the function you’re looking for.
from unittest import TestCase
# to find out how to use the assertRaises function.
- To find out what actions can be carried out on a data type:
# if you have a string e.g: str = ‘James’
# will list all the actions applicable to strings.
- To exit help: shift + zz
- To exit everything and go back to scratch: ctrl + z or q depending on which application you’re in.
That’s it for now. Let me know your feedback/suggestions in the comments below. If you found this useful I’ll write one up for basic Git commands as well, thanks!