Command Line
Contributors: Alanna Zhou
The Directory Structure of an Operating System
The OS (Operating System) on your computer organizes your files and folders in a hierarchy, all bundled up for each user. You may notice that you are the only user on your laptop, which is indicated by a folder that has the name of which you go by on your computer (in the diagram below, there happens to be four users) in some sort of Users
folder.
A directory is just a fancy word for folder, and files could mean anything with extensions like .zip
, .json
, .txt
, .mp3
, etc.
The command line comes in handy because it is a tool that allows you to jump in and out of directories (aka traversing this tree diagram), see what stuff is in them, and do cool things with just some commands you type!
Why Use Command Line?
It's Fast
It can help you navigate your computer in much faster ways than clicking around on your mousepad on the graphical user interface. We highly recommend you get familiar with this throughout the course!
It's Meant For Developing
For example, if you want to be able to develop software in a virtual environment (which you can think about as some mode that you activate that allows you to get access to libraries that are relevant to the software you are currently developing), you do it through the command line! But don't worry about this yet, let's get through the basics first...
The Basics of Command Line
Although this tutorial is done with MacOS commands, it's quite similar to those of Windows and Linux.
Traversing directories
As mentioned earlier, the file system on your laptop is organized in a tree-like hierarchy, with files nested within folders within folders, all bundled up in a big folder that corresponds to a specific user.
Let's assume that user1
is the root directory. This means that if you are using user1
on your laptop, when you open command line, you will automatically be in the directory user1
.
You can see what's in the directory that you are currently in with ls
(this is the Windows equivalent to dir
):
You can step further into directories (going down the tree in the diagram) with cd <folder name>
(folder name must be one of those that were listed when you typed ls
previously). Once you've entered D1
, you can then type ls
to see what's in D1
:
Oh, it's a text file named f1
! However, I'm not ready to see this file yet. Let's try stepping back up the tree with cd ..
(we just went from user1
to D1
, but let's go from D1
to user1
):
And of course, when I type ls
, we see that being back in user1
properly gives us the folder D1
that we saw when we first typed ls
!
And if I go further and further deeper down and go through more nested directories within user1
, because we established earlier that user1
is the root directory, I can always jump back to user1
easily with simply: cd
.
Looking at file contents
Now I'm ready to go back into D1
and see what that f1.txt
has with cat <file name>
:
Great! Looks like f1.txt
just contains a string yeetaki mushroomz
. Now I want to see it with my default text editor instead of on the command line, and I can do that with open <file name>
:
And a new window should open with the file opened:
MacOS & Linux Terminal
The Basics
look at what’s in the directory (aka folder) you are currently in:
ls
see the path that leads you to the directory you are currently in:
pwd
go into a directory:
cd <folder name>
step back out of a directory:
cd ..
jump to the root directory:
cd
Other helpful commands
make a directory:
mkdir <folder name>
delete a directory:
rm -rf <folder name>
delete a file:
rm <file name>
output contents of a file:
cat <file name>
to copy a file:
cp <name of file to be copied> <path you want the copy to be in>/<the name of the copy>
open Finder from where you are currently:
open .
open Visual Studio Code from where you are currently:
code .
Helpful tips to be faster
click on the up arrow to go through your history of commands
if you want to find a previous command and only know part of it (you don’t remember it completely), you can do:
ctrl + r
and start typing to see the most recent command that matches what you’ve typedto scroll through more suggestions, hold
ctrl
and hitr
for each suggestion
to get your cursor to the beginning of what you just typed:
ctrl + a
to get your cursor to the end of what you just typed:
ctrl + e
remember it as
e
for “end”
delete line of text where your cursor is:
ctrl + u
while you are in the middle of typing a folder name, or git branch name, you can press
tab
to autocompleteif there are multiple suggestions, you can go through each suggestion by pressing
tab
each time
Windows Command Prompt
The Basics
look at what’s in the directory (aka folder) you are currently in:
dir
see the path that leads you to the directory you are currently in:
pwd
go into a directory:
cd <folder name>
step back out of a directory:
cd ..
jump to the root directory:
cd
Other helpful commands
make a directory:
mkdir <folder name>
delete a directory:
rd /s /q "<path>"
delete a file:
rm <file name>
output contents of a file:
cat <file name>
to copy a file:
cp <name of file to be copied> <path you want the copy to be in>/<the name of the copy>
open Windows Explorer from where you are currently:
start .
open Visual Studio Code from where you are currently:
code .
Last updated