On October 23rd, 2014, we updated our
By continuing to use LinkedIn’s SlideShare service, you agree to the revised terms, so please take a few minutes to review them.
GETTING STARTED WITH THE LINUX SHELL
WHAT’S THE SHELL?
The shell (‘command line’) is a program that takes commands from you
and gives them to the OS to perform.
Before GUIs came around, the shell was the only way to interact with the
Even though GUIs are common today, it’s important to know how to use
the shell if you want to use the full power of Linux
Many different shell programs exist, but the most common one is called
bash - short for Bourne Again SHell (since it was written by Steve Bourne).
WHY USE A SHELL?
GUIs are good at a lot of things, but they’re not good at everything.
Using the shell can simplify your work and help you automate your tasks.
Moreover, there are some things you just can’t do with a GUI.
Here’s a little shell command that outputs the top ten largest files on your
hard drive to a file called ‘diskuse.txt’
du -h | sort -h | tail > diskuse.txt
Doing that on the GUI requires a specialized program written in a hundred
or so lines of code.
You can access a shell on pretty much any Linux system by hitting Ctrl + Alt
+ F1 - F6.
Each of these key combinations produces a ‘new’ terminal - a bit like
having six different screens, of which you can see one at a time.
Once on a shell, you may get a prompt for a login and a password. Enter
that, and you’ll get something like this
Let’s take a closer look here.
The text before the blinking cursor here is what we call a ‘prompt’, and it
provides information about the computer you’re working on. The prompt here is
There are four parts to this:
is my username on the computer
is the hostname (the ‘name’) of the computer
(the part after the :) means that I’m currently in my home directory
means that I’m a normal user (not root)
THREE VITAL COMMANDS
which shows you which directory you are currently working in
which shows you the contents of the current directory
which allows you to change your directory
COMPILING C/C++ PROGRAMS
Linux comes with a great C language compiler called gcc. Generally, it’s
pre-installed, but on some Ubuntu boxes, it’s not. If you’re using one of
those boxes, you can install it from the internet by typing this in the shell:
sudo aptitude install build-essential
Once you have gcc up and running, you can use it to compile C programs.
I’ve written a very basic program here (in notepad) as an example, and
saved it as helloworld.c
OUR EXAMPLE C PROGRAM
/* Small C Program to demonstrate the use of gcc */
printf (“Hello, world.n”);
/* End program*/
I’ve gone ahead and saved this program as helloworld.c in the /home/puranjay/Demo
OUR EXAMPLE C PROGRAM
Before we can compile our program, we have to find the folder it’s in.
First step : Find out where we are.
A pwd command results in ‘/home/puranjay’. Now, I stored our program in the folder
/home/puranjay/Demo’, so let’s try to get there.
I’ve done cd Demo to change my directory to ‘Demo’, and then did a pwd, which confirms that I’m now
in the right directory /home/puranjay/Demo
OUR EXAMPLE C PROGRAM
Just for fun, let’s check out the contents of our current directory, by doing an ls command.
The ls command shows that there are four objects here. The first three, with a / in front of their name
are directories (folders), and our shell has highlighted them in blue. Next to the directories, sure
enough, is our C file, helloworld.c
Now that we’re in the right folder, it’s time to start compiling.
gcc, like all compilers, is a complex piece of software in itself, consisting of
over 7 million lines of code. It has numerous options for code optimization,
error reporting, permissiveness and so forth.
Since our program is a simple one, we needn’t be concerned with all the
options. All we need is a simple command.
Our command is gcc helloworld.c -o output
The part right after gcc is our filename, helloworld.c .After providing the
filename, we’ve added -o ouput which tells the compiler to make an
executable output file called output
Another ls command reveals that the file output has now been created. It is
marked with a * by our shell, to show that it is an executable file - it can be run.
To run it, we’re just going to type its name in the shell.
And there you go! Our program runs just as expected, displaying ‘Hello, world.’
on the screen.