The Essential Primer For the Linux Terminal

Blank Terminal

Look at that hideous thing!  Surely we want to avoid the terminal right?  WRONG!  While the terminal is intimidating for the uninitiated, it is an immensely powerful tool with several advantages over GUIs and desktop environments.

Getting past your initial misgivings is important.  Once you learn and embrace the terminal, it will simplify your life and open up new worlds to explore.

 

Optional? Maybe. Recommended? Absolutely!

With each new distribution and version of Linux, the terminal becomes less and less necessary.  A few years ago, there was no graphical tool to install software, or even install Linux for that matter.  At some point every Linux user had to enter the terminal.

But many modern Linux distributions have handy graphical interfaces for most tasks, and the average Linux user could likely avoid the terminal.

Example of GUI tools for common Linux tasks.

There are a lot of excellent attributes that GUIs bring to the table.  I love my GUI, and if you want to take it from me, you’ll need to pry it from my cold, dead fingers!  However, they are not perfect.  There are several tasks that the terminal handles more elegantly, and, if used properly, the terminal is often much quicker to interact with.

 

GUI Problem #1: Inefficiency

Terminal commands are fast to execute.  I can install the Chromium web browser in Linux Mint (pictured above) with a single command and a password.

apt-get install chromium-browser

 

Reproducing that using the GUI requires finding the right application in the launch menu, typing in the password, browsing or searching for Chromium, opening the software listing, and finally installing Chromium.

The terminal method is faster and easier if you know the necessary command.  And therein lies the conundrum.  The GUI is easy to browse and poke around to find what you want.  The terminal requires accuracy and precise knowledge.  But, if you have the accuracy and knowledge, dealing with the GUI overhead just drags the process down.

This is a major reason that Linux masters hang around the terminal.  To us mere mortals, the terminal seems clunky and antiquated.  To them, the terminal seems simple and pure.  As you become familiar with the terminal, you will likely find yourself relying on it more and more.

 

GUI Problem #2: Difficult to Explain

If I were writing a how-to guide on installing Chromium, using the terminal would be much simpler to explain than using the GUI.  The GUI method would need several screenshots and is challenging to follow.

Additionally, GUI layouts and appearances change rapidly while terminal commands rarely change.  An installing Chromium using the terminal guide would likely last for years without needing updating.  An installing Chromium using the GUI guide would likely need updating with each new version of the distribution.

The terminal method requires a short command in an easy to copy & paste format.

 

And while this may not make too much of a difference to you, it does to everyone writing, speaking, or explaining Linux concepts.  If you ask for help on a Linux forum, you will likely be asked to run terminal commands.  If you read a how-to guide, you will likely see terminal commands.  We here at Down To Earth Linux do this often.

Terminal commands are much more clear and concise to write about.

 

GUI Problem #3: Inconsistency

One of the best attributes of Linux is the freedom and flexibility it provides.  It is also one of the worst attributes.  There are countless distributions of Linux, each with their own quirks and personalities.  For each distribution, there are several desktop environments.  For each desktop environment, there are many competing tools to do the same job.

Installing Chromium in Linux Mint running the Cinnamon desktop environment is significantly different from installing Chromium on Ubuntu running the Unity desktop environment.  And that is different from Debian running the XFCE desktop environment.  The tools are different, the methods of access are different.

However, the terminal command I used earlier will work on Linux Mint, Ubuntu, and Debian regardless of the desktop environment they are running.  Not all distributions use the same commands for the same job; however, the possible permutations decrease from hundreds down to only a few.

The simple and spartan nature of the terminal makes it largely consistent regardless of the distribution or desktop environment.

 

GUI Problem #4: Repeating Yourself

Computer geeks have a mantra: “don’t repeat yourself.”  Being computer geeks, they turned that into an acronym: “DRY.”  The DRY principle is simple.  If you find yourself regularly doing a specific task by hand, you’re doing it wrong.

As an example, let’s say we wanted to copy the BACKMEUP directory somewhere once a week, every week.  Doing this by hand is a DRY violation!  We should automate the procedure.

Well, most GUI file managers can’t do that.  But, you can write a short shell script and cron job to do that very quickly, if you’ve mastered the terminal.  Then you never have to copy BACKMEUP again.  It’s done automatically.

 

GUI Problem #5: Nightmarish Batch Operations

A batch operation is a series of jobs executed on a computer without manual intervention.  Running a batch operation is another way to not repeat yourself.  Let’s say we wanted to change all files in a directory with a ‘.php’ extension to ‘.html’ files.  Doing that in most file managers requires changing each one individually.

