Sie sind auf Seite 1von 8

The GNU/Linux Command Line - Part 1

September 28, 2011

1 Introduction

The GNU/Linux operating system provides a rich graphical user interace which is sucient for the
needs of most non-technical users. But as programmers and software developers, we need to learn how
to interact with the system using the text based command interface. This lesson will get you started
with the command line.
You can watch the screencast: cmdline/cmdline1.mp4, cmdline/cmdline2.mp4
Those who have some experience using the commandline (and also familiar with ideas like standard
output redirection) can quickly skip to the exercises section!

2 Launching the terminal

The rst step is to re up an application called the terminal. On my Ubuntu 10.04 machine, the
terminal is available under Application->Accessories; if you are using some other Linux distribution
(say, for example, Fedora - or even a dierent version of Ubuntu) this location might be dierent.
Figure 1 shows how our terminal looks like.
The rst thing you note inside the terminal is the command prompt - a string which looks like this:

recursive@sherlock:~$

This is the place where you type in commands. The prompt contains information like your login name
(recursive is my login name) and machine name (sherlock is the name of my machine). The  $ sign
tells you that you are logged in as an ordinary user .
1

3 Running a few simple commands

Let's now try running a few simple commands!


Check out Figure 2; it shows the output obtained by running the commands: date, echo, pwd and
ls
The working of date is obvious - it displays the current date and time. The echo command can be
used to display messages on the screen - echo hello will print the message hello.
On a Linux system, les are grouped into folders (directories ). A directory will contain les as well
as other directories. The starting point of this tree structured directory hierarchy is the root directory
which is represented by the symbol / (a forward slash). Most Linux systems will have a directory
called home under the root directory. If there is a user on your Linux system called rahul, there will
also be a directory called rahul under the home directory. You say that the login directory or home
directory of user rahul is /home/rahul (The rst / represents the root directory; the next / is used
as a separator - so /home/rahul implies the directory rahul under a directory called home which is
itself under the root directory).
The pwd command prints the working directory - that is, the directory under which you are currently
situated. In our case, it shows /home/recursive because we are logged in as the user recursive.

1 Linux systems have the concept of a super user - if you log in as a user called root, you have full control over
the machine. You can do things like install new software, create partitions, congure the network etc. The super user
prompt will contain the symbol # instead of  $.

1
Figure 1: Terminal

Figure 2: Simple Commands

2
Figure 3: Man page of the ls command

The ls command shows you all the les and directories under the current directory. On GNU/Linux
systems, it is possible to congure ls in such a way that it displays the names of directories and ordinary
les in dierent colours. In the gure given here, the names displayed in blue represent directories and
the names in black represent ordinary les (you will see that there is only one ordinary le here; it is
called url ).

4 Getting help

Almost all Linux commands have their own manual page ; a command called man helps you view the
manual page. Figure 3 shows the output obtained by running the command man ls.

5 Options

A single Linux command is often capable of performing many actions. For example, the ls command
is capable of displaying not only the names of les in a directory but also the size of the le, its
date/time, access permissions etc. We use what are called options to control the output generated by
a command. Figure 4 shows the output obtained by running the command ls -l abc (abc is the name
of a le). Note the number 6 in the output - it is the size of the le. You can also see the date/time
associated with the le, and many other things!

6 Creating a le - the quick and easy way!

Let's now create a small text le using a simple (but powerful) technique called standard output
redirection.
Try this experiment: at the command prompt, type:

3
Figure 4: Running ls -l

date > pqr

Do you see any output on the screen? No! If you run the ls command, you will see that a new le
called pqr has been created. Let us nd out what is inside this le by running the cat command:

cat pqr

You see that the content of this le is the output generated by the previous date command! Figure 5
shows the status of the terminal.
In general, if you run a command of the form:

A > B

the command A will get executed and whatever output it generates on the terminal will get stored
in a le called B. You will not see any output on the terminal. The important idea is that this will
work for ANY command that generates some output onto the terminal (the terminal is the so-called
standard output of your program). If you try:

echo hello > pqr

whatever is currently present in the le pqr gets erased and the string hello gets stored into it .
2

7 Copy, Delete, Rename les

Here is how you copy a le called abc to another le called def :

cp abc def

You can delete a le like this:


2 The symbol >> can be used to append the output of a command to a le without erasing the data already present

4
Figure 5: Output redirection

rm abc

And, you can rename a le by running the command:

mv def ijk

8 Create a new directory

The mkdir command can be used to create a new directory. Check out Figure 6. The cd command
can be used to move into the newly created directory. Running cd .. will take you back to the old
(parent) directory .
3
An empty directory can be deleted by using the rmdir command.

9 Copy les from one directory to another

Let's say you have two directories, called work and play both under the directory /home/recursive (as
shown in Figure 7). You wish to copy a le a.txt present in work to the directory play.
How you invoke the copy command depends on where you are located at the moment. If you are
currently located in the directory /home/recursive (you can verify this by typing pwd ), you can simply
do:

cp work/a.txt play

There are one or two ideas here which are worth mentioning:

1. A le name is always interpreted with respect to the current directory. For example, if the current
directory is /home/recursive, the name work/a.txt refers to the le a.txt present in directory work
which is under the directory recursive (which is in turn under home, and home is under the root
directory, /).

3 Don't forget to type a space between cd and ..

5
Figure 6: mkdir

2. A le can be uniquely identied by its absolute path name - a name beginning with a /, like
/home/recursive/work/a.txt, is called an absolute path name. An absolutely name can be used
to identify a le (or directory) without referring to the current directory.

3. The destination of the copy command can be a directory, in which case the source le gets copied
to that directory under the same name. In the above case, the le a.txt gets copied to play under
the very same name.

Now, what if you are currently under the directory work ? You can issue the command:

cp a.txt /home/recursive/play

Or, in an even simpler way:

cp a.txt ../play

The name .. always refers to the parent directory; if your current directory is /home/recursive/work,
the parent directory is /home/recursive. So the name ../play refers to /home/recursive/play.

10 Exercises

Please try to nd out the answers to the exercises by experimenting on your machine! You can use
the man pages as your reference.
All questions are multiple choice; there may be more than one correct answer - you have to choose
the BEST one.
After nding out the answers to all these questions, you can enter them on the recursive-labs.com
web site.

1. I have a directory called test which has many les and sub-directories under it. I wish to create a
copy of this directory (including all the les, sub-directories, sub-sub-directories etc under test );
let's call the copy new-test. What is the best way to do this?

6
Figure 7: Copy le

(a) cp -t test new-test

(b) cp -r test new-test

(c) cp -R test new-test

(d) Both (b) and (c)

2. I wish to rename a le called a.c to b.c (using the mv command). I want the mv command to
give me a warning (and ask for conrmation) if there already exists a le called b.c. How do I
do this?

(a) mv -i a.c b.c

(b) mv -p a.c b.c

(c) mv -w a.c b.c

(d) mv -c a.c b.c

3. The echo command will print a newline character after displaying the message (say hello). I
want to turn o this behaviour; that is, echo should not print a newline character. How do I do
this?

(a) echo no-newline hello

(b) echo -n hello

(c) echo -t hello

4. I wish to see the names of ALL the les and directories on my GNU/Linux system, starting from
the root directory. For this, I have to rst become root, the super-user (ordinary users may not
have permission to view contents of all directories. You can use the su command to become
super-user ) and then issue the command:

(a) ls -r /

(b) ls -R /

7
(c) Both (a) and (b)

5. Each user on a GNU/Linux system has a specic user id. Which is the command used to nd
out the user-id of the currently logged in user?

(a) who

(b) pid

(c) id

6. I want to print a message (say hello) in an innite loop. Which of the following commands can
be used for this purpose?

(a) dmesg

(b) pmesg

(c) yes

Das könnte Ihnen auch gefallen