Running a command on the terminal typically looks something like this:
name-of-command --options input-file-or-text output
name-of-command is the actual command. Options are often preceeded by two dashes, or they can likely be shorted to one dash and the first letter or an abbreviation. Then, there will occasionally be some kind of input text or file that the command is acting on, or changing. Similarly, you might specify a filename for the output of the command. You’ll see that many of the commands below are more simple.
cd on its own, or
cd ~ will take you to your home directory.
Create a directory:
Show the directory you’re currently in:
List the files in the directory:
List the files in a more readable way, with useful information like permissions included:
Open a file with its default application:
Open current directory in the finder:
or open some other directory:
Create an empty file if it doesn’t already exist:
Open and edit a file with the simple nano editor:
Move a file or directory:
mv file new/path/to/file
Rename a file or directory:
mv file new-file-name
Copy a file:
cp file name-of-file-copy
Copy a directory
cp -R directory directory-copy
-R option allows for recursive copying of files inside the directory.
Delete a file:
Delete a directory and its contents:
rm -rf path/to/directory
Let’s dissect this command:
rm is for deleting things.
-rf means that files will be recursively deleted, and the deletion will be forced.
r is for recursive,
f is for forced.
Never do this:
rm -rf /
Be very careful with the rm command. You can easily delete things on accident.
This command is deleting with the
rm command, recursively forcing the deletion of files and folders with
-rf, and we’ve passed the root directory,
/ as the thing to delete. This means it will delete everything on your hard drive. Some operating systems protect against this mistake, and if you’re not the root user you would need to prefix this command with
sudo to make it work. Be careful, and be nice.
Clear the terminal screen of previous activity:
Reset the terminal
Stop a running process
If a process is running in the terminal and you need to stop it, press the
control key and the
C key at the same time.
Run multiple commands on one line
&& you can chain together multiple commands that execute one after the other. This example creates a directory, then moves you into that new directory:
mkdir new-folder && cd new-folder
Aliases and environment variables
Aliases allow you to create abbreviated commands that alias long, complex, or regularly used commands.
Here is an example:
alias l="ls -al"
The above aliases the
ls -al command to a shortened
To create an alias you will open the .bashrc file in your home folder.
Open the .bashrc file with nano:
Add the following alias to the bottom of the file:
alias pizza="echo 'pizza is awesome!'"
Save the file by pressing
control + O.
Exit nano by pressing
control + x.
Environment variables represent values that are useful across for processes running on your computer.
Reading an environment variable:
In the terminal, run the following:
If you’re logged into a vagrant machine, you’ll see output like this:
This is your home folder, also known as your user folder.
On a Mac you’ll see output like this:
Setting an environment variable
In the terminal, set an environment variable like this:
Now, you can read the variable the same as we did before:
If you close or reset your terminal session, you’ll lose this temporary variable. To save an environment variable so it can be accessed in all your sessions, we’ll place the definition of the variable in the ~/.bashrc file.
Open the ~/.bashrc file:
Add the following to the bottom of the ~/.bashrc file:
export PIZZA="ooooooh, pizza"
Source the .bashrc file:
Now, you can close the terminal window, open a new one, and run the following command:
And you’ll still see the following output: