Linux Notes
Linux Notes
Linux is an operating system that evolved from a kernel created by Linus Torvalds when he was
a student at the University of Helsinki. To say that Linux is an operating system means that it's
meant to be used as an alternative to other operating systems, Windows, Mac OS, MS-DOS,
Solaris and others. Linux is not a program like a word processor and is not a set of programs like
an office suite. Linux is an interface between computer/server hardware, and the programs
which run on it.
Essentially, what an operating system does is provide a platform for everything else on your
computer to run on top of.
This platform is made up of lots of different parts. Some parts are responsible for making the
hardware work, others for displaying the user interface, and still other parts for ensuring that
applications can work with the hardware and each other. Just like Windows etc., Linux performs
all of these functions.
You may be asking, "If Linux does all the same things that Windows or Mac OS X does, why
bother switching?". The answer is that Linux has its own way of doing things, and for some
people, the Linux way suits them better.
It's modular and so infinitely flexible - for example, while Windows limits you to a single user
interface (what the system looks like), on Linux you can choose from a dozen different options.
It runs well on very old and slow hardware, as well as on super-computers and modern laptops
(by some counts, 90% of the world's super-computers are currently running a variant of Linux).
It's designed with security in mind, providing advanced features, such as strong encryption, as
standard.
Why Linux
Following are few reasons on why Linux is preferred
Stable & reliable operating system
OS crash, shut down, automatic restart, hang, slowing down is common for windows. However,
some operating systems can be so stable that most users never see their systems crash, even
after several years. This is true for Linux. You can go on like that for years without having any
trouble. Linux is a very stable operating system.
No Viruses
Your computer always has a risk for a virus. Linux hardly has any viruses. Linux makes it very
hard for this to happen. Most people use Windows, so the hackers keep on targeting Windows.
However, if anybody tries to make a virus for Linux, it will not work on Linux. The reason is Linux
doesn't allow any information deletion or system change without authorization from the user.
Whenever a new file has to do something with the system, an administrator password is
required (and if you're not an administrator on this system, you simply can't do it). So the
viruses can't just go around and delete or modify as they do not have the authorization for that.
Open source
Many of Linux's large quantity of programs are "open source," allowing the holder of the
software to improve the software in whatever way they want as long as credit is given.
2. What is BASH?
BASH = Bourne Again Shell
-
Bash is a shell written as a free replacement to the standard Bourne Shell (/bin/sh)
originally written by Steve Bourne for UNIX systems.
It has all of the features of the original Bourne Shell, plus additions that make it easier to
program with and use from the command line.
Since it is Free Software, it has been adopted as the default shell on most Linux systems.
\ vs. /:
Filenames:
The DOS world uses the eight dot three filename convention, meaning that all
files followed a format that allowed up to 8 characters in the filename, followed
by a period (dot), followed by an option extension, up to 3 characters long (e.g.
FILENAME.TXT). In UNIX/Linux, there is no such thing as a file extension. Periods
can be placed at any part of the filename, and extensions may be interpreted
differently by all programs, or not at all.
4. Special Characters
Before we continue to learn about Linux shell commands, it is important to know that there are
many symbols and characters that the shell interprets in special ways. This means that certain
typed characters: a) cannot be used in certain situations, b) may be used to perform special
operations, or, c) must be escaped if you want to use them in a normal way.
Character
\
Description
Escape character. If you want to reference a special character, you
must escape it with a backslash first.
Example: touch /tmp/filename\*
Current directory. Can also hide files when it is the first character in
a filename.
..
Parent directory.
[]
>
>>
<
&&
&
5. Executing Commands
The Command PATH
Most common commands are located in your shell's PATH, meaning that you can just
type the name of the program to execute it.
Example: Typing ls will execute the ls command.
Your shell's PATH variable includes the most common program locations, such as /bin,
/usr/bin, /usr/X11R6/bin, and others.
To execute commands that are not in your current PATH, you have to give the complete
location of the command.
Examples:
/home/bob/myprogram
./program (Execute a program in the current directory)
~/bin/program (Execute program from a personal bin directory)
Command Syntax
Commands can be run by themselves, or you can pass in additional arguments to make
them do different things. Typical command syntax can look something like this:
command [-argument] [-argument] [--argument] [file]
Examples:
Getting Help
When you're stuck and need help with a Linux command, help is usually only a few keystrokes
away. Help on most Linux commands is typically built right into the commands themselves,
available through online help programs (man pages and info pages), and of course online.
Using a Command's Built-In Help
Many commands have simple help screens that can be invoked with special command flags.
These flags usually look like -h or --help.
Example: grep -help
Online Manuals: Man Pages
The best source of information for most commands can be found in the online manual
pages, known as man pages for short. To read a command's man page, type man
command.
Examples:
man ls
To search for a particular word within a man page, type /word. To quit from a man
page, just type the Q key.
Sometimes, you might not remember the name of Linux command and you need to
search for it. For example, if you want to know how to change a file's permissions, you
can search the man page descriptions for the word permission like this:
man -k permission
If you look at the output of this command, you will find a line that looks something like:
chmod
- change file access permissions
Now you know that chmod is the command you were looking for. Typing man
chmod will show you the chmod command's manual page!
Info Pages
Some programs, particularly those released by the Free Software Foundation, use info pages as
their main source of online documentation. Info pages are similar to man page, but instead of
being displayed on one long scrolling screen, they are presented in shorter segments with links
to other pieces of information. Info pages are accessed with the info command, or on some
Linux distributions, pinfo (a nicer info browser).
For example: info df
- Loads the df info page.
Linux File-System
The Linux file system is a tree-like hierarchy of directories and files. At the base of the file
system is the / directory, otherwise known as the root (not to be confused with the root
user). Unlike DOS or Windows file systems that have multiple roots, one for each disk drive,
the Linux file system mounts all disks somewhere underneath the / filesystem. The following
table describes many of the most common Linux directories.
/bin
Description
The nameless base of the filesystem. All other directories, files,
drives, and devices are attached to this root. Commonly (but
incorrectly) referred to as the slash or / directory. The / is just a
directory separator, not a directory itself.
Essential command binaries (programs) are stored here (bash, ls,
mount, tar, etc.)
/boot
/dev
Device files. In Linux, hardware devices are accessed just like other
files, and they are kept under this directory.
/etc
/home
/lib
/proc
/root
/sbin
/tmp
/usr
/usr/bin
/usr/include
/usr/lib
/usr/local
Most user programs are kept here (cc, find, du, etc.)
Header files for compiling C programs.
Libraries for most binary programs.
Locally installed files. This directory only really matters in
environments where files are stored on the network. Locally-installed
files go in /usr/local/bin, /usr/local/lib, etc.). Also often used for
/usr/share
/usr/src
/usr/X11R6
/var
Program source code. E.g. The Linux Kernel, source RPMs, etc.
The X Window System.
Variable data: mail and printer spools, log files, lock files, etc.
DOS Command
cd
cd
cd, chdir
cd directory
cd directory
Description
Print Working Directory. Shows the current location in
the directory tree.
Change Directory. When typed all by itself, it returns
you to your home directory.
Change into the specified directory name.
Example: cd /usr/src/linux
cd ~
cd ..
cd ls
dir /w
ls directory
dir directory
ls -l
dir
ls -a
dir /a
ls -ld
directory
ls /usr/bin/d*
dir d*.*
DOS Command
Description
File
Cat
type
Tail
tail f
Cp
copy
Mv
rename,
ren, move
Rm
del
Mkdir
md
Rmdir
rd, rmdir
2. Finding Things
The following commands are used to find files. ls is good for finding files if you already know
approximately where they are, but sometimes you need more powerful tools such as these:
Linux Command
which
whereis
locate
find
Description
Shows the full path of shell commands found in your path. For
example, if you want to know exactly where the grep command is
located on the filesystem, you can type which grep. The output
should be something
like: /bin/grep
Locates the program, source code, and manual page for a command (if
all information is available). For example, to find out where ls and its
man page are, type: whereis ls The output will look something like:
ls: /bin/ls /usr/share/man/man1/ls.1.gz
A quick way to search for files anywhere on the filesystem. For
example, you can find all files and directories that contain the name
mozilla by typing: locate mozilla
A very powerful command, but sometimes tricky to use. It can be used
to search for files matching certain patterns, as well as many other
types of searches. A simple example is:
find . -name \*mp3
This example starts searching in the current directory . and all
subdirectories, looking for files with mp3 at the end of their names.
3. Informational Commands
The following commands are used to find out some information about the user or the system.
Linux Command
Explanation
ps
id
df
Report filesystem disk space usage (Disk Free is how I remember it)
du
top
free
cat /proc/cpuinfo
cat /proc/meminfo
uname -a
4. Other Utilities
Here are some other commands that are useful to know.
Linux Command
clear
Description
Clear the screen
echo
Display text on the screen. Mostly useful when writing shell scripts. For
example: echo Hello World
more
less
grep
Search for a pattern in a file or program output. For example, to find out
which TCP network port is used by the nfs service, you can do this:
grep .nfs. /etc/services
This looks for any line that contains the string nfs in the file
/etc/services and displays only those lines.
lpr
sort
su
Description
Scroll through your most recent commands. You can
scroll back to an old command, hit ENTER, and execute
the command without having to re-type it.
history command
TAB Completion
Complete recent commands with ! Try this: Type ! followed by the first couple of letters
of a recent command and press ENTER! For example,
type:
find /usr/bin -type f -name m\*
...and now type:
!fi
Search your command history with
CTRL-R