LINUX BASICS CDAC Mumbai 1
Sep 27, 2015
LINUX BASICS CDAC Mumbai 1
Introduction to Linux The Linux operating system (OS) was
first coded by a Finnish computer
programmer called Linus Benedict
Torvalds in 1991, when he was just
21! He had got a new 386, and he
found the existing DOS and UNIX too
expensive and inadequate.
As a hobby he decided to build his own tiny OS
In those days, a UNIX-like tiny, free OS called Minix was extensively used for
academic purposes. Since its source
code was available, Linus decided to
take Minix as a model.
2
GNU PROJECT
Established in 1984 by Richard Stallman, who believes that
software should be free from restrictions against copying or
modification in order to make better and efficient computer
programs
3
GNU is a recursive acronym for GNU's Not Unix
Aim at developing a complete Unix-like operating system which is free for copying and modification Companies make their money by maintaining and distributing the software, e.g. optimally packaging the software with different tools (Redhat, Slackware, Mandrake, SuSE, etc)
Stallman built the first free GNU C Compiler in 1991. But still, an OS was yet to be developed
Introduction to Linux Linus liked the endeavours of the Free Software Foundation and released his kernel under the GNU GPL
The Linux kernel and GNU tools made a complete, free operating system: the GNU/Linux operating system
Linux most commonly distributed with the toolset & collection of application called as distributions.(Redhat, Fedora, Ubuntu, Mandrake etc)
The largest part of the work on Linux is performed by the community: the thousands of programmers around the world that
use Linux and send their suggested improvements to the
maintainers.
Various companies have also helped not only with the development of the Kernels, but also with the writing of the body
of auxiliary software, which is distributed with Linux. 4
WHY LINUX ?
A Linux distribution has software worth thousands of dollars, for
virtually no cost
Linux operating system is reliable, stable, and very powerful
Linux comes with a complete development environment, including
compilers, toolkits, and scripting languages
Linux comes with networking facilities, allowing you to share
hardware
Linux utilizes your memory, CPU, and other hardware to the fullest
A wide variety of commercial software is also available
Linux is very easily upgradeable
Supports multiple processors as standard
True multitasking. So many apps, all at once 5
FILE SYSTEM OF LINUX
The Linux File system is organized
hierarchically.
6
LINUX FILE SYSTEM
Some important directories are
/bin contains commonly used commands.
/usr/bin contains less commonly used commands.
/mnt provides a location for mounting devices.
/etc contains system administration commands.
/dev contains device files.
/usr contains users directories
/opt addon application software packages.
7
LINUX FILE SYSTEM
/proc processor related files.
/home This is where user home directories are stored.
/root This is the root (administrator) user's home directory
/sbin Binaries which are only expected to be used by the
super user.
/tmp Temporary files.
/boot Has the bootable Linux kernel and boot loader
configuration files(GRUB).
/var This directory is used to store files which change
frequently
8
BASIC LINUX COMMANDS
File Handling
Text Processing
System Administration
Process Management
Archival
File Systems
9
SOURCES TO LEARN COMMANDS??
Primary man(manual) pages. man shows all information about the
command help shows the available options for that
command Secondary Books and Internet
10
FILE HANDLING COMMANDS
mkdir make directories
Usage: mkdir [OPTION] DIRECTORY...
eg. mkdir prabhat
rmdir remove directory
eg. rmdir prabhat
ls list directory contents
Usage: ls [OPTION]... [FILE]...
eg. ls, ls l, ls prabhat
cd changes directories
Usage: cd [DIRECTORY]
eg. cd prabhat
pwd print name of current working directory
Usage: pwd
11
FILE HANDLING COMMANDS(CONTD)
cp copy files and directories
Usage: cp [OPTION]... SOURCE DEST
eg. cp sample.txt sample_copy.txt
cp sample_copy.txt target_dir
mv move (rename) files
Usage: mv [OPTION]... SOURCE DEST
eg. mv source.txt target_dir
mv old.txt new.txt
rm remove files or directories
Usage: rm [OPTION]... FILE...
eg. rm file1.txt , rm -rf some_dir
12
FILE HANDLING COMMANDS(CONTD)
find search for files in a directory hierarchy
Usage: find [OPTION] [path] [pattern]
eg. find file1.txt find name file1.txt
history prints recently used commands
Usage: history
13
TEXT PROCESSING COMMANDS
cat concatenate files and print on the standard output
Usage: cat [OPTION] [FILE]...
eg. cat file1.txt file2.txt
cat -n file1.txt
echo display a line of text
Usage: echo [OPTION] [string] ...
eg. echo I love India
echo $HOME
grep print lines matching a pattern
Usage: grep [OPTION] PATTERN [FILE]...
eg. grep i apple sample.txt
14
TEXT PROCESSING COMMANDS (CONTD)
wc print the number of newlines, words, and bytes in files
Usage: wc [OPTION]... [FILE]...
eg. wc file1.txt
wc -l file1.txt
sort sort lines of text files
Usage: sort [OPTION]... [FILE]...
eg. sort file1.txt
sort r file1.txt
-r reverses the result
15
LINUX FILE PERMISSIONS
3 types of file permissions read, write, execute
10 bit format from 'ls -l command
1 2 3 4 5 6 7 8 9 10
file type owner group others
eg. drwxrw-r--
means owner has all three permissions, group has
read and write, others have only read permission
read permission 4, write 2, execute 1
eg. rwxrw-r-- = 764
673 = rw-rwx-wx
16
SYSTEM ADMINISTRATION
chmod change file access permissions
Usage: chmod [OPTION] [MODE] [FILE]
eg. chmod 744 calculate.sh
chown change file owner and group
Usage: chown [OPTION]... OWNER[:[GROUP]] FILE...
eg. chown remo myfile.txt
su change user ID or become superuser
Usage: su [OPTION] [LOGIN]
eg. su remo, su
17
SYSTEM ADMINISTRATION( CONTD)
passwd update a users authentication tokens(s)
Usage: passwd [OPTION]
eg. passwd
who show who is logged on
Usage: who [OPTION]
eg. who
18
PROCESS MANAGEMENT
ps report a snapshot of the current processes
Usage: ps [OPTION]
eg. ps, ps el
Top - Show system usage statistics
kill to kill a process(using signal mechanism)
Usage: kill [OPTION] pid
eg. kill -9 2275
killall - Stop a program. The program is specified by
command name.
19
ARCHIVAL COMMANDS
tar to archive a file
Usage: tar [OPTION] DEST SOURCE
eg. tar -cvf /home/archive.tar /home/original
tar -xvf /home/archive.tar
zip package and compress (archive) files
Usage: zip [OPTION] DEST SOURSE
eg. zip original.zip original
unzip list, test and extract compressed files in a ZIP archive
Usage: unzip filename
eg. unzip original.zip 20
FILE SYSTEM COMMANDS
fdisk partition manipulator
eg. sudo fdisk -l
mount mount a file system
Usage: mount -t type device dir
eg. mount /dev/sda5 /media/target
umount unmount file systems
Usage: umount [OPTIONS] dir | device...
eg. umount /media/target
21
FILE SYSTEM COMMANDS(CONTD)
du estimate file space usage
Usage: du [OPTION]... [FILE]...
eg. du
df report filesystem disk space usage
Usage: df [OPTION]... [FILE]...
eg. df
22
LINUX COMMAND SUMMARY
Command Function Example
ls To see the files in the directory ls
ls -l display details of the files such as permissions, size date of last update
ls -l
ls -a To list hidden files ls -a
cat To display contens of one or more files
cat hello
cp Copy file1 to file2 cp file1 file2i
rm or rm-i To delete a file rm hello
mv To rename a file mv hello mytextfile
head To display top 10 lines of the file Head hello
tail To display bottom 10 lines of file tail hello
wc To display number of lines,words & characters in a file
wc hello
nl To display file with line numbers nl hello
23
LINUX COMMAND SUMMARY
Command Function Example
pwd To know the present working directory pwd
cd To change directory, you can specify either relative or absolute path
cd /etc , cd .. cd ./cprograms
mkdir To create a new directory mkdir hello, mkdir /usr/prasad/hello
rmdir To delete a directory rmdir hello
find To search for file find hello
du To know the directory size in number of blocks
du prasad
df To know the disk space occupied by the total file system
df
24
INPUT / OUTPUT REDIRECTION
In linux all devices are considered as files, hence we have three standard files Stdin file desc. 0
Stdout file desc. 1
Stderr file desc. 2
Input is given through the keyboard, called standard i/p: stdin
Output is displayed on the monitor, called standard o/p: stdout
The error messages are also displayed on the monitor, called standard error: stderr
25
INPUT / OUTPUT REDIRECTION Instead of taking input from the keyboard, you can
take the i/p from a file known as input indirection.
$wc < filename (< input indirection)
Similarly, the o/p can be redirected to a file instead of
monitor.
$ls -l > filelist (> output redirection)
$cat filelist
you can combine both input indirection & o/p
redirection in one command
$wc file2
$cat file2>>file1 (can append a file to another file)
26
VI EDITOR
To create text files or C/C++ programs, you need a text editor.
The most commonly used editors are ed , Vi, vim etc.
27
STARTING VIM
You may use vim to open an already existing file by typing
vim filename
vim has three modes:
command mode
insert mode
Colon mode(Ex Command Mode)
In command mode, the letters of the keyboard perform editing
functions (like moving the cursor, deleting text, etc.).
To enter command mode, press the escape key. In
insert mode, the letters you type form words and sentences.
To enter Ex-command mode Press esc and :
28
Entering Text
In order to begin entering text in this empty file, you must
change from command mode to insert mode. To do this,
type i
Deleting Words
To delete a word, move the cursor to the first letter of the
word, and type
dw
This command deletes the word and the space following it.
To delete three words type
3dw
29
Deleting Lines
To delete a whole line, type
dd
Typing dd deletes the entire line containing the cursor
and places the cursor at the start of the next line.
To delete two lines, type
2dd
To delete from the cursor position to the end of the line, type
D (uppercase)
Moving around in a file
H to top line of screen
M to middle line of screen
L to last line of screen
G to last line of file
1G to first line of file
30
Moving by Searching
To move quickly by searching for text, while in
command mode:
Type / (slash).
Enter the text to search for.
Press .
The cursor moves to the first occurrence of that text.
To repeat the search in a forward direction, type
n
To repeat the search in a backward direction, type
N
31
To save the edits you have made, but leave vim
running and your file open:
Press .
Type :w
Press .
To quit vim, and discard any changes your have made
since last saving:
Press .
Type :q!
Press .
32