It’s good to get a feel for working in the command line. I know it can feel intimidating at the beginning, but it’s really just another way of interacting with your computer. Running a command in the command line is analogous to double-clicking an icon, for example, in many cases.
With the command line open — the Terminal application on a Mac and Git Bash in Windows — you can use simple text-based commands to interact with your computer. To run a command, you simply enter it and hit Enter.
Your home base, the home folder
Typically, when you open your command line application, you start, by default, in your home directory. And you’ll usually be storing your various code files somewhere within this home directory.
On a Mac, the full path of your home folder is:
/Users/<your-computer-name>
In Windows, the path is:
C:/Users/<your-computer-name>
Here are some common commands
When command line commands are listed, they’re typically prefixed with a $
. When you’re executing the commands, you do not enter that $
; you only enter everything after it.
The cd
command
cd
stands for change directory. This command lets you move around to/from different folders on your computer. Every command you executed is executed from within the directory you’re currently located.
You can use the following to move into a folder/directory that’s within your current directory:
$ cd <directory-name>
For example, if you’re in your home directory, you can run the following to move into your Documents
folder:
$ cd Documents
You can also supply a full path:
$ cd <path>
So you could, for example, move to your home directory (the path below is for a Mac) using:
$ cd /Users/<your-computer-name>
Conveniently, you can simply use ~
instead of typing out the whole path to your home directory. So you can move to home directory just by typing:
$ cd ~
Another shortcut: you can use ..
to move up a directory (i.e. into the parent directory):
$ cd ..
The ls
command
This command is used to list the current files and folders within the current directory. Just using the command itself will do so:
$ ls
You can frequently include options for commands by following the command with a dash, -
and certain characters corresponding to various available options.
If you include the -l
option, the files/folders will be displayed in list-format:
$ ls -l
The mkdir
command
This command can be used make a directory within your current directory:
$ mkdir <new-folder-name>
So, for example, to create a new folder named “test” in your current directory, you would run:
$ mkdir test
Play around a bit
The best way to get a feel for things is to work with them and play around.
If you stick to the few commands above, there’s no way you can harm anything, because, if you think about it, all you can possibly do is:
- View the contents of a folder (with
ls
) - Move in and out different folders (with
cd
) - And make new folders (with
mkdir
)
So you don’t have to worry about deleting things or anything else scary!
Try out these exercises
Using just ls
, cd
, and mkdir
, perform the following exercises (note that for most of them you’ll have to use more than one command!):
- Go into your
Downloads
folder and create a new directory named “test”; then move back up to yourDownloads
folder using the shortest command possible. - Go back to your home directory, move into your
Documents
folder, and list all of the files, in list format. - Move into your
Downloads/test
folder using a single command; then move back to your home directory using a single command that’s notcd ~
. (Hint: you can combine multiple..
in a single command.)