Вы находитесь на странице: 1из 7

Introduction to Linux Command-Line for Beginners

By Craciun Dan - Latest Version | Oct 05, 2013 | v0.2.0


Get the latest version from here.
Note: This is the rst 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 stu may be a bit dierent 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 nally 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 les in a directory:
mintuser@mint:~ > ls
Desktop Documents Downloads Music Pictures Projects Videos
This is a list of les inside the the home directory. The home directory contains
all your personal les and folders, as well as conguration settings for installed
applications, downloads, music and so on.
1 of 7
Your home directory usually is /home/USER, where USER is your username.
You can nd 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.
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 specied
in the /etc/passwd le.
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
2 of 7
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 les or scripts (that is, they are programs)
which can be ran to perform certain tasks, like for example listing the les 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 les 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 le one at
a time in your le manager and rename it manually.
Examples of frequently used commands are ls, cd, pwd, date or cat. With the
exception of executable les, 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.
3 of 7
The general form of a Linux command is:
command [option(s)] [filename(s)]
The rst 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 les), while the third is a lename upon which
the command may make changes. For example:
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
Will list all the les inside the / directory (also called root directory), using the
long listing format (which was specied by the -l option).
A command may or may not have arguments. An argument can be an option or
a lename. Here is another example:
[embryo@mint] ~$ echo -e "Hello, world\n"
Hello, world
[embryo@mint] ~$
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:
[embryo@mint] ~$ echo "Hello, world\n"
Hello, world\n
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
4 of 7
is your username. Let's say we want to move to the root directory:
cd /
The cd command is used to change the current directory. And now let's list all
the les inside this folder:
[embryo@mint] /$ ls /
bin dev floyd initrd.img lib media opt root sbin srv tmp var vmlinuz.o
boot etc home initrd.img.old lost+found mnt proc run selinux sys usr vmlinuz
[embryo@mint] /$
Alternatively, we could've written only ls instead of ls /, because ls without any
arguments will list the les 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:
[embryo@mint] /$ pwd
/
Listing Files with ls
This command will list les and directories, and it takes various arguments to
format its output. ls without any arguments will list the les in the current
working directory. ls -l will list the les using the long-listing format, outputting
details like modication date, owner and size of the les:
[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
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 les inside the /usr/bin directory, using
detailed, long listing format (-l), showing sizes in human readable format (-h)
and supressing the group details (-G):
[embryo@mint] ~$ ls -lhG /usr/bin
total 433M
5 of 7
-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 les (les 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:
cd /home/USER/Downloads
In this example we specied 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:
cd USER/Downloads
Notice that the forward slash is missing, and the shell will prex the path
specied 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
specied le or directory from there.
The . and .. Files
These are virtual les, 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 le from some location to the current working directory you
could do something like:
6 of 7
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 le 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:
cd ..
And moreover, to go inside /etc from /etc/apt/sources.list.d, issue:
cd ../..
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 les, creating les and directories, showing the
date and time, viewing les with cat, head and tail, searching for les and
getting help.
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
7 of 7