Command Line For Beginners
Command Line For Beginners
In this article I'll try my best to simply explain the parts that make up
the command line interface, and the basics of how it works, so you can
start using it for your daily tasks.
Let's go! =D
Table of Contents
• Difference between console, terminal, command line (CLI) and Shell
• Console
• Terminal
• Shell
• Command line (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 comment about customization
• Most common and useful commands to use
• Git commands
• Our first script
• 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.
When referring to this, you may have heard the terms Terminal,
console, command line, CLI, and shell. People often use these words
interchangeably but the truth is they're actually different things.
Console:
The console is the physical device that allows you to interact with the
computer.
In plain English, it's your computer screen, keyboard, and mouse. As a
user, you interact with your computer through your console.
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.
In plain English again, it's the "window" in which you enter the actual
commands your computer will process.
Keep in mind the terminal is a program, just like any other. And like
any program, you can install it and uninstall it as you please. It's also
possible to have many terminals installed in your computer and run
whichever you want whenever you want.
All operating systems come with a default terminal installed, but there
are many options out there to choose from, each with its own
functionalities and features.
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.
Same as the terminal, the shell is a program that comes by default in
all operating systems, but can also be installed and uninstalled by the
user.
In most Linux and Mac operating systems the default shell is Bash.
While on Windows it's Powershell. Some other common examples of
shells are Zsh and Fish.
Also keep in mind that the terminal is the program in which the shell
will run. But both programs are independent. That means, I can have
any shell run on any terminal. There's no dependance between both
programs in that sense.
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.
• The CLI, which takes commands as inputs in order for the computer
to execute tasks.
• The other is the GUI (graphical user interface), in which the user can
see things on the screen and click on them and the computer will
respond to those events by executing the corresponding task.
The first reason is that for many tasks, it's just more efficient. We'll
see some examples in a second, but there are many tasks where a GUI
would require many clicks around different windows. But on the CLI
these tasks can be executed with a single command.
In this sense, being comfortable with the command line will help you
save time and be able to execute your tasks quicker.
The third reason is that sometimes the CLI will be the only way in
which we'll be able to interact with a computer. Take, for example, the
case when you would need to interact with a cloud platform server. In
most of these cases, you won't have a GUI available, just a CLI to run
commands in.
So being comfortable with the CLI will allow you to interact with
computers on all ocassions.
The last reason is it looks cool and it's fun. You don't see movie
hackers clicking around their computers, right? ;)
This standard was stablished in the 1980's and most current shells
were developed according to that standard. That's why most shells
share similar syntax and similar features.
There are some slight differences you might want to know, though:
When we get to scripting later on, we'll see how we can define what
shell will execute a given script.
The point is that shells are customizable. You can edit how the
program works, what commands you have available, what information
your prompt shows, and more.
We won't see customization options in detail here, but 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.
Also, there are many plugins available online that allow you to
customize your shell in an easier way. You just install them and get
the features that plugin offers. Some examples
are OhMyZsh and Starship.
These customization options are also true for Terminals.
So not only do you have many shell and terminal options to choose
from – you also have many configuration options for each shell and
terminal.
If you're starting out, all this information can feel a bit overwhelming.
But just know that there are many options available, and each option
can be customized too. That's it.
ls // Output:
node_modules package.json package-lock.json public README.md src
If you pass this command the flag or paremter -a It will also show you
hidden 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 ..
If you enter cd alone, it will take you straight to your home directory.
• 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 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
• cp allows you to copy files or directories. This command 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
See that the directory doesn't change, as for "destination" I enter the
new name of the file.
If I wanted to copy the file into a diferent directory, but keep the same
file name, I can enter this:
cp test.txt ./testFolder/
And if I wanted to copy to a different folder changing the field name,
of course I can enter this:
cp test.txt ./testFolder/testCopy.txt
• mv is short for 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).
Again, this command takes two paremers, the file or directory we
want to move and the destination.
mv test.txt ./testFolder/
We can change the name of the file too in the same command if we
want to:
mv test.txt ./testFolder/testCopy.txt
• head allows you to view the beginning of a file or piped data directly
from the terminal.
head test.txt // Output:
this is the beginning of my test file
• tail works the same but it will show you the end of the file.
tail test.txt // Output:
The variable CDPATH defines the search path for the directory
containing DIR. Alternative directory names in CDPATH are
separated by a colon :.
A null directory name is the same as the current directory if DIR
begins with ....
• 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.
-a, --archive
same as -dR --preserve=all
--attributes-only
don't copy the file data, just the attributes
...
You can even enter man bash and that will return a huge manual about
everything there's to know about this shell. ;)
• code will open your default code editor. If you enter the command
alone, it just opens the editor with the latest file/directory you opened.
You can also open a given file by passing it as parameter: code
test.txt.
Or open a new file by passing the new file name: code
thisIsAJsFile.js.
• edit will open text files on your default command line text editor
(which if you're 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:

And then type :q! and hit enter.
The meme is funny because everyone struggles with CLI text editors
at first, as most actions (like exiting the editor) are done with keyboard
shortcuts. Using these editors is a whole other topic, so go look for
tutorials if you're interested in learning more. ;)
• ctrl+c allows you to exit the current process 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 and
pasting can be done with ctrl+shift+v
• clear will clear your terminal from all previous content.
• exit will close your terminal and (this is not a command but it's cool
too) ctrl+alt+t will open a new terminal for you.
• By pressing up and down keys you can navigate through the previous
commands you entered.
• By hitting tab you will get autocompletion 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.
For example if I write edit test and tab twice, I get testFolder/
test.txt. If I write edit test. and hit tab my text autocompletes
to edit test.txt
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 developer.
commit f15cf515dd3ec398210108dce092debf26ff9e12
Author: German Cocca <[email protected]>
...
• The --help flag will show you information about a given command,
exactly the same way it works with bash.
git diff --help // output:
GIT-DIFF(1) Git Manual GIT-
DIFF(1)
NAME
git-diff - Show changes between commits, commit and working tree, etc
SYNOPSIS
git diff [options] [<commit>] [--] [<path>...]
git diff [options] --cached [<commit>] [--] [<path>...]
...
• 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 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.
paramOne=$1
paramTwo=$2
paramThree=$3
...
• So we're expecting the 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.
We can do that like this:
while [ -z "$repoName" ]
do
echo 'Provide a repository name'
read -r -p $'Repository name:' repoName
done
What we're doing here is:
#! /bin/sh
repoName=$1
while [ -z "$repoName" ]
do
echo 'Provide a repository name'
read -r -p $'Repository name:' repoName
done
Round up
The terminal can feel like an intimidating 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!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
A