Introduction to GNU/Linux

UNIX concepts: filesystem, users, permissions and processes.
Using the shell. UNIX tools.
Accessing Windows/UNIX from UNIX/Windows.

Last significant update : 1 July 2004

1 Overview of GNU/Linux

2 UNIX Philosophy

3 UNIX Flavors

There are many UNIX flavors. Mainly:

GNU/Linux is a POSIX compliant complete rewrite of Unix, and a blend of System V and BSD.

4 Linux Kernel

Kernel of an operating system

An operating system is a complex system, made up of a large number of components, or software layers. Ideally, these components are independant from each other, so that the large complex system becomes a set of smaller and more simple components.

The kernel of an operating system is its core component ; or its bottom layer, the interface with hardware.

The Linux kernel:

Version numbers:

5 Shell

The Shell

Text mode / command line (instructions are given line by line) user interface:

The shell is much more flexible and powerful than a graphical user interface.

6 Various Shells

7 X Window System

The X Window system, or X, or X11:

8 Window Managers and Desktop Environments

Graphical Interface
Window Manager
Desktop Environment

There is no clear distinction between desktop environments and window managers.

The project works on interoperability and shared technology for desktop environments for the X Window System.

9 The Gnome and KDE Desktop Environments

Desktop environments like Gnome and KDE provide a platform for software applications development, and a large and increasing number of applications with a common look and feel.

GNU Network Object Model Environment (GNOME)
K Desktop Environment (KDE)

10 Switching between graphical and text mode

GNU/Linux is configured by default to have 7 virtual consoles, with X Window running on the seventh (when it is running).

Switching from X Window to one of the 6 consoles: Ctrl + Alt + F1, ..., Ctrl + Alt + F6.

Switching from a text console to another text console: Alt + F1, ..., Alt + F6.

Switching back to X Window: Alt + F7.

11 Bash Basics

12 Logging in, Session, Logging out

To use the system, a user must have an account on it. Basically, an account is a login, a password, and a personal home directory.

A user identifies himself to the system by typing his login and password when they are requested (by a virtual console, or by a graphical program called a display manager):

Logging in is the beginning of a user session. Sessions end when users log out.

Terminate a login shell.
Terminate a login or a non login shell.

13 Prompts

The prompt is a character or a character sequence displayed by the shell when it is waiting for the user's instructions.

14 Command Syntax

command [options] [arguments]

Convention: Operands which are enclosed in square brackets ( [ ] ) are optional

Most commands have many options.

15 Simple Commands

Print current user's name.
Change user password.
Display a line of text.
Display or set the system date and time.
cal or ncal
Display a calendar.
Clear the terminal screen.
Print information about the current kernel.

16 Aliases

List aliases, or define an alias.
alias ls='ls --color'
alias rm='rm -i'

17 Use Online Help

Many commands have a --help option.

Display list of Bash builtin commands.
Interface to manual pages.
Interface to info documents.
Display manual page descriptions.
apropos or man -k
Search the manual pages' names and descriptions.

And you are free to study and use existing Free code.

18 Man Pages

A manual (reference) page may have the following sections: NAME, SYNOPSIS, DESCRIPTION, OPTIONS, USAGE, ENVIRONMENT, FILES, SEE ALSO. Manual pages are also organised into sections (or directories in /usr/share/man/):

man pages sections
Section Type of pages
1 Executable programs or shell commands.
2 System calls (functions provided by the kernel)
3 Library calls (functions within program libraries)
4 Special files (usually found in /dev)
5 File formats and conventions eg /etc/passwd
6 Games
7 Miscellaneous (including macro packages and conventions).
8 System administration commands (usually only for root)
man 1 crontab
man 5 crontab

19 Info Documents

info info
info --apropos string

20 Useful Key Combinations

Key Combination Effect
Ctrl + C Terminate a running program.
Ctrl + D Equivalent to exit or logout. EOF (End Of File) character.
Ctrl + L Equivalent to clear.
Ctrl + Z Suspend current foreground process (Job Control).
Shift + Page Up and Shift + Page Down Browse terminal buffer (scroll up or down).
Ctrl + Alt + Backspace Kill X Window.