With a terminal, you need only run the following command once:

 

 

GUI or Terminal, That Is the Question

The problem with all the terminal commands above is that you have to know the commands and be comfortable using them.  Terminal commands are not intuitive, and are difficult to master.  In that respect, GUIs are superior.  You can browse around until you find what you are looking for.  And a well designed GUI requires very little experience or knowledge to master.

I’m not trying to persuade you to abandon the GUI.  However, some jobs and situations are better handled by the terminal.  Being familiar with the terminal, and knowing what it is better equipped to do is highly recommended.  Don’t abandon the GUI, just supplement it with the terminal.

 

 

Moving On: The Terminal Display

Finding the Terminal

So now that we have extolled the virtues of the terminal, let’s discuss actually using it.  The first step is finding it.  Many distributions have the terminal prominently displayed in the launcher menu.  If that doesn’t work, look under headings like system, tools, or accessories.

It is usually named Terminal or Terminal Emulator.

 

 

 

My Mouse Doesn’t Work!

Yep!  That’s correct.  The terminal is a keyboard environment.  Most terminal programs will let you highlight text and copy it using a mouse, but you can’t move the cursor or jump around using the mouse.  So set your mouse aside and grab your keyboard.

 

Dissecting the Prompt

When you open up the terminal, you should see something like this:Blank Terminal

 

Pictured above is the default prompt for Linux Mint, and it is a fairly standard example.  That being said, the prompt is completely customizable, and many distributions make their own changes.  Yours might be different.  The colors and text may vary, and you might see some other text.

Let’s analyze each section of the prompt above starting on the left and moving right.  Everything before the ‘@’ symbol is your username; in my case, my username is ‘john.’  After the ‘@’ symbol is the computer name.  This was set when installing Linux.  In my case, the computer name is ‘DTELinux.’

Next, we come to the tilde, ‘~’.  In Linux, the tilde is a special symbol meaning the current user’s home directory.  That space containing the tilde displays your current position in the Linux filesystem.  If you are in the Documents subdirectory within your home directory, that part of the prompt changes to ‘~/Documents’.

Then, we have the ‘$’.  This symbolizes that the terminal is waiting for your input.  Before the ‘$’ is contextual information, after the ‘$’ is your input.

And lastly, is the white rectangle.  That is your cursor.  Any typing you do is inserted where the cursor is.

 

Running Your First Command

Let’s start with a simple command that you will likely use often.  Type ‘ls’ into the terminal without the quotes, followed by the Enter key.

Output from 'ls' command.

 

We will discuss what that command does momentarily, for now let’s see what happened.  First, that particular command returns information.  In the terminal, returned information is displayed on its own line, or lines, without a preceding prompt.  After the feedback, a new prompt is given.

The terminal displays a history of what happens as you go along.  We first ran the ‘ls’ command.  Then ‘ls’ displayed information.  Then we had a new prompt.  This history is ordered chronologically with the newest at the bottom.

 

Command Examples: Navigating and Working With Files

The number of Linux commands is huge, and, for most humans, memorizing them is impossible.  Instead, you want to remember a few important ones and then use the magical tool, Google, to find new commands if you need them.

The most fundamental terminal skill is navigating through the file system, and executing basic actions on files.  So I’m going to talk about the most commonly used commands for that task.

ls

Output from 'ls' command.

The first command, ‘ls’, was used earlier.  The ‘ls’ command lists files and subdirectories.  If you don’t specify a directory, it will list the files and subdirectories in your current directory.  In the picture above, that is my home directory.

To specify a different directory to list the contents of, follow the ‘ls’ with a space and then the path to the directory you want.  If I wanted to see what was in the Pictures directory listed above, I could type ‘ls Pictures’ without the quotes.

Results from "ls Pictures" terminal command.

 

 

cd

The ‘cd’ command stands for Change Directory.  It changes your current location in the filesystem.  Type ‘cd’ followed by a space, and the path to the directory you want to go to.  So, if I wanted to move into the Pictures directory that I listed above.  I would type ‘cd Pictures’ without the quotes.

Result from running 'cd Pictures' terminal command.

 

Notice that this command did not have any information to return.  It just spit out a brand new prompt.  Some commands, like ‘ls’, return information, some do not.  If they don’t you’ll just get a new prompt, no fancy messages.

Also notice that the current directory part of the prompt has changed to show our new location.  It now says “~/Pictures.”

 

