Introduction to Linux

Introduction to Linux
Introduction to Linux
Andrew J. Bennieston
First Edition: June 30, 2010
1
Contents
1 Files & Directories
1.1 Directory Structure . . . .
1.2 File Manipulation . . . . .
1.2.1 Directories . . . .
1.2.2 Copying & Moving
1.3 Ownership & Permissions
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
3
3
3
4
5
5
2 Text Editors
6
3 Shell Trickery
3.1 System State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.2 Job Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.3 Pipes & Redirects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7
7
7
7
4 Printing
8
2
1 Files & Directories
1.1 Directory Structure
Linux uses a single-rooted hierarchical directory structure. That is, files and directories
are located in a tree which branches from a single root. The root directory, /, will
contain several other directories, amongst which will be:
/bin System-wide user binaries (shells, editors, basic commands)
/boot Essential system boot files
/dev Files representing hardware devices on the system
/etc System-wide configuration files
/home User home directories; user files are stored here
/lib System libraries (essentially, bits of code used by multiple programs)
/media, /mnt Both used as mount-points for external file systems
/opt Additional software (often KDE is installed in here, for instance)
/proc System state information
/root The root user’s home directory
/sbin System binaries (programs used for system administration)
/tmp Temporary files which programs may create during their operation
/usr Stores most of the installed software and libraries; also resources such as icons used
by that software
/var Log files, mostly
Linux follows the philosophy that “everything is a file”, so devices such as hard drives,
printers, keyboards, mice, etc. are represented as entries within /dev, while network
connections also have file-like properties!
1.2 File Manipulation
In this section I will use foo and bar as placeholders for filenames. Feel free to name
your own files however you like! Note that I will always refer to typing commands into
a terminal window; there are, however, graphical file managers available too!
3
1.2.1 Directories
To create a directory called foo, type:
mkdir foo
If you want to create a directory inside one that doesn’t already exist, you have two
options. For instance, to make foo/bar, you could do
mkdir foo
mkdir foo/bar
or you could use the -p option to mkdir, which tells it to create any directories which
are missing in the path you requested:
mkdir -p foo/bar
Note that in Linux, options to commands are typically prefixed with a single hyphen,
-, if they are single-letter options, and a double hyphen, -- for longer words. Thus,
mkdir -p or ls --color. To see the list of available options for a command, you can
typically use command -h or command --help. (Where command should be replaced by
the name of the command you wish to find out about). You can also type man command
to look for the manual page for a given command. Try man ls to see the information
about the ls command.
ls lists the contents of the current directory (or, whichever directory you pass as an
argument). You can change directory using the cd command. cd takes either absolute
paths (beginning with /) or relative paths (which begin with some other character),
taken to be relative to the current working directory. You can ask which the current
working directory is by typing pwd.
In shell commands, the ~ character is expanded to the home directory of the currently
logged in user, so cd ~ will change to your home directory, and mkdir ~/foo will make
a directory called foo within your home directory. Similarly, . refers to the current
directory, so mkdir ./bar will make a directory called bar in the current directory
(exactly the same as mkdir bar), and .. refers to the parent of the current directory
(one up in the hierarchy). Finally, cd takes a special option -, which changes back to
the previous directory:
Note that the hash symbol represents the start of a bash (shell) comment; you don’t
need to type the symbol or anything which follows on the same line for these commands
to work, they are here to annotate what is going on at each step!
cd
cd
cd
cd
~ # Go to your home directory
/var/log # Change into /var/log directory
- # Go back to your home directory (or wherever you were before the last cd)
.. # go up one level, you’re now in /home
Learning these shortcuts will help you to navigate around the filesystem quickly and
effectively from a terminal.
One final command relating to directories is the rmdir command, which removes a
directory (provided it is empty). For example,
4
mkdir foo
rmdir foo
1.2.2 Copying & Moving
You can make a new, empty file by typing touch foo to create a file called foo. To
copy files, use
cp source destination
and to move, use
mv source destination
You can copy or move multiple files at once, as long as the destination is a directory,
rather than a plain file:
cp foo1 foo2 foo3 MyFoos/
(the trailing / is optional; Linux knows whether the destination is a directory or not,
and will complain if it isn’t!)
Files can be deleted using rm. Be careful here; there is no “recycle bin” when you delete
a file in this way—you cannot get it back! rm -i will prompt you before deleting each
file, while rm -f will delete everything without asking (provided you have permission to
do so). If you have a directory full of files, and you want to delete the lot, you can use
rm -rf foo to delete a directory called foo and anything it contains. Again, be very
careful with rm; you can delete everything incredibly easily!
One final note regarding cp is that it will not copy directories recursively (i.e. copy
their contents as well) by default. For this, you need to pass the -r (recursive) option:
mkdir
touch
cp -r
ls -l
foo
foo/bar
foo bar
bar/bar
1.3 Ownership & Permissions
UNIX file permissions have three components, specifying which access rights a given
person has to a given file. They are broken down into user rights (for the owner of the
file), group rights (for the group which owns the file) and other rights (for everybody
else).
ls -l lists the permissions along with the user and group for each file. Permissions
are listed in the form -rwxrwxrwx, where r stands for read, w for write and x for execute.
The first group relate to user permissions, the second to group and the third to others.
For ordinary files, you should have read and write access for your user (and possibly
group) and no permissions for others. For executable (program) files, you should have
5
the execute permission set also. Directories require both read and execute to be set in
order to change into them (using cd).
File ownership can be changed with the chown and chgrp commands (to change user
and group, respectively). You’ll only be able to change ownership to users or groups
you have access to, i.e. yourself and any groups you are a member of. The -r option
to both commands applies the changes recursively. Permissions are changed with the
chmod command, though the format is far from intuitive.
There are two ways to use chmod. You can specify permission changes in the form
u+r (to add read permission for the user, for instance) or using a numeric notation.
Before discussing the numeric representation, here are some examples of the syntax just
described:
touch
chmod
ls -l
chmod
ls -l
chmod
ls -l
foo
u+rwx foo # Give user rwx
foo
go-rwx foo # Remove all permissions from group or others
foo
o+r foo # Give everyone read permission again
foo
It is also possible to change permissions with absolute values, instead of adding or
removing components. To do this, you compose a three digit octal number (don’t worry
too much about what that means, right now). The first digit refers to user permissions,
the second to group, and the third to other. To compose each digit, add together the
numbers below for the permissions you want, then stick the digits together:
4 Read (r)
2 Write (w)
1 Execute (x)
For example, to give the user rwx and the group rx, with no permissions for others,
you want user to have 4 + 2 + 1 = 7, group to have 4 + 1 = 5 and other to have 0, so the
permissions number is 750. Set this with chmod 750 foo.
Note that chmod takes the -r option to apply permissions recursively, too.
2 Text Editors
The topic of text editors is a hotly contested one in the Linux community. Everybody
has their own opinion of which is best. Clearly, the answer is that vim is best! Others
might try to tell you of an editor called Emacs. Don’t listen. Vim is best!
Vim does take a little getting used to, though, so in the mean time there are often a
whole load of graphical text editors installed in your distribution (look in the Applications list), and gvim which provides a graphical version of vim, along with some toolbar
and menu items to help you along. If you want to edit text from the command line, and
6
aren’t ready to dive into vim just yet, try pico or nano (one or both will be installed on
almost all Linux systems). A big advantage of using vim is that it is installed practically
everywhere, and on the few systems it isn’t available on, its predecessor, vi is always
present!
For more information on vim, talk to me; I could go on at length about how awesome
it is!
3 Shell Trickery
3.1 System State
To view a list of programs currently running (under your account), type ps ux. For
more information on the ps program, read man ps. ps tells you the user, process id,
CPU and memory use, start and run time of a job, and the name of the command as it
was initiated.
You can kill (end) a job that you’ve lost control of by typing kill -9 pid where pid
is the process id (second column in the ps ux output); don’t do this for things you can
close in other ways, though; it will almost certainly result in lost or unsaved data!
The commands pgrep and pkill can also be useful if you know the name of the
job you’re looking for the process id of. Read their man pages for more information
(man pgrep and man pkill).
3.2 Job Control
When you start a program from the terminal, by typing its name followed by any arguments it takes, the job will typically run in the foreground. That is, you won’t get
another shell prompt until the program ends. In order to run something in the background, returning you to a shell prompt so you can do something else while the job is
running, put an ampersand at the end of the command. For example,
xv # Runs the X viewer program, and ties up the terminal until you close it
xv& # Runs the X viewer program but returns you to a shell prompt
The jobs command allows you to see what is running in the background, and the
number it reports in square brackets is a job number. Use fg jobnumber to bring a job
back to the foreground.
Finally, you can terminate a job in the foreground by pressing Ctrl+C.
3.3 Pipes & Redirects
To redirect the output of a command into a file called foo, use the > symbol as follows:
ls -l > list.txt
To direct a file to the input of a program, use the < symbol,
7
cat < list.txt
These redirections work on the standard input and output streams of the program.
There is another output stream, called the standard error stream, which programs typically use to report errors or other status information not normally part of the output.
To capture this, use 2> as follows:
foo > output.txt 2> error.txt
To output both standard output and standard error to the same file, you can do
foo > output.txt 2>&1
which tells the shell to redirect standard output (stream 1) to output.txt, then standard
error (stream 2) to wherever stream 1 is going.
Finally, you can pipe the output of one program to the input of another. For instance,
the less program will buffer its input and let you scroll through using the arrrow keys
(or the space bar to move down a whole page). Try the following:
dmesg
Note that the output scrolls off the screen faster than you can read it. You might be
able to use the terminal’s scroll bar (if it has one) to go back up and see what it said,
but a better way is to run
dmesg | less
to give you control over the output.
Pipes can be used to send the output of one program to the input of another just
about anywhere, and lots of small command-line utilities exist to allow you to make use
of this. For example, the command below extracts your real name from the output of
the finger command applied to your username:
finger $(whoami) | grep Name | cut -d: -f3
Look at the manual pages for finger, grep and cut to see what they do, and try
running them in various combinations to extract other information!
4 Printing
To print a text, PostScript or PDF file from the commandline, you can use the lpr
command:
lpr -P PRINTERNAME filename
Hint: Try EPP_BLCKWHT2 as the printer name
To get better formatted plain-text printing, use a2ps (see its manual page for more
information):
a2ps -M A4 -P PRINTERNAME filename
To see the list of jobs waiting on a given printer, use lpq -P PRINTERNAME, and to
remove a job use lprm jobnumber, where jobnumber was the number reported by lpq.
8
Was this manual useful for you? yes no
Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Download PDF

advertisement