21 Special Characters

Shell Special Characters
Character Meaning or Use
newline space tab Word separators
; Command separator
& Background execution
( ) Command grouping (subshell)
{ } Command block
| > < & Redirections
* ? [ ] ~ ! Filename metacharacters
` Command substitution
$ Variable (or command) substitution
# Comment
\ " ' Escape or quote other characters

22 Command Line Features

23 Command Line Editing

Keys to move the cursor on the command line
Key Action
Home Move the cursor at the beginning of the command line.
End Move the cursor at the end of the command line.
Arrow Left Move the cursor one character to the left.
Arrow Right Move the cursor one character to the right.
List or edit and re-execute commands from the history list.

24 Command Line History

Key (combination) Action
Arrow Up Move up
Arrow Down Move down
Ctrl + R Search command history.
Event designators, and lines they reference in the history list.
Event designator Reference
!n Refer to command line n.
!-n Refer to the current command line minus n.
!! Refer to the previous command (same as !-1).
!string Refer to the most recent command starting with string.
!?string? Refer to the most recent command containing string.
^string1^string2^ Repeat the last command, replacing string1 with string2.

man history to know more and learn about Word Designators and Modifiers.

25 Command and File Completion

You don't have to type the full names of commands or files. Bash can complete the names for you.

Complete command name or file name.
Show command or file completion possibilities.

26 Filename Expansion

zero or more characters
exactly one character
exactly one character listed
exactly one character in the given range
any character that is not listed
any character that is not in the given range
exactly one entire word in the options given

27 Filesystem

28 Everything is a File

The system's operations on a file, a directory, a CD drive, ..., a screen, are fundamentally identical.

Files and directories, physical devices, and even streams of data or network connections are files (of a different type), so that:

29 Filesystem File Types

File types and their corresponding character in the first column of the output of ls -l.
File Type First column of ls -l output
Regular file -
Block device b
Character device c
Directory d
Symbolic link l
FIFO (named pipe) p
Socket s

30 File Attributes

The system stores information about files in a structure known as an inode. This information includes:

and file permissions:

Display file or filesystem status.

31 The Filesystem Tree

The Directory Tree

/ is the root directory.

32 Path to a File

Every directory always contains two special files:

Current directory
Parent directory (of the current directory).
Absolute Path to a File


Relative Path to a File

Relative to the current directory.

./directory/file.txt is the same as directory/file.txt or ../user/directory/file.txt when the current directory is /home/user/.

33 Filesystem Commands

34 Exploring the Filesystem

Change (current) Directory.
Report filesystem disk space usage.
Estimate file space usage.
List directory contents.
Print Working (current) Directory.

35 Finding Files

Search for files in a directory hierarchy.
List files in database that match a pattern. The database is periodically updated by updatedb.
Locate a command.
Locate the binary, source, and manual page files for a command.

36 Creating, Copying, Moving and Removing Files

copy files and directories
Make (create) directory
Move and/or rename files.
Remove (delete) files or directories.
Remove empty directories.
Change file timestamps.

Make links between files. By default, it makes "hard links". With the -s option, it makes symbolic (or "soft") links.

A "hard link" is another name for an existing file. The link and the original are indistinguishable (they share the same inode). Caution: the link can be removed without removing the file with unlink, but rm will remove the link and the file.

A symbolic link, on the other hand, refers to a different file. Most operations (opening, reading, writing, and so on) are passed the symbolic link file, the kernel automatically "dereferences" the link and operates on the target of the link. But some operations (e.g. removing) work on the link file itself, rather than on its target.

38 Filesystem Mounting

39 Filesystem Types

A filesystem is the methods and data structures that an operating system uses to keep track of files on a disk or partition; that is, the way the files are organized on the disk.

Filesystem Types
Filesystem Description
ext2 Linux native filesystem.
ext3 ext2 plus journaling.
reiserfs Robust and journaling filesystem.
iso9660 The standard CD-ROM filesystem.
fat and vfat Microsoft Windows fat and fat32.
ntfs Microsoft Windows (NT, ...)
Flush filesystem buffers to disk.

40 Mounting

To gain access to files on a device, you must first tell it where in the directory tree you would like those files to appear (mount point). This process is called mounting a filesystem.

Mount a filesystem.
Unmount a filesystem.
Mounting and unmounting a device (or a partition)

The system administrator can control which file systems are mounted at boot time, pre-determine the mount points for specific file systems, and allow some file systems to be mounted by normal users. This is configured in /etc/fstab, which includes the following fields:

  1. device
  2. mount point
  3. filesystem type
  4. options

41 Devices

First partition of the master IDE drive on the primary IDE controller.
Second partition of the master IDE drive on the primary IDE controller.
First partition of the slave drive on the secondary IDE controller.
First partition of the first SCSI device.
First partition of the second SCSI device.
Primary floppy drive
CD drive.
Garbage black hole. Anything sent to /dev/null will disappear.

42 File Names and Extensions

43 File Extensions

Files may contain (human readable) text, binary data, or both. Files may be meant to be executed, processed by an application, or edited in a simple text editor.

A file does not need any extension (like a .exe suffix) to be executed (it needs execution permission).

Applications may need to identify the type of files. File extensions are often used for this purpose.

Determine file type.

44 File Names

However, file names should only contain the following characters:
"a", ... "z", "A", ... "Z", "0", ... "9", "-", "_", "." because:

For example, in a graphical user interface, one can click on a file name which contains a space character; but in a shell, such a file has to be written file\ name\ with\ spaces.txt or "file name with spaces.txt" or 'file name with spaces.txt'.

45 Regular Files Special Names

46 Input/Output Redirection

47 Input/Output

Standard Input, Standard Output and Standard Error

In general, a command (a program):

These three are files, and are always open. As all open files, they are assigned to a file descriptor (an integer).

File descriptors for stdin, stdout and stderr.
File File descriptor
/dev/stdin or /dev/fd/0 0
/dev/stdout or /dev/fd/1 1
/dev/stderr or /dev/fd/2 2

48 Redirections

Capturing output from a file or a command (or program or script) and sending it as input to another file or command (or program or script).
(pipe) redirects standard output of a program as standard input of another.
redirects standard output to a file (and overwrites the file if it already exists).
uses a file as standard input.
Appends standard output to a file.
< Input_data_file  program1 | program2 > Output_data_file

A "filter" is a program which reads data from standard input, processes it in some way, and sends the processed data to standard output.

49 Redirections Examples

2> file
Direct standard error to file
2>> file
Append standard error to file.
Print standard error messages to standard output.
Print standard output messages to standard error.

50 Building Command Lines From Standard Input

Build and execute command lines from standard input.

Read remaining arguments from standard input instead of specifying them and execute the command.

cd /bin
ls | xargs whatis

51 Viewing and Editing Files

52 Viewing Files: Pagers

Pager with more features than more.

53 Editors

vi is a powerful text editor, available on any UNIX like system. vim stands for Vi IMproved. With gvim or kvim, you can use the mouse.

emacs is another powerful programmers text editor, and much more.

There are others:

54 VI Essentials

vi(m) basically has two modes of operation:

Command mode is the initial mode. The commands you type are displayed in the status (bottom) line (only some of them in vi). They are executed with Enter.

Press a, A, i, I, o or 0 to enter Insert mode. Press Esc to return to command mode.

vimtutor is an interactive vim tutorial (25 to 30 minutes).

55 File Commands

56 File Information

Determine file type.
Display file or filesystem status.
Print the number of newlines, words, and bytes in files.

57 Archives and Backups

GNU version of the tar archiving utility.
Compress or expand files.
Compress or expand files.
A block-sorting file compressor.
A block-sorting file compressor.
Package and compress (archive) files.
List, test or extract compressed files in a ZIP archive.
Extract files from rar archives.
Remote copy and update.

58 Comparing Text Files

Compare (two) files line by line.
Apply a diff file to an original.
Compare gzip compressed files.
Compare bzip2 compressed files.
Compare three files line by line.
Produce a pretty comparison between files.
Pretty-print diff outputs with GNU enscript.
Display word differences between text files.
Side-by-side merge of file differences.
Compare two sorted files line by line.

59 Checking File Integrity

Checksum and count the bytes in a file.
Generates or checks MD5 message digests.
Generates or checks SHA1 message digests.

60 Concatenating, Splitting and Joining Files

Concatenate files and print on standard output.
Concatenate and print files in reverse.
Reverse the order of characters in every line.
Split a file into pieces.
Split a file into sections determined by context lines.
Join lines of two files on a common field.

61 Sorting and Removing Duplicate Lines

Sort lines of text files.
Remove duplicate lines from a sorted file.
cat *.txt | sort | uniq > result-file

62 Dumping Files

output the first part of files.
output the last part of files.
Dump files in octal and other formats.
ASCII, decimal, hexadecimal, octal dump.

63 UNIX Tools

64 Regular expressions

Filename expansion belongs to the shell. There is a more general and widely used tool for matching strings, called regular expressions. There are mainly three flavors:

Regular Expressions
Expression Match
. Matches any single character.
* Matches the last character or expression 0 or more times.
+ Matches the last character or expression 1 or more times.
? Matches the last character or expression 0 or 1 time.
^ Matches the beginning of a line.
$ Match the end of a line.
[abcde] Matches exactly one character listed.
[a-e] Matches exactly one character in the given range.
[^abcde] any character that is not listed
[^a-e] any character that is not in the given range
\ Escapes special characters.

65 Filters

Print lines matching a pattern.
A grep with Perl-compatible regular expressions.
Search a file for a string or regular expression, with approximate matching capabilities.
Convert tabs to spaces.
Convert spaces to tabs.
Translate or delete characters.
Convert text file line endings between Unix and DOS formats.
Remove sections from each line of files.
Merge lines of files.

66 Text Formatting and Pretty Printing

Wrap each input line to fit in specified width
Simple optimal text formatter.
(same as cat -n)
Convert text files for printing
GNU a2ps is a filter which generates PostScript from various formats, with pretty-printing features, strong support for many alphabets, and customizable layout.

67 sed

Stream editor. Filter text in a pipeline.


sed "1,32 d" file.txt
Delete lines 1 through 32
sed 's/[ ^t]*$//' file.txt
Delete trailing whitespace (spaces/tabs) from end of each line.
Delete both leading and trailing whitespace from each line.
sed 's/^[ ^t]*//;s/[ ^t]*$//' file.txt
sed 's/foo/bar/g' file.txt
Replace all instances of "foo" with "bar".
sed '/^Delete/d' file.txt
Delete lines starting with "Delete".
sed --in-place 's/^\(.\{20\}\).*/\1/g' file
Replace all lines with only their first 20 characters.

68 There's more than one way to do it

sed emulating UNIX commands
UNIX command sed equivalent
cat sed ':'
tac sed '1!G;h;$!d'
grep sed '/patt/!d'
grep -v sed '/patt/d'
head sed '10q'
tail sed -e ':a' -e '$q;N;11,$D;ba'
tail -f sed -u '/./!d'
cut -c 10 sed 's/\(.\)\{10\}.*/\1/'
cut -d: -f4 sed 's/\(\([^:]*\):\)\{4\}.*/\2/'
tr A-Z a-z sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/'
wc -l sed -n '$='
uniq sed 'N;/^\(.*\)\n\1$/!P;D'
rev sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//'
basename sed 's,.*/,,'
dirname sed 's,[^/]*$,,'
cp orig new sed 'w new' orig

69 Users and Permissions

70 The root User

The super user, root can do anything he wants, including erasing the system.

An average user does not have the necessary permissions to modify any system file. Any software he uses can't either, as it inherits the user's permissions (alhough there are exceptions).

The use of the system as the root user must be strictly limited to system administration tasks.

71 Users and Groups

A running program (a process) has the same permissions as the user who executes it.

There are exceptions to this rule, but it explains why there are more users of the system than root ant its real users (see /etc/passwd).

Users usually belong to one or more groups (see /etc/group).

Users and groups names are human-friendly identifiers. The systems uses integers: UIDs and GIDs.

Print real and effective UIDs and GIDs.
Change user ID or become super-user.

Every file has:

72 Permissions

There are three sorts of permissions:

And each file has permissions defined for three types of users:

They appear as follows in colums 2 to 10 of the output of ls -l. The letter is replaced by a hyphen (-) where the permission is not set.

user group others
r w x r w x r w x

73 Octal and Binary Modes

Permission Read Write eXecute
Letter r w x
Octal valuee 4 2 1
Octal digit Text equivalent Binary value Meaning
0 --- 000 No permission
1 --x 001 Execute
2 -w- 010 Write
3 -wx 011 Write + Execute
4 r-- 100 Read
5 r-x 101 Read + Execute
6 rw- 110 Read + Write
7 rwx 111 Read + Write + Execute

74 File and Directory Permissions

File and Directory Permissions
Permissions on a File on a Directory
Read Read the file. List the contents of the directory.
Write Modify the file (can be emptied, but not deleted) Create, delete or rename files in the directory.
Execute Execute the file (the program) Access to the contents of the directory. Read, write to or execute a file in the directory.

75 Special Permissions

Special permissions on files and directories: SetUID, SetGID and Sticky bit.
Special Permissions on a File on a Directory
SUID or Set User ID A program is executed with the file owner's permissions (rather than with the permissions of the user who executes it). Files created in the directory inherit its UID.
SGID or Set Group ID The effective group of an executing program is the file owner group. Files created in the directory inherit its GID.
Sticky (bit) A program sticks in memory after execution. Any user can create files, but only the owner of a file can delete it.
Permissions as output in columns 2 to 10 of
ls -l and their meaning.
Permissions Meaning
--S------ SUID is set, but user (owner) execute is not set.
--s------ SUID and user execute are both set.
-----S--- SGID is set, but group execute is not set.
-----s--- SGID and group execute are both set.
--------T Sticky bit is set, bot other execute is not set.
--------t Sticky bit and other execute are both set.

76 Default Permissions

System calls to create new files and directories have default or base permissions:

The first octal digit is for special permissions. The next three are for user, group and others.

Before creating the file or directory, the base permissions are compared to a mask that will "mask out" permission bits to determine the final permissions for the object being created.

The calculation to determine the final permissions is to take the binary of the base permissions and perform a logical AND operation on the ones complement representation of the binary umask.

Display or set file creation mask.

The usual umask is 0022, which results in 0755 for directories, and 0644 for regular files.

  0  0  2  2         0  7  7  7         0  6  6  6
000000010010       000111111111       000110110110
111111111111       111111101101       111111101101
------------       ------------       ------------
111111101101       000111101101       000110100100
                     0  7  5  5         0  6  4  4

77 Setting Permissions

change permissions of a file
change file owner and group
change group ownership
chmod u+x file
chmod 744 file

78 Shell Variables and Custimization

79 Local and Environment Variables

Variables are prefixed by a dollar sign ($). A value is assigned to a variable through an equals sign (=) (no whitespace on either side of it).

MY_VARIABLE='my value'

By default, variables are seen only within the shell itself. To pass variables to other programs invoked within the shell, they have to be exported to the environment.

Display ot set shell variables.
Display environment variables.
Run a program in a modified environment.
Export a local variable to the environment.

80 Environment Variables

The behaviour of many programs can be altered by environment variables.

Important environment variables.
Variable Meaning
PATH Colon-separated list of directories to search for each command.
EDITOR Default editor.
DISPLAY In the X window system, a display consists (simplified) of a keyboard, a mouse and a screen.
PS1 Prompt displayed before each new command.


You have a X Window server running on your machine, at hostname. You can run a client application on a remote machine, at rhostname, and view it on the local machine.

On the remote client:

export DISPLAY=hostname:0

(or in general hostname:D.S where S is the screen and D is the display.)

On the server:

xhost + rhostname
xhost -

Xhost is a very insecure mechanism. Use ssh -X to do this more easily and securely.

82 Bash Configuration Files

The systemwide initialization file, executed for login shells.
The systemwide per-interactive-shell startup file.
~/.bash_profile, ~/.bash_login or ~/.profile
The personal initialization file, executed for login shells.
The individual per-interactive-shell startup file.

83 Processes

84 Process Types

85 Process Attributes

86 Monitoring Processes

Display a dynamic real-time view of a running system.
Report a snapshot of the current processes.
Display a tree of processes.
Run programs and summarize system resource usage.

87 Killing Processes

A program can be killed by sending it a termination signal.

Send a signal to a process.
Kill processes by name.
Kill a client by its X resource.

kill -15 is the default, and orders the process to terminate in an clean way.

kill -9 sends a termination signal which can't be ignored.

88 Process Priority

Process priority is calculated from the nice number, and recent CPU usage of the process.

Run a program with modified scheduling priority.
Alter priority of running processes.

89 Job Control Commands

Job control means switching between several jobs or processes.

A foreground job occupies the terminal which initialized it. It can be put in the background, so that the terminal can accept new commands.

command &
Run command in the background.
Ctrl + C
Terminate a process running in the foreground.
Ctrl + Z
Suspend a process running in the foreground.
Reactivate a suspended program in the background.
Place a job in the foreground, and make it the current job.
Lists processes in the background. Each one has a number n and can be referred to by %n.

90 Scheduling

91 Scheduling Occasional Tasks

Executes commands at a specified time.
at hh:mm mm/dd/yy
at> echo "File created using the at utility" > atfile
at> ^D
at now + 1 hour
Lists the user's pending jobs. Equivalent to at -l.
Deletes jobs, identified by their job number.
Executes commands when system load levels permit.

92 Scheduling Repetitive Tasks

The cron daemon examines crontab files every minute, and checks if the command in each line should be run in the current minute.

Maintain crontab files.
crontab -e
minute hour day_of_month month day_of_week command
  1. minute (0 to 59)
  2. hour (0 to 23)
  3. day of month (1 to 31)
  4. month (1 to 12, or jan, feb, etc)
  5. day of week (0 to 7, 0 or 7 is Sunday, or sun, mon, etc)
  6. command (or script)
  7. newline

93 Scripting

94 Special Characters

Shell Special Characters
Character Meaning or Use
newline space tab Word separators
; Command separator
& Background execution
( ) Command grouping (subshell)
{ } Command block
| > < & Redirections
* ? [ ] ~ ! Filename metacharacters
` Command substitution
$ Variable (or command) substitution
# Comment
\ " ' Escape or quote other characters