mkdir

The ‘mkdir’ command stands for make directory.  It is used to create a directory.  It requires you to specify the name of the directory you want.  To do so, follow ‘mkdir’ with a space, followed by the name of the directory.  If you want proof that it worked, run the ‘ls’ command afterwards.

Results from mkdir followed by ls command in terminal

 

If you want to create a directory somewhere besides your current location, you can.  Let’s say I now wanted to create a subdirectory called fubar within MyAwesomePicturesDirectory.  To do this, I would run this command: ‘mkdir MyAwesomePicturesDirectory/fubar’.

Results from mkdir subdirectory followed by ls command in terminal.

 

 

rm

The ‘rm’ command removes a file.  For example, if I wanted to remove MyAwesomePicture.png from the Pictures directory, I would type ‘rm MyAwesomePicture.png’ without the quotes.

Results from rm followed by ls terminal command.

 

 

mv

‘mv’ is short for move.  It is used to move a file or directory from one location to another.  ‘mv’ requires two pieces of information to work.  First, is the file or directory to move.  Second is the location you want to move it to, and the name you want to give it.  So, if I wanted to move the fubar directory out of MyAwesomePicturesDirectory and into Pictures, I could use the following command: ‘mv MyAwesomePicturesDirectory/fubar fubar’

Results from mv followed by ls terminal command

 

The ‘mv’ command is also handy for renaming files or directories.  If I wanted to rename the fubar directory to MoreAwesomePictures but keep it in the same location, I could do this: ‘mv fubar MoreAwesomePictures’.

Result from mv command in terminal to rename a file.

 

 

cp

‘cp’ stands for copy.  It is used to create a copy of a file.  Because the example above is getting messy, I’m going to start from scratch with a different example by closing and reopening the terminal, changing into the Documents directory, and listing the contents.

Results from ls terminal command in Documents directory

 

Now, if I wanted to create a copy of the WorldDomination.txt file and name it TakeOverTheWorld.txt, I would run the following command: ‘cp WorldDomination.txt TakeOverTheWorld.txt’.

Results from cp terminal command

 

 

Arguments and Switches

As you saw above, some commands need information given to them to work.  That information is provided using arguments.  The ‘cp’ command had two arguments, the source file, and the destination.  Each arguments is preceded by a space.  Some commands have only one argument, some have several arguments.  It just depends on the command.

 

In addition to arguments, commands can have switches.  Switches change what the command does in some way.  In Linux, switches are preceded by a space and a dash.

As an example, let’s take that ‘cp’ command again.  Without any switches, ‘cp’ cannot copy directories; it only copies files.  However, if we toss in the ‘-r’ switch, the cp command acts differently and suddenly gains the ability to copy directories.

To illustrate this, let’s start with a clean slate again by restarting the terminal and listing the contents.

Output from 'ls' command.

 

 

Now, let us copy the Downloads directory into the Music directory while keeping the same name.  To do it, I run the following command: ‘cp Downloads Music/Downloads -r’.

Results of cp -r terminal command.

 

Notice that I had two arguments, and the -r switch.  Let’s see what would have happened had I not included the switch.  This time, I’m going to try copying the Pictures directory into the Music directory without using that important switch.

Results of cp command to copy directory without the -r switch.

 

It didn’t work.  In its default mode of operation, ‘cp’ doesn’t work on directories.  The mode of operation needs altering using a switch.

 

Multiple Switches

Occasionally, you may need more than one switch to do what you want.  You can simply tack on more switches, each with the space and a dash.  For example, the ‘ls’ command with two switches could look like this: ‘ls -l -a’.

However, it is more efficient to combine the two switches together.  Two or more switches are combined by using only one space and dash, and placing the letters together like this: ‘ls -la’.

Results of ls -la and ls -l -a terminal commands

 

Those two switches change the types of information returned by the ls command and its appearance.  Notice that both methods of specifying the switches produced the same result.

 

Switches With Arguments

Some switches need arguments themselves to work properly.  For example, the ‘ls’ command has a switch which will ignore the specified file.  It requires an argument.  Provide that argument just as you would to the command itself.  Type a space followed by the argument.

For example, in the Documents directory, there are two files.  TakeOverTheWorld.txt and WorldDomination.txt.  If I wanted to list the contents of the directory, but ignore WorldDomination.txt, I would use the following command: ‘ls -I WorldDomination.txt’.

Results of ls -I terminal command.

Long-Form Switches

