Note: Keep in mind that since this is mostly a Debian/Ubuntu/Mint related website, examples are given for these operating systems, however this does not mean they will not work, but some stuff may be a bit different on your platform (like graphical tools, some specific directories or configuration files and so on).
What Is GNU/Linux?
GNU/Linux is an operating system comprising of the Linux kernel, GNU tools and utilities, eventually a graphical
desktop environment like KDE or GNOME, and all other user applications and games. A Linux distribution like Ubuntu, Mint or
Fedora takes all these parts, puts them together and further provides CD/DVDs, ISO images, support and a package management
system for installing and removing applications. There are several big distributions out there upon which other projects
build on. For example, there is Debian, on which Ubuntu is based. Mint is another growing distribution which is based on
Ubuntu. You can read the Introduction to Linux and Ubuntu for a more in-depth information
about Linux and distributions using it.
What Is a Shell?
A shell is a command interpreter which allows you to interact with the computer. The way things work is pretty
simple: you type in commands, the shell reads them, performs the tasks it was asked to do, and finally it sends the
results to the standard output, which is usually the screen. Here is an example output of the ls command, which
lists the files in a directory:
mintuser@mint:~ > ls Desktop Documents Downloads Music Pictures Projects Videos
Your home directory usually is /home/USER, where USER is your username. You can find out
what is your home directory by typing echo $HOME, and see what is your username by typing whoami or
echo $USER.
To access the shell you will have to open a terminal application. Once the terminal starts, Bash will run
inside it. The default shell application in Ubuntu is GNOME Terminal, and there are several ways to start it.
You can press the keyboard shortcuts Ctrl+Alt+T, or press Alt+F2 and then type gnome-terminal in the
run window that appears, followed by Enter. The third method is to go to Applications and search for the terminal
program there.
In Linux there are several terminal applications (also called terminal emulators or console applications). These include GNOME Terminal, Konsole, Guake, Yakuake, XTerm, rxvt and so on. You can read about picking a terminal emulator application here.
After starting the terminal, the prompt may look something like user@host$ , so you can now start entering commands.
In Linux there are several terminal applications (also called terminal emulators or console applications). These include GNOME Terminal, Konsole, Guake, Yakuake, XTerm, rxvt and so on. You can read about picking a terminal emulator application here.
After starting the terminal, the prompt may look something like user@host$ , so you can now start entering commands.
Bash and Other Shells
The default shell on most Linux distributions is Bash, the Bourne-Again Shell. Bash is pretty complex and has features
like command completion, keyboard shortcuts, persistent command history, aliases and built-ins. Bash is based on the
older UNIX shell sh, which is not used anymore. In Linux, sh is usually a symbolic link to Bash.Other popular shells include:
- csh - the C Shell, with a syntax resembling the C programming language
- tcsh - an improved version of the C Shell
- ksh - the Korn Shell, initially developed in the early 1980s
- dash - the Debian Almquist Shell, a lightweight shell created by the Debian distribution
To see what is the default shell on your system, type cat /etc/passwd in the terminal, and look for the line
containing your username. Example output for user embryo, showing Bash as the default shell:
mintuser@mint:~ > cat /etc/passwd root:x:0:0:root:/root:/bin/bash ... embryo:x:1000:1000:Embryo,,,:/home/embryo:/bin/bash mintuser:x:1001:1001:Embryo,,,:/home/mintuser:/bin/bash
What Are Linux Commands?
Before starting entering commands, let's see what a Linux command is. Linux commands are executable binary files or
scripts (that is,
they are programs) which can be ran to perform certain tasks, like for example listing the files in a directory or showing
the current date and time. They are identical to graphical applications, only they don't create fancy windows and show
their output in text-mode. Later we'll see how powerful these commands can be, and how combining them into scripts will
lead to the possibility of task automation. For example, renaming a very large number of files in a directory can be done
in one single line, making use of the appropriate commands. This would take forever in a graphical environment, where
you would have to select a file one at a time in your file manager and rename it manually.Examples of frequently used commands are ls, cd, pwd, date or cat. With the exception of executable files, there is also a category of commands called shell built-ins, which are commands provided by the shell itself (Bash in our case). We'll deal with those later.
The general form of a Linux command is:
The first argument is the command itself (like ls or echo), the second argument can be an option or a list
of options to pass to the command (for example to show or hide hidden files), while the third is a filename upon which
the command may make changes. For example:
Will list all the files inside the / directory (also called root directory), using the long listing format
(which was specified by the -l option).
A command may or may not have arguments. An argument can be an option or a filename. Here is another example:
This will output the text Hello, world, followed by a newline character. The -e switch tells the echo
command to interpret escaped characters, like the trailing \n, which will add a newline after the text inside
the double-quotes. Without the -e switch, the output would have been this:
Now let's proceed and type in some basic commands to become familiar with the shell environment.
command [option(s)] [filename(s)]
mintuser@mint:~ > ls -l / total 96 drwxr-xr-x 2 root root 4096 sep 17 13:46 bin drwxr-xr-x 3 root root 4096 sep 27 15:14 boot drwxr-xr-x 15 root root 4380 oct 5 12:36 dev drwxr-xr-x 159 root root 12288 oct 4 18:23 etc
A command may or may not have arguments. An argument can be an option or a filename. Here is another example:
[embryo@mint] ~$ echo -e "Hello, world\n" Hello, world [embryo@mint] ~$
[embryo@mint] ~$ echo "Hello, world\n" Hello, world\n
Moving Around Using cd and pwd
Usually when you start a terminal the default starting location (called the current working directory) is your
home folder, /home/USER, where USER is your username. Let's say we want to move to the root directory:
cd /
[embryo@mint] /$ ls / bin dev floyd initrd.img lib media opt root sbin srv tmp var vmlinuz.old boot etc home initrd.img.old lost+found mnt proc run selinux sys usr vmlinuz [embryo@mint] /$
[embryo@mint] /$ pwd /
Listing Files with ls
This command will list files and directories, and it takes various arguments to format its output. ls without
any arguments will list the files in the current working directory. ls -l will list the files using
the long-listing format, outputting details like modification date, owner and size of the files:
[embryo@mint] ~/apps/ioquake3$ ls -l total 2268 drwxr-xr-x 2 embryo embryo 4096 sep 28 05:13 baseq3 -rw-r--r-- 1 embryo embryo 15148 sep 28 05:12 COPYING drwxr-xr-x 2 embryo embryo 4096 sep 28 05:12 demoq3 -rw-r--r-- 1 embryo embryo 9930 sep 28 05:12 id_patch_pk3s_Q3A_EULA.txt -rwxr-xr-x 1 embryo embryo 689417 sep 28 05:12 ioq3ded.i386 -rwxr-xr-x 1 embryo embryo 879 sep 28 05:12 ioquake3
[embryo@mint] ~$ ls -lhG /usr/bin total 433M -rwxr-xr-x 1 root 38K ian 17 2013 [ lrwxrwxrwx 1 root 8 sep 9 18:39 2to3 -> 2to3-2.7 -rwxr-xr-x 1 root 39 feb 17 2012 7z -rwxr-xr-x 1 root 40 feb 17 2012 7za ...
Absolute and Relative Paths
An absolute path is a complete path to a given point, and it always starts with the root directory
(the forward slash: /). Examples of absolute paths: /usr/bin, /etc/apt/cache,
/home/USER. As you can see, they are all absolute paths since they specify the complete path up to
a certain point.On the other hand, a relative path will not start with the forward slash, and it will always take into consideration the current working directory. For example, if the current directory is /home and you would like to change it to /home/USER/Downloads, you could do it like this using an absolute path:
cd /home/USER/Downloads
cd USER/Downloads
The . and .. Files
These are virtual files, the single dot meaning the current working directory, while the double dot means the
parent directory of the current working directory. These may seem strange, but they are actually very useful
when working in a shell. For example if you are inside your home directory and you want to copy a file from some location
to the current working directory you could do something like:
cp /etc/apt/sources.list /home/USER
cp /etc/apt/sources.list .
Use .. to go up one level in the hierarchy, to the parent directory. For example, if you are inside /etc/apt/sources.list.d and you would like to go to /etc/apt, you could use:
cd ..
cd ../..
Resources
This tutorial is based on this guide, written a while ago.
This version further details several aspects of the shell, as well as adding new commands and explanations for some Bash
features.
- LinuxCommand.org - Easy to understand, must-read guide designed for beginners about the Linux command-line
- Introduction to Linux by Machtelt Garrels - Very good introduction to Linux command-line, with real-life examples
- The UNIX Programming Environment by Mark Burgess - Introduction to UNIX, including Bash and Perl programming guides