Note: This is the first article in a series intended to cover the basics of Linux and working in with the command-line. Since this is a work in progress, one chapter will be provided at a time. To see the table of contents, have a look here. If you are new to Linux and Ubuntu, I suggest you read the Ubuntu introductory guides too.
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, standard directories 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:
[email protected]:~ ls
Desktop Documents Downloads Music Pictures Projects Videos
This is a list of files inside the the home directory. The home directory contains all your personal files and folders, as well as configuration settings for installed applications, downloads, music and so on.
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 [email protected]$ , 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
In this tutorial we will focus on Bash, since it is the most widely used and also one of the most powerful shells out there. Bash is developed by the GNU project, and it is a modern implementation of the older Bourne Shell (sh). On a Debian or Ubuntu distribution, the default shell used by the system is specified in the /etc/passwd file.
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:
[email protected]:~ cat /etc/passwd
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:
command [option(s)] [filename(s)]
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:
[email protected]:~ ls -l /
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
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:
[[email protected]] ~$ echo -e “Hello, worldn”
[[email protected]] ~$
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:
[[email protected]] ~$ echo “Hello, worldn”
Now let’s proceed and type in some basic commands to become familiar with the shell environment.
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:
The cd command is used to change the current directory. And now let’s list all the files inside this folder:
[[email protected]] /$ 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
[[email protected]] /$
Alternatively, we could’ve written only ls instead of ls /, because ls without any arguments will list the files in the current working directory. To see which is the current working directory, we will use the pwd command, which stands for print working directory:
[[email protected]] /$ 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:
[[email protected]] ~/apps/ioquake3$ ls -l
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
You can also group options. For example, ls -l -h -G /usr/bin can be written as ls -lhG /usr/bin and it will list all files inside the /usr/bin directory, using detailed, long listing format (-l), showing sizes in human readable format (-h) and supressing the group details (-G):
[[email protected]] ~$ ls -lhG /usr/bin
-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
Another useful argument is -a, which will also show hidden files (files that start with a dot like .bashrc). Try it in your home directory!
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:
In this example we specified the full path to the Downloads diretory, or the absolute path. Notice the leading forward slash for root. To do it using a relative path we would issue this command:
Notice that the forward slash is missing, and the shell will prefix the path specified by cd with the path of the current working directory, which is /home, thus resulting /home/USER/Downloads. When specifying a relative path, the shell takes into consideration the current directory, and starts the search for the specified file or directory from there.
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
As you can notice, you specify your current working directory. Still, this can be done by typing less text, like this:
cp /etc/apt/sources.list .
Notice how the . tells cp to copy the sources.list file inside the current working directory.
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:
And moreover, to go inside /etc from /etc/apt/sources.list.d, issue:
This concludes the introductory chapter on shell basics. The next chapter will focus on showing and explaining some useful general-purpose commands, including copying and removing files, creating files and directories, showing the date and time, viewing files with cat, head and tail, searching for files and getting help.
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