Sometimes, commands provide switches in a different format.  Instead of using a single character as the switch, it uses several letters or full words.  I like to call these long-form switches.  Long-form switches are preceded by a space and two dashes, instead of one.  And arguments for long-form switches are provided using an equal sign instead of a space.

For example, the ‘ls’ command has a long-form switch called ‘color’.  ‘color’ adjusts how ‘ls’ deals with color coding results.  If I use the argument ‘no’ for the color switch, the results will have no color coding.  So the command ‘ls –color=no’ lists the directory contents with no coloring.

Results of ls --color=no terminal command.

Is Your Head Spinning Yet?

I know, this probably seems horrible.  It does take some getting used to.  Nobody becomes a terminal wizard overnight.  It takes a lot of practice.  Fortunately, there is some help.  All of these commands have a manual page which talks about all the switches and gives you an idea of how to use them.

Learning to access the manual and using some tips for finding what you need keeps you from needing to memorize commands, arguments, and switches.  Check out our article: How to Use Man Pages to Enhance Terminal Skills.

 

Dealing With Special Characters

Some readers may have noticed that every file and directory name hasn’t had any spaces or weird characters like these: “!@#$%^&*()-/\'””.  Well, that was on purpose.  Certain symbols have special meanings in the terminal, and can’t be used without some special care.

If you find yourself in need of a space or a special character you need to ‘escape’ the character.  Escaping a character is the act of telling the terminal to treat it like a plain old character, instead of something special.  To escape a character, place a backslash (‘\’) immediately before it.

As an example, let’s say we wanted a directory named “Dictators and Despots.”  I don’t know why, just say we wanted it.  Well, the command for making a directory is ‘mkdir’, but if we just used the command ‘mkdir Dictators and Despots’, the terminal would treat that as 3 separate arguments instead of one argument.

To make the terminal understand what we want, we need a backslash before each space.  Like this: ‘mkdir Dictators\ and\ Despots’.

Results of mkdir command with spaces followed by ls

How about a directory named ABCDEFG#!J?  We must escape the pound sign (‘#’) and the exclamation point (‘!’).

Results of mkdir with special characters followed by ls terminal command.

 

Backslash means “don’t treat this next character special!”.  If for some reason you need a backslash, you escape it as well: ‘\\’.

 

A Shortcut For Spaces

There is another way to get spaces accepted by the terminal, but it only works for spaces, none of the other special characters.  That shortcut is to enclose the name in double quotes.  If I wanted a directory named Foo Bar, I could use the command ‘mkdir “Foo Bar”‘.

Results of mkdir command with quotes spaces followed by ls terminal command.

 

It is often easier to double quote something than to escape every space.

 

Time Savers

Up and Down Arrows

The terminal keeps track of what commands you run.  Pressing the up arrow accesses that history and copies the command to your current prompt.  Pressing the up arrow once accesses the last command.  Twice accesses two commands ago.  Three times, three commands ago.  And so on.

If you make a typo when writing a command, rather than rewriting it from scratch, you can press the up arrow to copy the previous command to the current prompt, and then fix the mistake.  This is a tremendous time saver when dealing with large commands with several arguments and switches.

Tab Key

The terminal has an auto-complete feature.  If you are typing a command, file name, or directory name, you can press the tab key to auto-complete what you are typing.  For example, if I wanted to use the ‘cd’ command to navigate to /home/, I could type ‘cd /ho’, then press the tab key.  The terminal would automatically fill in the ‘me/’ for the directory name.  Thus leaving me with ‘cd /home/’.

If the terminal has multiple auto-complete options at the point where you press tab, it won’t do anything.  You can press the tab key twice and get a listing of all the possibilities auto-complete has to choose between.

The tab key is hugely important.  You should rarely have to type a full directory or file name out all the way.  Type out some of it, then press the tab key.  Save yourself a lot of effort!

FFS! My Head Hurts!

Well, this covers the fundamental basics.  The first few times you use the terminal will be a painful experience.  It takes practice for the terminal to become a familiar tool that simplifies your life.

Despite being a card-carrying member of the GUIs are Awesome, Terminals are Antiquated club, I fall back on terminal commands frequently.  If you know the command, the switches, and the necessary arguments, it’s often quicker and easier.

 

If you have any tips for terminal newcomers, let us know in the comments below!

 

 

Written By

John is a sailing instructor and mechanical engineering student who happens to be a computer geek. To find more information about John, visit his website or find him on social media by clicking on the icons below.

Facebook Twitter Google+ Website

Tags: , ,

Join the Conversation!