Академический Документы
Профессиональный Документы
Культура Документы
Introduction to Linux
sergio@silicon:~$
Commands are introduced by typing some characters and pressing the Enter
key.
• echo STRING
Displays the line of text given in STRING.
Commands consist of one or more chunks or tokens separated by spaces.
The rst token is the command itself, that is the instruction to be executed.
The rest of the tokens are arguments, which give extra information on how the
instruction should be executed. They can be options, le names, numbers...
This depends on each program.
Notice that the CLI is case-sensitive! This means that Echo, ECHO, etc, will
not work!
∗ Actually it should read, "Introduction to command-line interfaces on UNIX-like operating
systems". For more information on UNIX, Linux, GNU, etc., you can have a look at their
Wikipedia pages.
1
Figure 1: Example of a UNIX le system.
2
so on. Everything is contained in a master directory, referred as root, denoted as
/. In Figure 1 there is an example of a le system. The root directory contains
three subdirectories:home, usr and media. home contains three subdirectories,
graham, john and terry. graham contains two les, parrot.avi and spam.txt.
The extensions of the les, avi and txt, suggest that they are (most likely, but
not 100% sure!) a video and a plain text le, respectively.
B Change to the root directory (cd /) and list its contents (ls).
3
B Create a directory in your home directory called dir.
B Go into dir, and create another directory inside called subdir. Go back to
your home directory.
We can also refer to les and directories in other directories by giving the path
to them, which is the names of the parent directories joined by slash characters
(/).
B Change into the subdirectory that you just created using cd dir/subdir.
The current directory is referred as ., and the parent directory as ...
The home directory can be referred as ∼.
Files and directories are uniquely identied by their full address, known as
absolute path : it is the list of its parent directories in order, separated by /.
B What is the absolute path of subdir? What is the relative path to /etc/X11
from your current directory? List the contents of that directory using only one
command.
B Auto-completion is a neat trick that most modern shells can do. Type
ls /h, and before pressing Enter, press the Tab key ( →−
−−−
→ ). Nice, isn't
it?
Before you jump to the next section, make sure that you understand:
4
B Download the text le located at http://www.chem.helsinki.fi/~sergio/
menu
• wget URI
Download the resource located at URI.
The le menu is a plain text le. That means that it only contains a bunch
of characters. Text les are not the same as a le produced by a text processor,
like Microsoftr Word documents or similar! There is no format, pages, etc.:
only text.
The command cat is the most simple way to see the contents of a text le.
It simply dumps the le onto the console. The name comes from concatenate,
because it can be used to join several les.
5
B Edit menu. Make some changes to the le, save and exit. Examine the le
with less
B Create a new text le containing a short line of text. What is its size? Use
ls -l for this. How is the size related to the text you wrote?
Once you are more familiar with text editors, you should try to nd which of
the many ts best your needs and preferences. They have dierent capabilities,
but many of them are specially adapted to the needs of programmers . Some
2
have familiar graphic interfaces, such as Kate or gedit. Vim and Emacs are con-
sidered to be the most powerful editors by many, and with a long development
history and a large user community, they have plenty of add-ons and extensions
available. This has a cost: they are not the most intuitive and easy to use for
beginners, which is specially true for Vim.
6
B Familiarize yourself using the previous commands on the directories you have
created and the menu le:
Rename a le.
Remove a le.
Remove a directory.
The touch command might be useful to create dummy les to play around.
• touch FILE[s]
Create empty FILE[s].
If FILE[s] already exist, modify timestamps (the times shown by ls -l). This
pretends to have modied (touched) the le.
Wildcards are special characters that can be used to refer to multiple les at
the same time. ? can be used to substitute any single character, and * for any
string of characters.
B Create a new directory. Go into it, and create three les called file1, fileZ
and file.txt. What is the output of ls file?? And ls file*?
B What will happen if you enter rm *? Think twice before doing this!
Help!
So many commands already! You must be wondering how people manage to
remember all this. Practice helps, especially when you need CLIs often. But
the truth is that few remember all details all the time, and everyone needs some
help at some point!
There are two main ways to get help: the help option and the man pages.
7
• --help option
A command run as COMMAND --help, instead of doing what the command nor-
mally does, will display a brief explanation of its usage and purpose and a
summary of its options.
#
• man COMMAND
Shows the man pages (the reference manual) of COMMAND.
This is commonly the most detailed information that can be found about the
command.
man uses by default less as front-end to navigate the le.
" !
B Look at the documentation of some command using the help option. Then,
have a look at its man page.
B Open the shell calculator bc and run some calculations. What is output and
what is input?
• bc
Simple calculator. Exit with quit.
The output of a command can be saved to be used for some other purpose
later on. This is called output redirection, and is done using the > operator.
• COMMAND > FILE
Redirect the output of COMMAND into FILE.
B Run bc > out, run a few calculations, and exit. Examine the content of the
newly created le out.
If the le you are redirecting to already exists, the > operator will overwrite
its contents. The operator instead appends the output to the end of the le.
8
• COMMAND FILE
Same as >, but append to the end of FILE instead of overwrite.
B Test the dierences between > and rewriting an existing le, appending new
output to an existing le, etc.
Input can be also redirected. Imagine that you have to run a program
many times, typing always the same input. We can redirect input using the <
operator, so that a program takes the information it needs from a le instead
of the keyboard.
• COMMAND < FILE
Use the contents of FILE as input for COMMAND.
B Create a text le input containing several arithmetic operations, one in each
line. Run bc so the operation are read from input.
In some cases, we produce some output with a program, which is then fed
to another program as input. This can be done easily using pipes.
• COM1 | COM2
Pipeline: execute COM1 and redirect its output as input for COM2.
#include <stdio.h>
int main(){
printf("Hello,world\n");
}
Computers only understand machine code, which, as you might have guessed,
is composed of 1s and 0s. In order to translate the program into something the
9
processor can understand, we use programs called compilers. For this course,
we will use , the GNU C compiler.
In order to run the program, we need to give the path to it. For something
that we just compiled, it will be in the same directory (.), so we need to write
./ before the name of the program.
10