7. Linux Basics
7. Linux Basics
UNIX STRUCTURE
A file system is a logical collection of files on a partition or disk. A partition is a container for information
and can span an entire hard drive if desired. One file system per partition allows for the logical
maintenance and management of different file system. Everything in UNIX is considered to be a file.
The main concept that unites all versions of UNIX is the following: -
1. Kernel: The kernel is the heart of the operating system. It interacts with hardware and most of
the tasks like memory management, task scheduling and file management.
2. Shell: The shell is the utility that processes your requests. When you type in a command at your
terminal, the shell interprets the command and calls the program that you want. The shell uses
standard syntax for all commands. C Shell, Bourne Shell and Korn Shell are most famous shells
which are available with most of the UNIX variants.
3. Commands and Utilities: There are various command and utilities which you would use in your
day to day activities. cp, mv, cat and grep etc. are few examples of commands and utilities.
There are over 250 standard commands plus numerous others provided through 3rd party
software. All the commands come along with various optional options.
4. Files and Directories: All data in UNIX is organized into files. All files are organized into
directories. These directories are organized into a tree-like structure called the file system.
The UNIX has two types of accounts Super user account and User account. A user account must be
created by super user. A user account has following information: - login name, password, group
identification, home directory and login shell. The directories have specific purposes and generally hold
the same type of information for easily locating files. The directories are:
DIRECTORY DESCRIPTION
/ This is the root directory.
/bin Here all executable files are located. They are available to all users.
/dev Here device drivers are located.
/etc Contains various commands & files which are used for system administration.
/lib Contains shared library files.
/boot Contains files for booting the system
/home Contains the home directory for users and other accounts.
/mnt Used to mount temporary file system such as cd rom, floppy disk.
/tmp Hold temporary files used between system boot and other uses.
/usr It contains other directories. Among these are home directory for each user.
/var Typically contains variable length files such as log and print files.
/proc Contains all processes marked as a file by process no or dynamic information.
/sbin Contains binary executable files, usually for system administration.
/kernel Contains kernel files (core of UNIX system)
The UNIX disk is divided into four logical parts:
BLOCK BOOT SUPER BLOCK INODE LIST DATA BLOCK
Inode contains information of file except the data. The things are:
✓ Owner
✓ Size
✓ Permission
✓ No of link
✓ File type
✓ Date last modified
✓ Date last accessed
✓ Date last inode changed
✓ Disk address of data
COMMANDS
✓ pwd: give the current path (Present Working Directory). Single dot: current directory & two dot:
parent directory.
✓ date: give current date and time
o -u: display the date in Greenwich mean time
o mm: is the month no
o dd: is the day no in the month
o HH: is the hour no in 24 hr. format
o MM: is the minute no
o cc: is the century minus one
o YY: last 2 digits of the year no.
✓ cal: calendar of current year. If particular moth and year the $cal 8 2017
✓ who: shows no of user currently logged in.
✓ who am i: exclusively that terminal where that command is given.
✓ ps: show process no
✓ clear: clear the screen
✓ echo: print the message after that echo
✓ man: help
✓ bc: calculator
✓ mkdir: creates a directory
✓ rmdir: removes the empty directory
✓ mv: rename directory / file
✓ cp: copy a file. –i: interactive option.
✓ rm: remove files & Use –i for option.
✓ cd: change directory
✓ ls: directory Listing
o -a: list all entries including those that begin with dot & two dot.
o -d: if an argument is directory, list only its name.
o -l: list in long format
o -n: same as l except owner’s UID and group GID are printed.
o -x: output in multiple columns
o -r: sort filename in reverse order
o -i: display inode no
✓ ls –l output
_ permission no_of_link owner size date_last_mofified name
_: type of file (d-directory file, b-block mode device, c-character mode device, - : ordinary file)
No of user: - 1) Owner (u) 2) Group (g) 3) Others (o)
Permission: - 1) Read (r) 2) Write (w) 3) Execute (x)
Format of permission: r w x rwx r w x : first is user then group and last others
✓ ls > list: Directing output to a file
✓ lc: listing in column wise
✓ wc: counting no of lines in a file. It has three values: first no of lines then words and then
character. Also –c for character only, -l for line and –w for word only.
✓ rm *: removes all files without giving warning.
✓ echo *: shows all file of current working directory
✓ cat: it took the input and send to standard output.
o >: output redirection
o <: input redirection
o cat > filename: took input and store in the file.
o cat < filename: display the content of file.
o -v: display non printing character
✓ sort: it took the input from standard input and then sort the input and show in standard output. It
takes all as ASCII character.
✓ more: paging output. Ex ls | more. Use pipe
✓ write <username>: communicate b/w user. To stop ctrl+d. user shoul be online.
✓ mesg n/y: block getting message, n- stop message getting, y- begin message getting
✓ mail <username>: sending mail offline or online
✓ mail: reading mail
✓ grep <pattern> <filename>: search for pattern in the file and shows the line where the pattern
appears. –v option can be used. It shows the line where pattern does not appear.
✓ chmod: change permission to a filename. + is used for granted and – is used for withdraw. Ex.
Chmod ugo+x filename. Permission can be given in octal mode also. Ex $chmod 777 filename.
Where first is for owner, then group and last other. The values is calculated as read (4), write
(2), and execute (1).
✓ passwd: changing your password
✓ cmp file1 file 2: compare two files byte by byte and the location of first mismatch is echoed.
Ex:
$x=2
$echo $x
2
$sh
$echo $x : x not defined here, parent shell variable is not transferred to child shell
$y=3
$echo $y
3
$^d : child shell destroyed
$echo $y : y not found as child shell is destroyed and all its variables.
Parent shell variable can be shown in child by using “export” keyword.
$x=2
$export x: x is copied to child shell
$sh
$echo $x
Each running program has exit status. When a program (running) ends, it returns a integer no to OS. If
returned 0, then successfully finished. If returned other than 0 then the program if finished
unsuccessfully.
FILTER COMMAND
It transforms and show the content of the file.
➢ sort <filename>: Sort, merge and remove repeated lines with sort
➢ pr <filename>: to format text to provide margins and headers, double spacing and multiple
column output
➢ | -> Ex ls|pg
➢ more: first one page and then one line by one.
➢ head: pickup lines from the beginning
➢ tail: pickup lines from the end
➢ cut: extract characters or field (vertically), range can be given using hyphen
➢ paste: join two files laterally and multiple lines to a single line.
Ex. dept.lst
01|account|6213
03|progs|5423
05|marketing|6521
04|production|9876
02|sales|1006
✓ $ pr dept.lst
✓ $ head –n 3 dept.lst
✓ $ tail –n 3 dept.lst
✓ $ cut –c 1-2 dept.lst use this if column width is same for every column
✓ $ cut –d \| -f 2,3 dept.lst -d for delimiter (default tab) and –f for field list
✓ $ cut –d \| -f 2,3 dept.lst | tee cutlist1 tee facilitate that saves the output to file.
✓ $ cut –c 1-2 dept.lst | tee cutlist2
✓ $ paste cutlist1 cutlist2 default is tab b/w then
✓ $ paste –d”|” cutlist1 cutlist2 default is then “|”
✓ Sort dept.lst
✓ Sort –t”|” –k 2 dept.lst sort on second column and specify default separation
✓ Sort –t”|” –r –k 2 dept.lst same as above but in reverse i.e. descending