Command Line for Beginners – How to Use the Terminal Like a Pro [Full Handbook]
Command Line for Beginners – How to Use the Terminal Like a Pro [Full Handbook]
Let's go! =D
Table of
Contents
Difference
between
console,
terminal,
command line
(CLI) and Shell
Console
Termina
l
Shell
Comma
nd line
(CLI)
:
(CLI)
Why should I
even care
about using the
terminal?
Different kinds
of shells
A bit of
history -
Posix
How do
I know
what
shell I'm
running
?
What
shell is
better?
A
c
o
m
m
e
:
n
t
a
b
o
u
ADVERTISEMENT
t
c
u
s
t
o
m
i
z
a
ti
o
n
Most common
and useful
commands to
use
Git
comman
:
comman
ds
Round up
Difference
between
console,
command
line (CLI),
terminal
and Shell
I think a good place to
start is to know
exactly what the
command line is.
console, command
:
console, command
line, CLI, and shell.
People often use these
words interchangeably
but the truth is they're
actually different
things.
Differentiating each
isn't necesarilly crucial
knwoledge to have,
but it will help clarify
things. So lets briefly
explain each one.
Console:
The console is the
physical device that
allows you to interact
with the computer.
ADVERTISEMENT
Terminal:
A terminal is a text
input and output
environment. It is a
program that acts as a
wrapper and allows us
to enter commands
that the computer
processes.
Shell:
A shell is a program
that acts as command-
line interpreter. It
processes commands
and outputs the
results. It interprets
and processes the
commands entered by
the user.
Command
line or CLI
(command
line
interface):
The CLI is the
interface in which we
enter commands for
the computer to
process. In plain
English once again, it's
the space in which you
enter the commands
the computer will
process.
:
process.
Why
should I
even care
about
using the
terminal?
:
We just mentioned
that most operating
systems come with a
GUI. So if we can see
things on the screen
and click around to do
whatever we want,
you might wonder why
you should learn this
complicated
terminal/cli/shell
thing?
So being comfortable
with the CLI will allow
you to interact with
computers on all
ocassions.
Different
kinds of
shells
Before diving into the
actual commands you
A bit of
history –
Posix
For shells, there's a
common standard
called Posix.
How do I
know what
shell I'm
running?
To know what shell
you're currently
:
you're currently
running, just open
your terminal and
enter echo $0 . This
will print the current
running program
name, which in this
case is the actual shell.
What shell is
better?
There's not A LOT of
difference between
most shells. Since most
of them comply with
the same standard,
you'll find that most of
them work similarly.
As mentioned,
Bash is the
most widely
used and comes
installed by
default on Mac
and Linux.
Zsh is very
similar to Bash,
but it was
created after it
and comes with
some nice
improvements
over it. If you'd
like to have
more detail
about its
differences,
here's a cool
article about it.
Fish is another
commonly used
shell that
:
shell that
comes with
some nice built-
in features and
configurations
such as ADVERTISEMENT
autocompletio
n and syntax
highlighting.
The thing about
Fish is that it's
not Posix
complaint,
while Bash and
Zsh are. This
means that
some of the
commands
you'll be able to
run on Bash
and Zsh won't
run on Fish and
viceversa. This
makes Fish
scripting less
compatible
:
compatible
with most
computers
compared to
Bash and Zsh.
features than
:
features than
what Posix
requires.
So a common practice
is to use this
"enhanced" shells like
Bash or Zsh for
general interaction,
and a "stripped" shell
like Ash or Dash to
execute scripts.
When we get to
scripting later on, we'll
see how we can define
what shell will execute
a given script.
:
If you're interested in
a more detailed
comparison between
these shells, here's a
video that explains it
really well:
If had to recommend a
shell, I would
ADVERTISEMENT
recommend bash as
it's the most standard
and commonly-used
one. This means you'll
be able to translate
your knowledge into
most environments.
A comment
:
A comment
about
customization
I just mentioned that
Fish comes with built-
in configuration such
as autocompletion and
syntax highlighting.
This come built-in in
Fish, but in Bash or
Zsh you can configure
these features, too.
We won't see
customization options
in detail here, but
know that when you
:
know that when you
install a shell in your
computer, certain files
will be created on your
system. Later on you
can edit those files to
customize your
program.
These customization
options are also true
for Terminals.
Most
common
and useful
commands
to use
:
to use
Now that we have a
foundation of how the
CLI works, let's dive
into the most useful
commands you can
start to use for your
daily tasks.
Echo prints in
the terminal
whatever
parameter we
pass it.
:
echo Hello freeCodeCamp! // Output: Hello freeCodeCamp!
ls presents you
the contents of
the directory
you're
currently in. It
will present
you with both
the files and
other
directories
:
directories
your current
directory
contains.
ls // Output:
node_modules package.json package-lock.json public README.md src
files or directories.
Like .git or
.gitignore files
ls -a // Output:
. .env .gitignore package.json public src
.. .git node_modules package-lock.json README.md
:
cd is short for
Change
directory and it
will take you
from your
current
directory to
another.
While on my home
directory, I can enter
cd Desktop and it will
take me to the
Desktop Directory.
If I want to go up one
directory, meaning go
to the directory that
contains the current
directory, I can enter
cd ..
mkdir stands
for make
directory and it
will create a
new directory
for you. You
have to pass
the command
the directory
name
parameter.
If I wanted to create a
new directory called
"Test" I would enter
mkdir test .
rmdir stands
for Remove
directory and it
does just that.
It needs the
directory name
parameter just
:
parameter just
as mkdir :
rmdir test .
touch allows
you to create
an empty file in
your current
directory. As
parameters it
takes the file
name, like
touch
test.txt .
rm allows you
to delete files,
in the same
way rmdir
allows you to
remove
directories. rm
test.txt
ADVERTISEMENT
cp allows you
to copy files or
directories.
This command
takes two
:
takes two
parameters:
the first one is
the file or
directory you
want to copy,
and the second
one is the
destination of
your copy
(where do you
want to copy
your
file/directory
to).
If I want to make a
copy of my txt file in
the same directory, I
can enter the
following:
cp test.txt testCopy.txt
cp test.txt ./testFolder/
cp test.txt ./testFolder/testCopy.txt
mv is short for
move, and lets
:
move, and lets
us move a file
or directory
from one place
to another.
That is, create
it in a new
directory and
delete it in the
previous one
(same as you
could do by
cutting and
pasting).
mv test.txt ./testFolder/
mv test.txt ./testFolder/testCopy.txt
head allows
you to view the
beginning of a
file or piped
data directly
from the
terminal.
cd --help // output:
cd: cd [-L|[-P [-e]] [-@]] [dir]
Change the shell working directory.
In a similar way,
the man
command will
return info
about any
particular
command.
man cp // output:
NAME
cp - copy files and directories
SYNOPSIS
:
cp [OPTION]... [-T] SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...
DESCRIPTION
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
ADVERTISEMENT
Mandatory arguments to long options are mandatory for short
too.
-a, --archive
same as -dR --preserve=all
--attributes-only
don't copy the file data, just the attributes
...
on Mac or
Linux will likely
be either Nano
or Vim).
:
If you open your file
and then can't exit
your editor, first look
at this meme:
ctrl+c allows
you to exit the
current process ADVERTISEMENT
the terminal is
running. For
example, if
you're creating
a react app
with npx
create-react-
app and want
to cancel the
build at some
point, just hit
ctrl+c and it
will stop.
Copying text
from the
terminal can be
done with
ctrl+shift+c
:
ctrl+shift+c
and pasting can
be done with
ctrl+shift+v
By pressing up
and down keys
you can
navigate
through the
previous
commands you
entered.
:
entered.
By hitting tab
you will get
autocompletio
n based on the
text you've
written so far.
By hitting tab
twice you'll get
suggestions
based on the
text you've
written so far.
autocompletes to edit
test.txt
Git
:
Git
commands
Besides working
around the file system
and
installing/uninstalling
things, interacting
with Git and online
repos is probably the
most common things
you're going to use the
terminal for as a
ADVERTISEMENT
developer.
git commit
commits your
changes to the
repository.
Commits must
always be must
be
accompanied
by the -m flag
and commit
:
and commit
message.
ADVERTISEMENT
Keep in mind
you need to
create your
remote repo first
in order to get its
URL. We'll see
how you can do
this from the
:
command line
with a little
script later on. ;)
git remote -v
lets you list the
current remote
repository
you're using.
git push
uploads your
commited
changes to your
remote repo.
git checkout
moves you
from one
:
from one
branch to
another. It
takes your
destination
branch as
paremeter.
particularly
useful when
working in
teams, when
many
developers are
:
developers are
working on the
same code
base. In this
case each
developer
periodically
pulls from the
remote repo in
order to work
in a code base
that includes
the changes
done by all the
other devs.
As a side comment,
when comparing
differences between
branches or repos,
git merge
merges
(combines) the
branch you're
currently in
with another.
Keep in mind
the changes
will be
incorporated
only to the
branch you're
currently in,
not to the other
one.
commit f15cf515dd3ec398210108dce092debf26ff9e12
Author: German Cocca <[email protected]>
...
same way it
works with
bash.
:
git diff --help // output:
GIT-DIFF(1) Git Manual GIT-DIFF
NAME
git-diff - Show changes between commits, commit and working tree, et
SYNOPSIS
git diff [options] [<commit>] [--] [<path>...]
git diff [options] --cached [<commit>] [--] [<path>...]
...
Our first
script
Now we're ready to
get to the truly fun and
awesome part of the
command line,
scripting!
As I mentioned
previously, a script is
nothing more than a
series of commands or
instructions that we
can execute at any
given time. To explain
:
given time. To explain
how we can code one,
we'll use a simple
ADVERTISEMENT
example that will allow
us to create a github
repo by running a
single command. ;)
First thing to
do is create a
.sh file. You
can put it
wherever want.
I called mine
newGhRepo.sh .
Then open it on
your text/code
editor of
choice.
On our first
line, we'll write
the following:
#! /bin/sh
This is called a
:
This is called a
shebang, and its
function is to declare
what shell is going to
run this script.
Remember previously
when we mentioned
that we can use a given
shell for general
interaction and
another given shell for
executing a script?
Well, the shebang is
the instruction that
dictates what shell
runs the script.
As mentioned too,
we're using a "stripped
down" shell (also
known as sh shells) to
run the scripts as
they're more efficient
(though the difference
might be unnoticeable
to be honest, It's just a
:
to be honest, It's just a
personal preference).
In my computer I have
dash as my sh shell.
If we wanted this
script to run with bash
the shebang would be
#! /bin/bash
A parameter is a set of
characters that is
entered after the
script/comand. Like
:
script/comand. Like
with the cd command,
we need to specify a
directory parameter in
order to change
directory (ie: cd
testFolder ).
ADVERTISEMENT
paramOne=$1
paramTwo=$2
paramThree=$3
...
So we're
expecting the
repository
:
repository
name as
parameter of
our script. But
what happens if
the user
forgets to enter
it? We need to
plan for that so
next we're
going to code a
conditional
that keeps
asking the user
to enter the
repo name until
that parameter
is received.
while [ -z "$repoName" ]
do
echo 'Provide a repository name'
read -r -p $'Repository name:' repoName
done
:
What we're doing here
is:
1. While the
repoName
variable is not
assigned
( while [ -z
"$repoName" ] )
2. Write to the
console this
message ( echo
'Provide a
repository
name' )
3. Then read
whatever input
the user
provides and
assign the input
to the
repoName
variable ( read
-r -p
:
-r -p
$'Repository
name:'
repoName )
Now that we
have our repo
name in place,
we can create
our local Git
repo like this:
ADVERTISEMENT
This is creating a
readme file and
writting a single line
with the repo name
( echo "# $repoName"
curl -u coccagerman
https://api.github.com/user/repos
-d '{"name": "'"$repoName"'",
"private":false}'
curl is a command to
transfer data from or
to a server, using one
of the many supported
protocols.
After running
this command,
GitHub will
prompt us to
enter our
private token
for
authentication.
<process1> |
<process2> .
And as last
step, we
rename our
master branch
to main, add
the remote
origin we just
obtained, and
push our code
to GitHub! =D
#! /bin/sh
repoName=$1
while [ -z "$repoName" ]
do
echo 'Provide a repository name'
read -r -p $'Repository name:' repoName
done
I have a
.bash_aliases file on
my system, so let's edit
that.
In our CLI we
enter cd to go
over home
directory.
Then we can
enter ls -a to
list all files
(includen
hidden ones)
and check if we
have either a
.bashrc or
.bash_aliases
file in our
system.
We open the
:
We open the
file with our
text/code
editor of
choice.
And we write
our new alias
like this: alias
newghrepo="dash
/home/German/Desktop/ger/code/projects/scripts/newGhRepo.sh
After we're
done editing,
we save our
file, restart our
terminal, and
voilà! Now we
can run our
script by just
entering
newghrepo , no
matter in what
directory we
currently are.
Much quicker
:
Much quicker
than opening
the browser
and clicking
around to
create our
repo! =D
Round up
The terminal can feel
like an intimidating
and intricate place
:
and intricate place
when you're starting
out. But it's certainly
worth it to put time
and effort into
learning the ins and
outs of it. The
efficiency benefits are
too good to pass up!
If you're
interested in
learning more
about the
terminal and
Bash, Zach
Gollwitzer has
an awesome
crash course
series on
youtube. He has
also great
tutorials on
other topics
such as Node
and Javascript,
:
so I recommend
that you follow
him. ;)
learned something
new. If you want, you
can also follow me on
linkedin or twitter.
ADVERTISEMENT
Our mission: to help people learn to code for free. We accomplish this by creating
thousands of videos, articles, and interactive coding lessons - all freely available to the
:
thousands of videos, articles, and interactive coding lessons - all freely available to the
public.
Donations to freeCodeCamp go toward our education initiatives, and help pay for
servers, services, and staff.
Mobile App
:
Our Charity
About Alumni Network Open Source Shop Support Sponsors Academic Honesty