95 Hello World


echo Hello World 

The first line can be ommitted to run the script using a non interactive shell: bash

Execute permission must be set to simply run it: ./

If the directory which contains the file is in the PATH environment variable, you don't need the path:

96 Accessing Windows/UNIX from UNIX/Windows

97 Character Code Issues

Character codes map characters to numbers encoded as one or more bytes.

ASCII is the safest character repertoire in data transfer.
ISO Latin 1 or ISO 8859-1
Western European languages.
ISO Latin 9 or ISO 8859-15
Slight modification of ISO8859-1 to includes the Euro symbol.
ISO 10646
International standard which defines UCS.
The more practical definition of UCS. Unicode imposes additional constraints on implementations to ensure that they treat characters uniformly across platforms and applications.

98 Newlines

Operating System Hexadecimal Newline Character(s)
Microsoft Windows 0D 0A
Apple Macintosh OS 9 and earlier 0D
UNIXes, GNU/Linux, Mac OS X and higher 0A
Convert text file line endings between Unix and DOS formats.

99 Solutions

100 More Information

101 Use Online Help

Many commands have a --help option.

Display list of Bash builtin commands.
Interface to manual pages.
Interface to info documents.
Display manual page descriptions.
apropos or man -k
Search the manual pages' names and descriptions.

And you are free to study and use existing Free code.

102 Resources