Você está na página 1de 132

EXPERIMENT 1

AIM: a)Introduction to UNIX,LINUX and UBUNTU.


b).Installation of UBUNTU.
Unix (all-caps UNIX for the trademark) is a multitasking, multiuser computer
operating system that exists in many variants. The original Unix was
developed at AT&T's Bell Labs research center by Ken Thompson, Dennis
Ritchie, and others. From the power user's or programmer's perspective,
Unix systems are characterized by a modular design that is sometimes called
the "Unix philosophy," meaning the OS provides a set of simple tools that
each perform a limited, well-defined function, with a unified filesystem as the
main means of communication and a shell scripting and command language
to combine the tools to perform complex workflows.
By the most simple definition, UNIX is a computer operating system - the
base software that controls a computer system and its peripherals. In this
sense, UNIX behaves in the same way that the perhaps more familiar PC
operating systems Windows or MacOS behave. It provides the base
mechanisms for booting a computer, logging in, running applications, storing
and retrieving files, etc.
More specificially, the word "UNIX" refers to a family of operating systems
that are related to one or both of the original UNIX operating systems - BSD
and SystemV. Examples of modern UNIX operating systems include IRIX
(from SGI), Solaris (from Sun), Tru64 (from Compaq) and Linux (from the
Free Software community). Even though these different "flavors" of UNIX
have unique characteristics and come from different sources, they all work
alike in a number of fundamental ways. If you gain familiarity with any one of
these UNIX-based operating systems, you will also have gained at least some
familiarity with nearly every other variant of UNIX.
Components
The Unix system is composed of several components that are normally
packaged together. By including in addition to the kernel of an operating
system the development environment, libraries, documents, and the
portable, modifiable source-code for all of these components, Unix was a
self-contained software system. This was one of the key reasons it emerged
as an important teaching and learning tool and has had such a broad
influence.
The inclusion of these components did not make the system large the
original V7 UNIX distribution, consisting of copies of all of the compiled
binaries plus all of the source code and documentation occupied less than
10MB, and arrived on a single 9-track magnetic tape. The printed
documentation, typeset from the on-line sources, was contained in two
volumes.
The names and filesystem locations of the Unix components have changed
substantially across the history of the system. Nonetheless, the V7
implementation is considered by many to have the canonical early structure:
UE123029

Page 1

Kernel source code in /usr/sys, composed of several subcomponents:


conf configuration and machine-dependent parts, including boot code
dev device drivers for control of hardware (and some pseudohardware)

UE123029

Page 2

sys operating system "kernel", handling memory management,


process scheduling, system calls, etc.

h header files, defining key structures within the system and


important system-specific invariables
Development Environment Early versions of Unix contained a
development environment sufficient to recreate the entire system from
source code:
cc C language compiler (first appeared in V3 Unix)
as machine-language assembler for the machine
ld linker, for combining object files
lib object-code libraries (installed in /lib or /usr/lib). libc, the system
library with C run-time support, was the primary library, but there have
always been additional libraries for such things as mathematical
functions (libm) or database access. V7 Unix introduced the first
version of the modern "Standard I/O" library stdio as part of the system
library. Later implementations increased the number of libraries
significantly.
make build manager (introduced in PWB/UNIX), for effectively
automating the build process
include header files for software development, defining standard
interfaces and system invariants

UNIX PROCESSES
When a program is started on UNIX, it creates what is known as a "process" on the system.
Every process is assigned a unique serial number called its process id or PID for short.
Processes can be created by any user, but can only be destroyed by someone with the
permissions to do so - usually the user that created the process or the system administrator. This
ensures that the compute jobs you start on the system will not be disturbed by any other user of
the system until they complete or you decide to stop them yourself.
Processes and process management becomes important on UNIX systems
that are shared between a number of users. The concept of users and PIDs is
the main tool by which the available system resources are shared fairly
among everybody who needs access to them. Processes can be suspended
or given lower priority in cases where one or more users should step out of
the way for someone else, but wish to do so without losing their work up to
that point.
One further consideration on this topic is the fact that a running UNIX
process can spawn "child" processes. For example, any program you run
from inside a UNIX shell will be a child process of that shell. Conversely, the
shell is the parent process of this child. Child proceses have associated with
them both their own process id (PID) as well as their parent's process id
UE123029

Page 3

(PPID).
Normally this concept of parent and child processes is not something you
need to be bothered with as a user. However, it can be useful to understand
how UNIX organizes processes if you are trying to keep track of certain
system resources (e.g. memory and CPU), if you are working with
environment variables, or if you need to track down a rogue program or
script. Some of these items will be discussed later so it's good to have a
basic idea about what a UNIX process is.
LINUX
Linux is a UNIX-base operating system. Its original creator was a Finnish
student name Linus Torvalds, although being open source it has change a
great deal since its original conception. It belongs to nobody, and is free to
download and use. Any changes to it are open for all to adopt, and as a result
it has developed into a very powerful OS that is rapidly gaining in popularity
worldwide, particularly among those seeking an alternative to Windows.
In 1991, hardware was expanding rapidly, and DOS was the king of operating
systems. Software development was slower, and Macs, while better, were
also much pricier than PCs. UNIX was growing, but at that time in its history
the source code was jealously guarded and expensive to use.
Linus Torvalds was a Helsinki university student who liked playing around
with software and computers, and in 1991 he announced the creation of a
new core operating system that he had named Linux. It is now one of the
most used systems for the PC, and is particularly suitable for businesses with
small IT budgets. Linux is free to use and install, and is more reliable than
almost all other systems, running for many months and even years without a
reboot being necessary.
Advantages and Benefits of Linux
One of the significant benefits of open source software such as Linux is that
because it has no owner, it can be debugged without resource to a license
owner or software proprietor. Businesses therefore have the flexibility to do
as they wish with the OS without having to worry about conforming to
complex license agreements.
The major advantage of Linux is its cost: the core OS is free, while many
software applications also come with a GNU General public License. It can
also be used simultaneously by large numbers of users without slowing down
or freezing and it is very fast. It is an excellent networking platform and
performs at optimum efficiency even with little available hard disk space.
Linux also runs on a wide range of hardware types, including PCs, Macs,
mainframes, supercomputers, some cell phones and industrial robots. Some
prefer to dual-boot Linux and Windows while others prefer Linux and Mac OS.
System76 machines come pre-installed with Linux in the form of Ubuntu, a
Debian distribution of Linux. This is the most popular distribution of Linux for
laptops.
UE123029

Page 4

Components of Linux System


Linux Operating System has primarily three components
Kernel - Kernel is the core part of Linux. It is responsible for all major
activities of this operating system. It is consists of various modules and
it interacts directly with the underlying hardware. Kernel provides the
required abstraction to hide low level hardware details to system or
application programs.
System Library - System libraries are special functions or programs
using which application programs or system utilities accesses Kernel's
features. These libraries implements most of the functionalities of the
operating system and do not requires kernel module's code access
rights.
System Utility - System Utility programs are responsible to do
specialized, individual level tasks.

Kernel Mode vs User Mode


Kernel component code executes in a special privileged mode called kernel
mode with full access to all resources of the computer. This code represents
a single process, executes in single address space and do not require any
context switch and hence is very efficient and fast. Kernel runs each
processes and provides system services to processes, provides protected
access to hardwares to processes.
Support code which is not required to run in kernel mode is in System Library.
User programs and other system programs works in User Mode which has
no access to system hardwares and kernel code. User programs/ utilities use
System libraries to access Kernel functions to get system's low level tasks.
Basic Features :Following are some of the important features of Linux
Operating System.
UE123029

Page 5

Portable - Portability means softwares can works on different types of


hardwares in same way.Linux kernel and application programs supports
their installation on any kind of hardware platform.
Open Source - Linux source code is freely available and it is
community based development project. Multiple teams works in
collaboration to enhance the capability of Linux operating system and
it is continuously evolving.
Multi-User - Linux is a multiuser system means multiple users can
access system resources like memory/ ram/ application programs at
same time.
Multiprogramming - Linux is a multiprogramming system means
multiple applications can run at same time.
Hierarchical File System - Linux provides a standard file structure in
which system files/ user files are arranged.
Shell - Linux provides a special interpreter program which can be used
to execute commands of the operating system. It can be used to do
various types of operations, call application programs etc.
Security - Linux provides user security using authentication features
like password protection/ controlled access to specific files/ encryption
of data.
Architecture

Linux System Architecture is consists of following layers


Hardware layer - Hardware consists of all peripheral devices (RAM/
HDD/ CPU etc).
Kernel - Core component of Operating System, interacts directly with
hardware, provides low level services to upper layer components.
Shell - An interface to kernel, hiding complexity of kernel's functions
from users. Takes commands from user and executes kernel's
UE123029

Page 6

functions.
Utilities - Utility programs giving user most of the functionalities of an
operating systems.
Linux Vs Windows
The main benefits and advantages of Linux over other operating systems,
particularly Microsoft Windows, are:
It is free to use and distribute.
Support is free through online help sites, blogs and forums.
It is very reliable more so than most other operating systems with
very few crashes.
A huge amount of free open source software has been developed for it.
It is very resistant to malware such as spyware, adware and viruses.
It runs in a wide variety of machines than cannot be updated to use
newer Windows versions.
Since the source code is visible, backdoors are easily spotted, so
Linux offers greater security for sensitive applications.
Linux offers a high degree of flexibility of configuration, and significant
customization is possible without modifying the source code.
The Linux operating system is widely use by both home and business users,
and its usage is increasing daily. It is considered that Linux will eventually
overtake Microsoft Windows as the most popular operating system, which
could also open the door further for more free software such as Open Office,
The Gimp, Paint, Thunderbird, Firefox and Scribus. It is easy to install and run
alongside your existing operating system, so give it a try, because it is also
easy to remove if you dont like it which is unlikely.
UBUNTU
Ubuntu is an operating system that is developed by a worldwide community
of programmers as well as by employees of Ubuntu's commercial sponsor,
Canonical. Ubuntu is based on the concept of free or open-source software,
meaning that you do not pay any licensing fees for Ubuntu, and you can
download, use, and share the operating system free of charge.
Being a Linux-based operating system, Ubuntu has a well-deserved
reputation for stability and security. Historically, Linux has proven itself to be
a workhorse server operating system, and this is where, up until now, it has
been most widely used and best known. As of June, 2007, 78 percent of the
world's top 500 supercomputers were running Linux, according to
Top500.org.
However, in recent years, Linux has also become viable on desktop and
laptop computers, making it an option for individuals and businesses. Ubuntu
is generally acknowledged to be the most widely used version of Linux
available, and Mark Shuttleworth, the founder and CEO of Canonical,
estimates Ubuntu has between six and eight million users. Because the
UE123029

Page 7

software is free to download and share, it is difficult to track exact usage


numbers.
Ubuntu versus Windows and OS X
How does Ubuntu compare to the two best-known operating systems
Microsoft Windows and Apple OS X? The most obvious way is in the licensing
and distribution terms. Ubuntu is "free software" a term which is often
misunderstood to mean only free of cost. While Ubuntu is free of cost, the
term "free software" more accurately refers to the freedom to run the
program for any purpose, to study how the program works and modify it to
your needs, to redistribute copies, and to improve the program and release
your improvements to the public (see the Free Software Foundation's Web
site for a detailed definition).
Ubuntu also includes many of the programs used for everyday computing at
no cost, unlike Windows and OS X. Some examples are:
Office Suite: OpenOffice.org, a full office suite with a word processor,
spreadsheet, and presentation software that can read and write in .doc,
.xls, and .ppt formats and can also output to PDF, and supports the ISO
standard for electronic office documents, Open Document Format.
(Free training for OpenOffice.org is available at LearnFree.org.)
Desktop Email Client: Evolution, an email program with a similar
interface to Microsoft Outlook.
Web Browser: Firefox, the increasingly popular Web browser.
Databases: The two best-known open-source databases on Linux are
PostgreSQL and MySQL, but commercial databases such as Oracle and
IBM's DB2 are also available. There are also tools like Glom that
provide an easy-to-use graphical interface for designing and editing
databases.
Others: Ubuntu's online Applications Guide lists some Ubuntucompatible applications that allow you to edit images, listen to and
manage music, edit and watch videos, read PDFs, connect to instant
messaging services from MSN, AOL, Google, Yahoo, and more.
Updates and bug fixes: Security updates and bug fixes for
applications and the operating system are managed by Ubuntu, and
users are notified about these updates through an icon in the taskbar,
which they can click on to install. (Note that you must be connected to
the Internet to receive these notices.)
Another way in which Ubuntu differs from Windows and OS X is in the way it
releases new versions. Whereas Apple releases a new version about every 18
months to two years, and Microsoft took nearly five years between Windows
XP and Windows Vista, Ubuntu makes a new version available every six
months, which users can update over the Internet without reinstalling the
operating system, programs, or settings. (By contrast, neither Windows nor
Apple offers online updates, and both require the purchase of a CD/DVD to
install.)
Each release includes bug fixes and security updates at no cost for 18
UE123029

Page 8

months. After 18 months, security updates and bug fixes will no longer be
provided, but you're free to keep using that version of Ubuntu if you like, or
update online (free of charge) to a newer version that is supported in this
way. Moreover, every two years, Ubuntu releases a version that provides bug
fixes and security updates for a longer period of time three years on
desktops or laptops, and five years on servers making it a good solution
for those who want a longer rest between releases.
Of course, being open source gives Ubuntu one other major difference over
Windows and OS X, and that is the ability for users to modify it in any way
that suits them. There are two types of modifications most relevant here:
Bug fixes, security fixes, or feature enhancements, which are
contributed back to Ubuntu or the original application authors if
relevant.
Customizations to Ubuntu for a given set of circumstances, called a
derivatives. Some examples are highlighted below, but a full list can be
found on Ubuntu's Web site:
nUbuntu, a security-testing platform.
Ubuntu Studio, for multi-media editing and creation.
b) AIM :- How to Install Linux on your System
1.Its easy to install Ubuntu from a DVD. Heres what you need to do:
Put the Ubuntu DVD into the DVD-drive
Restart your computer. You should see a welcome screen prompting you to
choose your language and giving you the option to install Ubuntu or try it
from the DVD.
If your computer doesnt automatically do so, you might need to press
the F12 key to bring up the boot menu, but be careful not to hold it down that can cause an error message.

UE123029

Page 9

2
Prepare to install Ubuntu

Make sure you have enough space on your computer to install Ubuntu
It is advised to select Download updates while installing and Install this thirdparty software now
Should stay connected to the internet so you can get the latest updates while you
install Ubuntu

3 Set up wireless
If you are not connected to the internet, you will be asked to select a wireless
network, if available. We advise you to connect during the installation so we
can ensure your machine is up to date. So, if you set up your wireless
UE123029

Page 10

network at this point, its worth then clicking the Back button to go back to
the last screen (Preparing to install Ubuntu) and ticking the box
marked Download updates while installing.

4 Allocate drive space


Use the checkboxes to choose whether youd like to Install Ubuntu alongside
another operating system, delete your existing operating system and replace
it with Ubuntu, or if youre an advanced user choose the Something
elseoption

UE123029

Page 11

5 Begin the installation


Depending on your previous selections, you can now verify that you have
chosen the way in which you would like to install Ubuntu. The installation
process will begin when you click the Install Now button.
Ubuntu needs about 4.5 GB to install, so add a few extra GB to allow for your
files.

6 Select your location


If you are connected to the internet, this should be done automatically.
UE123029

Page 12

Check your location is correct and clickForward to proceed. If youre


unsure of your time zone, type the name of the town youre in or click on the
map and well help you find it.

7 Select your preferred keyboard layout


Click on the language option you need. If youre not sure, click the Detect
Keyboard Layout button for help.

UE123029

Page 13

8 Enter your login and password details

9 Learn more about Ubuntu while the system installs

10 Thats it. All thats left is to restart your computer and start enjoying Ubuntu!

UE123029

Page 14

EXPERIMENT 2
Aim: To implement various linux/unix commands :
Echo,cat,touch,man,mkdir,cd,rmdir,pwd,ls,who,whoami,wc,cal,ncal,more,less,
cp,mv,cmp,head,tail
COMMAND ECHO
display a line of text i.e Echo the STRING(s) to standard output.
SYNTAX :echo [OPTION]... [STRING]...
DESCRIPTION :
-n

do not output the trailing newline.

-e

enable interpretation of backslash escapes.

-E

disable interpretation of backslash escapes (default).

--help

display a help message and exit.

-versio
n

output version information and exit.

If -e is in effect, the following sequences are recognized:


\\

backslash

\a

alert (BELL)

\b

backspace

\c

produce no further output.

UE123029

Page 15

\e

escape

\f

form feed

\n

new line

\r

carriage return

\t

horizontal tab

\v

vertical tab

\0NNN

byte with octal value NNN (1 to 3 digits)

\xHH

byte with hexadecimal value HH (1 to 2 digits)

USAGE:
1.Using echo without any options:
gurprem@ubuntu:~/gurprem$ echo "lets get started"
lets get started
2.Using e with \n escape sequence:
gurprem@ubuntu:~/gurprem$ echo -e "lets \n get \n started"
lets
get
started
3.Using n to not output the trailing newline
gurprem@ubuntu:~/gurprem$ echo -n "lets \nget \n started"
lets \nget \n started
gurprem@ubuntu:~/gurprem$ echo -n "lets "
lets
gurprem@ubuntu:~/gurprem$ echo -n "lets \n get"
lets \n
getgurprem@ubuntu:~/gurprem$ echo 'ls'
ls
4.Using E to disable interpretation of backslash escapes
gurprem@ubuntu:~/gurprem$ echo -E " lets\n get \n started"
lets\n get \n started
5. Using option \b backspace with backslash interpretor -e which
removes all the spaces in between.
gurprem@ubuntu:~/gurprem$ echo -e "Tecmint \\bis \\ba \\bcommunity
\\bof \\bLinux \\bNerds"

UE123029

Page 16

TecmintisacommunityofLinuxNerds
6. Using option \n New line with backspace interpretor -e treats
new line from where it is used.
gurprem@ubuntu:~/gurprem$ echo -e "Tecmint \\nis \\na \\ncommunity
\\nof \\nLinux \\nNerds"
Tecmint
is
a
community
of
Linux
Nerds
7. Using option \t horizontal tab with backspace interpretor -e
to have horizontal tab spaces.
gurprem@ubuntu:~/gurprem$ echo -e "Tecmint \\tis \\ta \\tcommunity
\\tof \\tLinux \\tNerds"
Tecmint
is
a
community of
Linux
Nerds
8. How about using option new Line \n and horizontal tab \t
simultaneously.
gurprem@ubuntu:~/gurprem$ echo -e "\\n\\tTecmint \\n\\tis \\n\\ta
\\n\\tcommunity \\n\\tof \\n\\tLinux \\n\\tNerds"
Tecmint
is
a
community
of
Linux
Nerds
9. Using option \v vertical tab with backspace interpretor -e to
have vertical tab spaces.
gurprem@ubuntu:~/gurprem$ echo -e "\\vTecmint \\vis \\va
\\vcommunity \\vof \\vLinux \\vNerds"
Tecmint

is
a

community
of

Linux
Nerds

10. How about using option new Line \n and vertical tab \v
simultaneously.
gurprem@ubuntu:~/gurprem$ echo -e "\\n\\vTecmint \\n\\vis \\n\\va
\\n\\vcommunity \\n\\vof \\n\\vLinux \\n\\vNerds"

UE123029

Page 17

Tecmint
is
a
community
of
Linux
Nerds
Note: We can double the vertical tab, horizontal tab and new line
spacing using the option two times or as many times as required.
11. Using option \r carriage return with backspace interpretor e to have specified carriage return in output.
gurprem@ubuntu:~/gurprem$ echo -e "Tecmint \\ris a community of Linux
Nerds"
is a community of Linux Nerds
12. Using option \c suppress trailing new line with backspace
interpretor -e to continue without emitting new line.
gurprem@ubuntu:~/gurprem$ echo -e "Tecmint is a community \\cof Linux
Nerds"
Tecmint is a community avi@tecmint:~$
13. Omit echoing trailing new line using option -n.
gurprem@ubuntu:~/gurprem$ echo -n "Tecmint is a community of Linux
Nerds"
Tecmint is a community of Linux Nerdsavi@tecmint:~/Documents$
14. Using option \a alert return with backspace interpretor -e
to have sound alert.
gurprem@ubuntu:~/gurprem$ echo -e "Tecmint is a community of \\aLinux
Nerds"
Tecmint is a community of Linux Nerds.

COMMAND CAT
It can

be used for the following purposes under UNIX or Linux:


Display text files on screen.
Copy text files.
Combine text files.
Create new text files.

SYNTAX:
cat [OPTIONS]... filename

UE123029

Page 18

cat file1 file 2


cat file1 file2 > newCombinedFile
DESCRIPTION:
-A
Show all.
-b
Omits line numbers for blank space in the output.
-e
A $ character will be printed at the end of each line prior to a new line.
-E
Displays a $ (dollar sign) at the end of each line.
-n
Line numbers for all the output lines.
-s
-T

If the output has multiple empty lines it replaces it with one empty line.
Displays the tab characters in the output.
Non-printing characters (with the exception of tabs, new-lines and
form-feeds) are printed visibly.

-v
Usage:
1.-A

gurprem@ubuntu:~/gurprem$ cat -A os1


hello world!!$
gurprem@ubuntu:~/gurprem$ cat -A os1
hello world!!in os1!$
gurprem@ubuntu:~/gurprem$ cat -A os1
hello world!!in ^I^Ios1!$
NOTE: -A displays ^I^ in palce of tab
$
hii!$

2.
-b,
gurprem@ubuntu:~/gurprem$ cat -b os1
hello world!!
gurprem@ubuntu:~/gurprem$ cat -b os1
1
hello world!!in os1!
2

hii!

3.
E
gurprem@ubuntu:~/gurprem$ cat -E os1
hello world!!in
os1!$ NOTE:Dispalys tabs inpalce of tab.
$
hii!$
-n,
gurprem@ubuntu:~/gurprem$ cat -n os1

UE123029

Page 19

1
2
3

hello world!!in os1!


hii!

s
gurprem@ubuntu:~/gurprem$ cat demo
hello

world

night night
gurprem@ubuntu:~/gurprem$ cat -s demo
hello
world
night night
4.
-t ,T, v
gurprem@ubuntu:~/gurprem$ cat -v demo
hello
good evening
gurprem@ubuntu:~/gurprem$ cat -vT demo
hello^I^Igood evening
gurprem@ubuntu:~/gurprem$ cat -T demo
hello^I^Igood evening
gurprem@ubuntu:~/gurprem$ cat -t demo
hello^I^Igood evening
5. To Create a new file:
gurprem@ubuntu:~/gurprem$ cat>demo
hello world !
This command creates a new file demo.txt. After typing into the file
press control+d (^d) simultaneously to end the file.
6.To Append data into the file:
gurprem@ubuntu:~/gurprem$ cat>>demo
hello...this is my first file.
To append data into the same file use append operator >> to write into
the file, else the file will be overwritten (i.e., all of its contents
will be erased).
7.To display a file:
gurprem@ubuntu:~/gurprem$ cat demo

UE123029

Page 20

hello world!
This command displays the data in the file.
8.To concatenate several files and display:
gurprem@ubuntu:~/gurprem$ cat file1 file2
The above cat command will concatenate the two files (file1.txt and
file2.txt) and it will display the output in the screen.
9.To concatenate several files and to transfer the output to another
file.
gurprem@ubuntu:~/gurprem$ cat os1>>file2
gurprem@ubuntu:~/gurprem$ cat file2
hello world!!in os1!
hello world!!in os1!
-In the above example the output is redirected to new file file2. The
cat command will create new file file3.txt and store the concatenated
output into file3.txt.

COMMAND MAN:
man command which is short for manual, provides in depth information about the
requested command (or) allows users to search for commands related to a
particular keyword.
SYNTAX:
man commandname [OPTIONS]
DESCRIPTION:
-a
-k

Print a one-line help message and exit.


Searches for keywords in all of the manuals available..

USAGE:
1. gurprem@ubuntu:~/gurprem$ man mkdir
Display the information about mkdir command.

COMMAND

TOUCH :

touch command is used to update last modified time and date of a file. It is also
used to create a new empty file with current date and time.
SYNTAX:
touch [options] File
DESCRIPTION:
-a
update only the access time.
-B,
Modify the time by going back SECONDS seconds.
--backward=SE

UE123029

Page 21

CONDS
-c, --no-create do not create any files.
-d,
parse STRING and use it instead of current time.
--date=STRING
-m
change only the modification time.
set time given by WORD: access atime use (same as -a)
--time=WORD
modify mtime (same as -m).
USAGE:
1.
gurprem@ubuntu:~/gurprem$ touch os1
gurprem@ubuntu:~/gurprem$ ls -l
total 516
-rw-rw-r-- 1 gurprem gurprem
45
-rw-rw-r-- 1 gurprem gurprem
44
-rw-rw-r-- 1 gurprem gurprem
0
....
-rw-rw-r-- 1 gurprem gurprem
35
-rw-rw-r-- 1 gurprem gurprem
57
-rw-rw-r-- 1 gurprem gurprem 211370
-rw-rw-r-- 1 gurprem gurprem 197214

Sep 2 13:16 demo


Sep 2 13:15 demo~
Aug 31 04:12 new$documnet#~
Sep 2 13:36 os1
Aug 27 03:11 os1~
Aug 31 05:05 typescript~
Aug 31 05:08 typescript-lab1

2.
gurprem@ubuntu:~/gurprem$ touch -a file1
gurprem@ubuntu:~/gurprem$ ls -l
total 516
-rw-rw-r-- 1 gurprem gurprem
45 Sep 2
-rw-rw-r-- 1 gurprem gurprem
44 Sep 2
-rw-rw-r-- 1 gurprem gurprem
35 Aug 27
-rw-rw-r-- 1 gurprem gurprem
14 Aug 27
....
3.
gurprem@ubuntu:~/gurprem$ touch -c fff
gurprem@ubuntu:~/gurprem$ cat fff
cat: fff: No such file or directory

13:16
13:15
02:53
03:17

demo
demo~
file
file1

4.
gurprem@ubuntu:~/gurprem$ touch -d "next thursday" file1
gurprem@ubuntu:~/gurprem$ ls -l
total 516
-rw-rw-r-- 1 gurprem gurprem
45 Sep 2 13:16 demo
-rw-rw-r-- 1 gurprem gurprem
44 Sep 2 13:15 demo~
-rw-rw-r-- 1 gurprem gurprem
35 Aug 27 02:53 file
-rw-rw-r-- 1 gurprem gurprem
14 Sep 4 2014 file1
-rw-rw-r-- 1 gurprem gurprem
42 Aug 27 02:56 file

COMMAND LS:
ls - list directory contents
List information about the FILEs (the current directory by default).

UE123029

Page 22

Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.


SYNTAX
ls [OPTION]... [FILE]...
DESCRIPTION:
Option
Description:
-a
do not ignore entries starting with .
-A
do not list implied . and ..
-b
print C-style escapes for nongraphic characters
-B
Ignore backups i.e do not list implied entries ending with ~
-c
sort by ctime, newest first
with -lt: sort by, and show, ctime
with -l: show ctime and sort by name
-C
List entries by columns
-f
do not sort, enable -aU, disable -ls color
-F
append indicator (one of */=>@|) to entries
-g
Do not list the owner
-h l
Print sizes in human readable form
-si
likewise, but use powers of 1000 not 1024
-do not list implied entries matching shell PATTERN (overridden
hide=PATTER
by -a or -A)
N
-i
Print index number of each file
-k
Use 1024 byte block
-m
fill width with a comma separated list of entries
-n
like -l, but list numeric user and group Ids
-p
Append / to directories
-Q
enclose entry names in double quotes
-r
Reverse order while sorting
-s, --size
print the allocated size of each file, in blocks
-S
-w
-1

sort by file size


assume screen width instead of current value
-1
list one file per line

1.
gurprem@ubuntu:~/gurprem$ ls
demo~ f2~ f5~ file1
gg2
f1
f3
f6
file1~
lab work 1.2
f1~
f4
f7
file2~
lab work 1.3
f10
f4~ f8
file file new$documnet#
f2
f5
f9
gg1
new$documnet#~
2. a
gurprem@ubuntu:~/gurprem$ ls -a
.
f1~ f3
f5~ f9
file file
..
f10 f4
f6
file1
gg1
demo~ f2
f4~ f7
file1~ gg2
typescript~

UE123029

Page 23

os1~
test
typescript
typescript~
typescript-lab1

lab work 1.3


new$documnet#
new$documnet#~

test
typescript

f1
f2~ f5
typescript-lab1

f8

file2~

lab work 1.2

3. -A
gurprem@ubuntu:~/gurprem$ ls -A
demo~ f2~ f5~ file1
gg2
f1
f3
f6
file1~
lab work 1.2
f1~
f4
f7
file2~
lab work 1.3
f10
f4~ f8
file file new$documnet#
f2
f5
f9
gg1
new$documnet#~

os1~

4. b
gurprem@ubuntu:~/gurprem$ ls
demo~ f2~ f5~ file1
f1
f3
f6
file1~
f1~
f4
f7
file2~
f10
f4~ f8
file\ file
f2
f5
f9
gg1

-b
gg2
lab\ work\ 1.2
lab\ work\ 1.3
new$documnet#
new$documnet#~

os1~
test
typescript
typescript~
typescript-lab1

5. -B
gurprem@ubuntu:~/gurprem$ ls -B
f1
f3 f6 f9
gg1
f10 f4 f7 file1
gg2
f2
f5 f8 file file lab work 1.2

os1~
test
typescript
typescript~
typescript-lab1

lab work 1.3


new$documnet#
test

typescript
typescript-lab1

6. c
gurprem@ubuntu:~/gurprem$ ls -c
f10 f9
f6
f1~
file1~ typescript-lab1 file file
os1~
f5
typescript f5~ f2
gg1
lab work 1.2
new$documnet#
file2~
f4
f7
f3
f2~
test
lab work 1.3
new$documnet#~
f8
f4~
f1
file1 demo~
typescript~
gg2
gurprem@ubuntu:~/gurprem$ ls -c
new f8
f4~ f1
file1
demo~
typescript~
gg2
f10 f9
f6
f1~ file1~ typescript-lab1 file file
os1~
f5
typescript f5~ f2
gg1
lab work 1.2
new$documnet#
file2~
f4
f7
f3
f2~ test
lab work 1.3
new$documnet#~
6.1 c lt
gurprem@ubuntu:~/gurprem$ ls -c -lt
total 560
-rw-rw-r-- 1 gurprem gurprem
0
-rw-rw-r-- 1 gurprem gurprem
0
-rw-rw-r-- 1 gurprem gurprem
24
-rw-rw-r-- 1 gurprem gurprem
8
-rw-rw-r-- 1 gurprem gurprem
0
-rw-rw-r-- 1 gurprem gurprem
0
-rw-rw-r-- 1 gurprem gurprem 16384

UE123029

Sep
Sep
Sep
Sep
Sep
Sep
Sep

Page 24

6
3
3
3
3
3
3

11:17
04:01
03:58
03:54
03:46
03:46
03:44

new
f10
f5
f4
f8
f9
typescript

-rw-rw-r--rw-rw-r--rw-rw-r--rw-rw-r--rw-rw-r--rw-rw-r--rw-rw-r--rw-rw-r--rw-rw-r--rw-rw-r--rw-rw-r-drwxrwxr-x
dr--r--rw-rw-rw-r--rw-rw-r--rw-rw-r--rw-rw-r--rw-rw-r--rw-rw-r--rw-rw-r--rw-rw-r-drw-rw-r--rw-rw-r--rw-rw-r--

1
1
1
1
1
1
1
1
1
1
1
2
2
1
1
1
1
1
1
1
1
2
1
1

gurprem
gurprem
gurprem
gurprem
gurprem
gurprem
gurprem
gurprem
gurprem
gurprem
gurprem
gurprem
gurprem
gurprem
gurprem
gurprem
gurprem
gurprem
gurprem
gurprem
gurprem
gurprem
gurprem
gurprem

gurprem
18 Sep 3 03:42 f7
gurprem
21 Sep 3 03:31 f4~
gurprem
37 Sep 3 03:22 f6
gurprem
18 Sep 3 03:17 f5~
gurprem
67 Sep 3 03:07 f3
gurprem
48 Sep 3 02:58 f1
gurprem
38 Sep 3 02:58 f1~
gurprem
19 Sep 3 02:47 f2
gurprem
7 Sep 3 02:47 f2~
gurprem
59 Sep 3 02:39 file1
gurprem
9 Sep 3 02:39 file1~
gurprem
4096 Sep 2 23:13 gg1
gurprem
4096 Sep 2 13:46 test
gurprem
44 Sep 2 13:16 demo~
gurprem 197214 Aug 31 05:17 typescript-lab1
gurprem 26409 Aug 31 05:16 lab work 1.2
gurprem 29950 Aug 31 05:16 lab work 1.3
gurprem 211370 Aug 31 05:08 typescript~
gurprem
0 Aug 31 04:39 file file
gurprem
23 Aug 31 04:16 new$documnet#
gurprem
0 Aug 31 04:16 new$documnet#~
gurprem
4096 Aug 27 03:38 gg2
gurprem
57 Aug 27 03:11 os1~
gurprem
35 Aug 27 02:54 file2~

6.2 c l
gurprem@ubuntu:~/gurprem$ ls -c -l
total 560
-rw-rw-r-- 1 gurprem gurprem
44 Sep 2 13:16 demo~
-rw-rw-r-- 1 gurprem gurprem
48 Sep 3 02:58 f1
-rw-rw-r-- 1 gurprem gurprem
38 Sep 3 02:58 f1~
-rw-rw-r-- 1 gurprem gurprem
0 Sep 3 04:01 f10
-rw-rw-r-- 1 gurprem gurprem
19 Sep 3 02:47 f2
-rw-rw-r-- 1 gurprem gurprem
7 Sep 3 02:47 f2~
-rw-rw-r-- 1 gurprem gurprem
67 Sep 3 03:07 f3
-rw-rw-r-- 1 gurprem gurprem
8 Sep 3 03:54 f4
-rw-rw-r-- 1 gurprem gurprem
21 Sep 3 03:31 f4~
-rw-rw-r-- 1 gurprem gurprem
24 Sep 3 03:58 f5
-rw-rw-r-- 1 gurprem gurprem
18 Sep 3 03:17 f5~
-rw-rw-r-- 1 gurprem gurprem
37 Sep 3 03:22 f6
-rw-rw-r-- 1 gurprem gurprem
18 Sep 3 03:42 f7
-rw-rw-r-- 1 gurprem gurprem
0 Sep 3 03:46 f8
-rw-rw-r-- 1 gurprem gurprem
0 Sep 3 03:46 f9
-rw-rw-r-- 1 gurprem gurprem
59 Sep 3 02:39 file1
-rw-rw-r-- 1 gurprem gurprem
9 Sep 3 02:39 file1~
-rw-rw-r-- 1 gurprem gurprem
35 Aug 27 02:54 file2~
-rw-rw-r-- 1 gurprem gurprem
0 Aug 31 04:39 file file
drwxrwxr-x 2 gurprem gurprem
4096 Sep 2 23:13 gg1
drw-rw-r-- 2 gurprem gurprem
4096 Aug 27 03:38 gg2
-rw-rw-r-- 1 gurprem gurprem 26409 Aug 31 05:16 lab work 1.2
-rw-rw-r-- 1 gurprem gurprem 29950 Aug 31 05:16 lab work 1.3
-rw-rw-r-- 1 gurprem gurprem
0 Sep 6 11:17 new

UE123029

Page 25

-rw-rw-r-- 1 gurprem gurprem


23 Aug
-rw-rw-r-- 1 gurprem gurprem
0 Aug
-rw-rw-r-- 1 gurprem gurprem
57 Aug
dr--r--rw- 2 gurprem gurprem
4096 Sep
-rw-rw-r-- 1 gurprem gurprem 16384 Sep
-rw-rw-r-- 1 gurprem gurprem 211370 Aug
-rw-rw-r-- 1 gurprem gurprem 197214 Aug
gurprem@ubuntu:~/gurprem$

31
31
27
2
3
31
31

04:16
04:16
03:11
13:46
03:44
05:08
05:17

new$documnet#
new$documnet#~
os1~
test
typescript
typescript~
typescript-lab1

7. C
gurprem@ubuntu:~/gurprem$ ls C
demo~
lab1
f1
f1~
f10
f2

f2~

f5~

file1

gg2

new$documnet#~

f3
f4
f4~
f5

f6
f7
f8
f9

file1~
file2~
file file
gg1

lab work 1.2


lab work 1.3
new
new$documnet#

os1~
test
typescript
typescript~

8. f
gurprem@ubuntu:~$ cd gurprem
gurprem@ubuntu:~/gurprem$ ls
f5~
f4~
new$documnet# file1
file file
f2~
lab1
file2~
f1~
.
lab work 1.3
f9
f5
demo~
test

-f
gg1
lab work 1.2
typescript

f2
f1
typescript~

f8
..
typescript-

gg2
f10
f6
new

f3
file1~
f7
new$documnet#~

f4
os1~

new$documnet#~

typescript-

9. F
gurprem@ubuntu:~/gurprem$ ls -F
demo~ f2~ f5~ file1
gg2/
lab1
f1
f3
f6
file1~
lab work 1.2
f1~
f4
f7
file2~
lab work 1.3
f10
f4~ f8
file file new
f2
f5
f9
gg1/
new$documnet#
10. g
gurprem@ubuntu:~/gurprem$ ls -g
total 560
-rw-rw-r-- 1 gurprem
44 Sep
-rw-rw-r-- 1 gurprem
48 Sep
-rw-rw-r-- 1 gurprem
38 Sep
-rw-rw-r-- 1 gurprem
0 Sep
-rw-rw-r-- 1 gurprem
19 Sep
-rw-rw-r-- 1 gurprem
7 Sep
-rw-rw-r-- 1 gurprem
67 Sep
-rw-rw-r-- 1 gurprem
8 Sep
-rw-rw-r-- 1 gurprem
21 Sep

UE123029

typescript-

2
3
3
3
3
2
3
3
3

13:15
02:58
02:57
04:01
02:47
23:29
03:07
03:54
03:30

Page 26

os1~
test/
typescript
typescript~

demo~
f1
f1~
f10
f2
f2~
f3
f4
f4~

-rw-rw-r--rw-rw-r--rw-rw-r--rw-rw-r--rw-rw-r--rw-rw-r--rw-rw-r--rw-rw-r--rw-rw-r--rw-rw-r-drwxrwxr-x
drw-rw-r--

1
1
1
1
1
1
1
1
1
1
2
2

gurprem
gurprem
gurprem
gurprem
gurprem
gurprem
gurprem
gurprem
gurprem
gurprem
gurprem
gurprem

24
18
37
18
0
0
59
9
35
0
4096
4096

11. h l
gurprem@ubuntu:~/gurprem$ ls
total 560K
-rw-rw-r-- 1 gurprem gurprem
-rw-rw-r-- 1 gurprem gurprem
-rw-rw-r-- 1 gurprem gurprem
-rw-rw-r-- 1 gurprem gurprem
-rw-rw-r-- 1 gurprem gurprem
-rw-rw-r-- 1 gurprem gurprem
-rw-rw-r-- 1 gurprem gurprem
-rw-rw-r-- 1 gurprem gurprem
-rw-rw-r-- 1 gurprem gurprem
-rw-rw-r-- 1 gurprem gurprem
-rw-rw-r-- 1 gurprem gurprem
-rw-rw-r-- 1 gurprem gurprem
-rw-rw-r-- 1 gurprem gurprem
-rw-rw-r-- 1 gurprem gurprem
-rw-rw-r-- 1 gurprem gurprem
-rw-rw-r-- 1 gurprem gurprem
-rw-rw-r-- 1 gurprem gurprem
-rw-rw-r-- 1 gurprem gurprem
-rw-rw-r-- 1 gurprem gurprem
drwxrwxr-x 2 gurprem gurprem
drw-rw-r-- 2 gurprem gurprem
-rw-rw-r-- 1 gurprem gurprem
-rw-rw-r-- 1 gurprem gurprem
-rw-rw-r-- 1 gurprem gurprem
-rw-rw-r-- 1 gurprem gurprem
-rw-rw-r-- 1 gurprem gurprem
-rw-rw-r-- 1 gurprem gurprem
dr--r--rw- 2 gurprem gurprem
-rw-rw-r-- 1 gurprem gurprem
-rw-rw-r-- 1 gurprem gurprem
-rw-rw-r-- 1 gurprem gurprem

Sep 3 03:58 f5
Sep 3 03:10 f5~
Sep 3 03:22 f6
Sep 3 03:42 f7
Sep 3 03:46 f8
Sep 3 03:46 f9
Sep 3 02:39 file1
Sep 2 23:36 file1~
Aug 27 02:53 file2~
Aug 31 04:39 file file
Sep 2 23:13 gg1
Aug 27 03:38 gg2
-h -l
44
48
38
0
19
7
67
8
21
24
18
37
18
0
0
59
9
35
0
4.0K
4.0K
26K
30K
0
23
0
57
4.0K
16K
207K
193K

Sep
Sep
Sep
Sep
Sep
Sep
Sep
Sep
Sep
Sep
Sep
Sep
Sep
Sep
Sep
Sep
Sep
Aug
Aug
Sep
Aug
Aug
Aug
Sep
Aug
Aug
Aug
Sep
Sep
Aug
Aug

2
3
3
3
3
2
3
3
3
3
3
3
3
3
3
3
2
27
31
2
27
31
31
6
31
31
27
2
3
31
31

12. si
gurprem@ubuntu:~/gurprem$ ls -si
total 560
803507
4 demo~ 803511
4 f6

UE123029

Page 27

13:15
02:58
02:57
04:01
02:47
23:29
03:07
03:54
03:30
03:58
03:10
03:22
03:42
03:46
03:46
02:39
23:36
02:53
04:39
23:13
03:38
05:03
05:15
11:17
04:16
04:12
03:11
13:46
03:44
05:05
05:08

demo~
f1
f1~
f10
f2
f2~
f3
f4
f4~
f5
f5~
f6
f7
f8
f9
file1
file1~
file2~
file file
gg1
gg2
lab work 1.2
lab work 1.3
new
new$documnet#
new$documnet#~
os1~
test
typescript
typescript~
typescript-lab1

803475

32 lab work 1.3

803406
803362
803517
803508
803478
803510
803515
803509
803513
803506

4
4
0
4
4
4
4
4
4
4

f1
f1~
f10
f2
f2~
f3
f4
f4~
f5
f5~

803512
803410
803516
803411
803502
803405
803455
803412
803413
803437

4
0
0
4
4
4
0
4
4
28

f7
f8
f9
file1
file1~
file2~
file file
gg1
gg2
lab work 1.2

803521
0 new
803440
4 new$documnet#
803353
0 new$documnet#~
803407
4 os1~
803503
4 test
803399 16 typescript
803472 208 typescript~
803469 196 typescript-lab1

13. hide
gurprem@ubuntu:~/gurprem$ ls --hide 'g*'
demo~ f2~ f5~ file1
lab work 1.3
f1
f3
f6
file1~
new
f1~
f4
f7
file2~
new$documnet#
f10
f4~ f8
file file
new$documnet#~
f2
f5
f9
lab work 1.2 os1~

test
typescript
typescript~
typescript-lab1

gurprem@ubuntu:~/gurprem$ ls --hide 'f*'


demo~ lab work 1.2 new$documnet#
test
gg1
lab work 1.3 new$documnet#~ typescript
gg2
new
os1~
typescript~
14.-i
gurprem@ubuntu:~/gurprem$ ls -i
803507 demo~ 803509 f4~
803502
803406 f1
803513 f5
803405
803362 f1~
803506 f5~
803455
803517 f10
803511 f6
803412
803508 f2
803512 f7
803413
803478 f2~
803410 f8
803437
803510 f3
803516 f9
803475
lab1
803515 f4
803411 file1 803521

file1~
file2~
file file
gg1
gg2
lab work 1.2
lab work 1.3

typescript-lab1

803440
803353
803407
803503
803399
803472
803469

new$documnet#
new$documnet#~
os1~
test
typescript
typescript~
typescript-

new

15. k m
gurprem@ubuntu:~/gurprem$ ls -k -m
demo~, f1, f1~, f10, f2, f2~, f3, f4, f4~, f5, f5~, f6, f7, f8, f9,
file1,
file1~, file2~, file file, gg1, gg2, lab work 1.2, lab work 1.3, new,
new$documnet#, new$documnet#~, os1~, test, typescript, typescript~,
16. n
gurprem@ubuntu:~/gurprem$ ls -n
total 560
-rw-rw-r-- 1 1000 1000
44 Sep
-rw-rw-r-- 1 1000 1000
48 Sep
-rw-rw-r-- 1 1000 1000
38 Sep
-rw-rw-r-- 1 1000 1000
0 Sep
-rw-rw-r-- 1 1000 1000
19 Sep

UE123029

2
3
3
3
3

13:15
02:58
02:57
04:01
02:47

Page 28

demo~
f1
f1~
f10
f2

-rw-rw-r-- 1 1000 1000


-rw-rw-r-- 1 1000 1000
-rw-rw-r-- 1 1000 1000

7 Sep
67 Sep
8 Sep

2 23:29 f2~
3 03:07 f3
3 03:54 f4

17. p
gurprem@ubuntu:~/gurprem$ ls -p
demo~ f2~ f5~ file1
gg2/
lab1
f1
f3
f6
file1~
lab work 1.2
f1~
f4
f7
file2~
lab work 1.3
f10
f4~ f8
file file new
f2
f5
f9
gg1/
new$documnet#

new$documnet#~

typescript-

os1~
test/
typescript
typescript~

18. Q
gurprem@ubuntu:~/gurprem$
"demo~" "f3"
"f7"
"typescript-lab1"
"f1"
"f4"
"f8"
"f1~"
"f4~" "f9"
"f10"
"f5"
"file1"
"f2"
"f5~" "file1~"
"f2~"
"f6"
"file2~"

ls -Q
"file file"

"new$documnet#"

"gg1"
"gg2"
"lab work 1.2"
"lab work 1.3"
"new"

"new$documnet#~"
"os1~"
"test"
"typescript"
"typescript~"

19. r
gurprem@ubuntu:~/gurprem$ ls -r
typescript-lab1 os1~
f1~
typescript~
new$documnet#~
f1
typescript
new$documnet#
demo~
test
new

lab work 1.3

file file

f9

f5~

f3

lab work 1.2

file2~

f8

f5

f2~

gg2

file1~

f7

f4~

f2

gg1

file1

f6

f4

f10

20. s S
gurprem@ubuntu:~/gurprem$ ls -s -S
total 560
208 typescript~
4 f3
4 f5
196 typescript-lab1
4 file1
4 new$documnet#
32 lab work 1.3
4 os1~
4 f4~
28 lab work 1.2
4 f1
4 f2
16 typescript
4 demo~
4 f5~
4 gg1
4 f1~
4 f7
4 gg2
4 f6
4 file1~
4 test
4 file2~
4 f4
gurprem@ubuntu:~/gurprem$ ls -r -s -S
total 560
0 new$documnet#~
4 file1~
0 new
4 f7
0 file file
4 f5~
0 f9
4 f2

UE123029

Page 29

4
4
4
4

f6
f1~
demo~
f1

4
0
0
0
0
0
0

4
4
16
28

f2~
f10
f8
f9
file file
new
new$documnet#~

gg2
gg1
typescript
lab work 1.2

0
0
4
4

f8
f10
f2~
f4

4
4
4
4

f4~
new$documnet#
f5
file2~

21. w
gurprem@ubuntu:~/gurprem$ ls
demo~ file1~
f1
file2~
f1~
file file
f10
gg1
f2
gg2
f2~
lab work 1.2
f3
lab work 1.3
f4
new
f4~
new$documnet#
f5
new$documnet#~
f5~
os1~
f6
test
f7
typescript
f8
typescript~
f9
typescript-lab1
file1
22. -1
gurprem@ubuntu:~/gurprem$ ls
demo~
f1
f1~
f10
f2
f2~
f3
f4
f4~
f5
f5~
f6
f7
f8
f9
file1
file1~
file2~
file file
gg1
gg2
lab work 1.2
lab work 1.3
new

UE123029

-w 25

-1

Page 30

4
4
4
4

os1~
file1
f3
test

32 lab work 1.3


196 typescript-lab1
208 typescript~

new$documnet#
new$documnet#~
os1~
test
typescript
typescript~
typescript-lab1

COMMAND MKDIR:
mkdir command is used to create one or more directories.
SYNTAX:
The Syntax is
mkdir [options] directories
DESCRIPTION:
-m
-p
-v

Set the access mode for the new directories.


Create intervening parent directories if they don't exist.
Print help message for each directory created.

USAGE:
Create directory:
gurprem@ubuntu:~/gurprem$ mkdir test
The above command is used to create the directory 'test'.
Create directory and set permissions:
gurprem@ubuntu:~/gurprem$ mkdir -m -wx test
gurprem@ubuntu:~/gurprem$ cd test
bash: cd: test: Permission denied
Command WHO
who command can list the names of users currently logged in, their terminal,
the time they have been logged in, and the name of the host from which they have
logged in.
SYNTAX:
The Syntax is
who [options] [file]
DESCRIPTION:
am i
-b
-d
-H
-m

Print the username of the invoking user, The 'am' and 'i' must be
space separated.
Prints time of last system boot.
print dead processes.
Print column headings above the output.
Same as who am i.

UE123029

Page 31

-q
-T,-w

Prints only the usernames and the user count/total no of users


logged in.
Include user's message status in the output.

USAGE:
1.
gurprem@ubuntu:~/gurprem$ who
gurprem :0
2014-09-02 13:05 (:0)
gurprem pts/1
2014-09-02 13:12 (:0)
2. gurprem@ubuntu:~/gurprem$ whoami
Gurprem
3.
gurprem@ubuntu:~/gurprem$ who -b
system boot 2014-09-01 19:47
4.
gurprem@ubuntu:~/gurprem$ who -d
pts/0
2014-09-01 20:14
term=0 exit=0

0 id=/0

5.
gurprem@ubuntu:~/gurprem$ who -H
NAME
LINE
TIME
COMMENT
gurprem :0
2014-09-02 13:05 (:0)
gurprem pts/1
2014-09-02 13:12 (:0)
6.
gurprem@ubuntu:~/gurprem$ who -m
gurprem pts/1
2014-09-02 13:12 (:0)
7.
gurprem@ubuntu:~/gurprem$ who -q
gurprem gurprem
# users=2

COMMAND PWD:
pwd - Print Working Directory. pwd command prints the full filename of the
current working directory.
SYNTAX:
The Syntax is
pwd [options]
OPTIONS:

UE123029

Page 32

-P
-L

The pathname printed will not contain symbolic links.


The pathname printed may contain symbolic links.

USAGE:
1.
gurprem@ubuntu:~/gurprem$ pwd
/home/gurprem/gurprem

COMMAND

WC

Word Count (Wc) command counts and displays the number of lines, words,
character and number of bytes enclosed in a file.
SYNTAX:
The Syntax is
wc [filename]
OPTIONS:
-c
-m, --chars
-l, --lines
-L, --max-linelength
-w, --words
--help
--version

print the byte counts


print the character counts switch.
print the newline counts
print the length of the longest line
print the word counts
display this help and exit
output version information and exit

USAGE:
1.
4

gurprem@ubuntu:~/gurprem$ wc test
18 102 test.txt

4 = Lines
18 = Words
102 = Bytes
In the above example, the word count cmd counts and displays the
number of lines, words, bytes in the file test.txt.
gurprem@ubuntu:~/gurprem$ wc c demo
31 demo
gurprem@ubuntu:~/gurprem$ wc l demo

UE123029

Page 33

1 demo
gurprem@ubuntu:~/gurprem$ $ wc L demo
30 demo
gurprem@ubuntu:~/gurprem$ wc m demo
31 demo
gurprem@ubuntu:~/gurprem$ wc w demo
5 demo

Command CAL and NCAL


cal command is used to display the calendar.
SYNTAX:
The Syntax is
cal [options] [month] [year]
OPTIONS:
-1
-3
??-s
??-m
-j
-y

Displays
Displays
Displays
Displays
Displays
Displays

single month as output.


prev/current/next month output.
sunday as the first day of the week.
Monday as the first day of the week.
Julian dates (days one-based, numbered from January 1).
a calendar for the current year.

USAGE:
1.
gurprem@ubuntu:~/gurprem$ cal -1
September 2014
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
2
gurprem@ubuntu:~/gurprem$ cal -3
August 2014
September
Su Mo Tu We Th Fr Sa
Su Mo Tu We
1 2
1 2 3
3 4 5 6 7 8 9
7 8 9 10
10 11 12 13 14 15 16
14 15 16 17
17 18 19 20 21 22 23
21 22 23 24
24 25 26 27 28 29 30
28 29 30

UE123029

2014
Th Fr
4 5
11 12
18 19
25 26

Page 34

Sa
6
13
20
27

October 2014
Su Mo Tu We Th
1 2
5 6 7 8 9
12 13 14 15 16
19 20 21 22 23
26 27 28 29 30

Fr
3
10
17
24
31

Sa
4
11
18
25

3
gurprem@ubuntu:~/gurprem$ cal -j
September 2014
Su Mo Tu We Th Fr Sa
244 245 246 247 248 249
250 251 252 253 254 255 256
257 258 259 260 261 262 263
264 265 266 267 268 269 270
271 272 273
4.
gurprem@ubuntu:~/gurprem$ cal 3 2013
March 2013
Su Mo Tu We Th Fr Sa
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
5.
gurprem@ubuntu:~/gurprem$ ncal
September 2014
Su
7 14 21 28
Mo 1 8 15 22 29
Tu 2 9 16 23 30
We 3 10 17 24
Th 4 11 18 25
Fr 5 12 19 26
Sa 6 13 20 27

Command RMDIR :
Removes a directory.
SYNTAX :
rmdir [-p] directory ...
DESCRIPTION:
The rmdir utility removes the directory entry specified by each
directory argument , provided the directory is empty.
Arguments are processed in the order given. In order to remove both a parent
directory and a subdirectory of that parent, the subdirectory must be specified first
so the parent directory is empty when rmdir tries to remove it.
Options
--ignorefail-onnon-

UE123029

ignore any failure which occurs solely because a directory is nonempty.

Page 35

empty
-p

-v, -verbose

remove DIRECTORY and its ancestors; e.g., 'rmdir -p a/b/c' is


similar to 'rmdir a/b/c a/b a'
output a diagnostic for every directory processed

USAGE:
1.
gurprem@ubuntu:~/gurprem$ rmdir -p gg //gg empty
gurprem@ubuntu:~/gurprem$ ls
demo~ f4
gg1
lab work 1.3
os1~
f1
file2~
gg2
new$documnet#
test
f2
file file lab work 1.2 new$documnet#~ test2
gurprem@ubuntu:~/gurprem$ rmdir -v gg1
rmdir: removing directory, gg1
rmdir: failed to remove gg1: Directory not empty

typescript~
typescript-lab1

Command CP:
Copies files and directories.
SYNTAX:
cp [OPTION]...SOURCE DESTINATION
USAGE:
1.
gurprem@ubuntu:~/gurprem$ cp f1 f2
gurprem@ubuntu:~/gurprem$ cat f1
in f1!
gurprem@ubuntu:~/gurprem$ cat f2
in f1!
This command shall copy f1 contents into f2 and overwrite f2.
2.
gurprem@ubuntu:~/gurprem$ cp -v
f1 -> f2

f1 f2

Command MV:
The mv command is used to move or rename files.

UE123029

Page 36

SYNTAX:
mv [OPTIONS].. filename NewName
USAGE:
gurprem@ubuntu:~/gurprem$ mv f1 file1
gurprem@ubuntu:~/gurprem$ ls
demo~ f2~
file2~
gg2
new$documnet#
test
f1~
f4
file file1 lab work 1.2 new$documnet#~ typescript~
f2
file1 gg1
lab work 1.3 os1~
typescriptlab1this is my second file

Command DIFF:
Diff analyzes two files and prints the lines that are different. Essentially, it outputs
a set of instructions for how to change first file in order to make it identical to the
second file.

The first line of the diff output will contain:


line numbers corresponding to the first file,
a letter (a for add, c for change, or d for delete), and
line numbers corresponding to the second file.
Diff file1 file2
USAGE:
1.
gurprem@ubuntu:~/gurprem$ diff file1 file2
1c1,2
< hello world!!
--> hello world!!in os1!
> hello world!!in os1
NOTE :It tells here that line 1 in file1 needs to be changed according
to lines 1,2 of file2.

Command CD
The cd command, which stands for "change directory", changes the shell's current
working directory.
DESCRIPTION:
The cd command is one of the commands you will use the most at the command
line in linux. It allows you to change your present working directoryx. You use it to
move around within the hierarchy of your file system.

UE123029

Page 37

USAGE:
gurprem@ubuntu:~/gurprem cd Desktop
gurprem@ubuntu:~/gurprem/Desktop$ cd os
gurprem@ubuntu:~/gurprem/Desktop/os$ cd oslab1
gurprem@ubuntu:~/gurprem/Desktop/os/oslab1$
Command RMDIR:
Removes a directory.
SYNTAX:
rmdir [-p] directory ...
DESCRIPTION:
The rmdir utility removes the directory entry specified by each directory argument,
provided the directory is empty.
Arguments are processed in the order given. In order to remove both a parent
directory and a subdirectory of that parent, the subdirectory must be specified first
so the parent directory is empty when rmdir tries to remove it.
OPTIONS:
--ignore-fail-on-nonempty
-p
-v, --verbose
--help
--version

ignore any failure which occurs solely because a directory is


non-empty.
Each directory argument is treated as a pathname of which
all components will be removed, if they are empty, starting
with the lastmost component. (See rm for fully nondiscriminant recursive removal.)
Display verbose information for every directory processed.
Display a help message, and exit.
Output version information, and exit.

USAGE:
gurprem@ubuntu:~/gurprem/Desktop/os/oslab1$ rmdir mydir
Removes the directory mydir.
Command whoami
Whoami Prints current user ID. Equivalent to id -un.
USAGE:
gurprem@ubuntu:~/gurprem$ whoami
gurprem

COMMAND MORE:
Displays text, one screen at a time.

UE123029

Page 38

DESCRIPTION:
more is a filter for paging through text one screen at a time. It does not provide as
many options or enhancements as less, but is nevertheless quite useful and simple
to use.
Options
-num
Sets the number of lines that makes up a screenful. lines must be an integer.
lines
Causes more to count logical, rather than screen lines (i.e., long lines are not
-f
wrapped).
Do not scroll. Instead, clear the whole screen and then display the text. This
-p
option is switched on automatically if the more executable is named page.
Do not scroll. Instead, paint each screen from the top, clearing the remainder
-c
of each line as it is displayed.
-s
Squeeze multiple blank lines into one blank line.
-u
Do not display underlines.
+/strin Search for the string string, and advance to the first line containing string
g
when the file is displayed.
+num Start displaying text at line number num.

USAGE:
1.more +3 myfile
Display the contents of file myfile, beginning at line 3.
gurprem@ubuntu:~/gurprem$ more +3 myfile
this is line 3
this is line 4
this is last line
2.more +/"super" myfile
Display the contents of file myfile, beginning at the first line
containing the string "super".
gurprem@ubuntu:~/gurprem$
...skipped

more +/"supercomputers" myfile

Systems can be found on almost any device that contains a computer


from cellular phones and video game consoles to supercomputers and web
servers.
3. ls | more

UE123029

Page 39

List the contents of the current directory with ls, using more to
display the list one screen at a time.
gurprem@ubuntu:~/gurprem $ ls|more
area1
area1~
bubble
demo~
demo1
myfile
myfile~
4.-s

Squeeze multiple blank lines into one blank line

gurprem@ubuntu:~/gurprem $ more -s myfile11


this is myfile11.
Hello there there
How are you
Bye...
5. -u do not display underlines.
gurprem@ubuntu:~/gurprem $ more -u myfile2
this is myfile11.
Hello there there
How are you
Bye...
Command HEAD:
head - output the first part of files
SYNTAX:
head [OPTION]... [FILE]...
DESCRIPTION :
Print the first 10 lines of each FILE to standard output. With more than one FILE,
precede each with a header giving the file name. With no FILE, or when FILE is -,
read standard input.
Mandatory arguments to long options are mandatory for short options too.
OPTIONS:
-c
--bytes=[-]K print the first K bytes of each file; with the leading

UE123029

Page 40

-n
-q
(default)
-v

'-', print all but the last K bytes of each file


--lines=[-]K print the first K lines instead of the first 10; with the
leading '-', print all but the last K lines of each file
never print headers giving file names
always print headers giving file names

1.
gurprem@ubuntu:~/gurprem$ head labwork
sane-v4l (5)
- SANE interface for Video for Linux API
savelog (8)
- save a log file
scalb (3)
- multiply floating-point number by integral
pow...
scalbf (3)
- multiply floating-point number by integral
pow...
scalbl (3)
- multiply floating-point number by integral
pow...
scalbln (3)
- multiply floating-point number by integral
pow...
scalblnf (3)
- multiply floating-point number by integral
pow...
scalblnl (3)
- multiply floating-point number by integral
pow...
scalbn (3)
- multiply floating-point number by integral
pow...
scalbnf (3)
- multiply floating-point number by integral
pow...
2. c
gurprem@ubuntu:~/gurprem$ head -c 10 labwork
sane-v4l (
sane-v4l (gurprem@ubuntu:~/gurprem$ head -c -5 file1
in file1
i am a bad boy!
hello !
lets get started!
gurprem@ubuntu:~/gurprem$ head file1
in file1
i am a bad boy!
hello !
lets get started!
bye!
gurprem@ubuntu:~/gurprem$

UE123029

Page 41

3.-n
gurprem@ubuntu:~/gurprem$ head -n 2 file1
in file1
i am a bad boy!
gurprem@ubuntu:~/gurprem$ head -n -4 file1
in file1
i am a bad boy!
hello !
4.-q
gurprem@ubuntu:~/gurprem$ head -q file1
in file1
i am a bad boy!
hello !
lets get started!
bye!
5. v
gurprem@ubuntu:~/gurprem$ head -v file1
==> file1 <==
in file1
i am a bad boy!
hello !
lets get started!
bye!

COMMAND

TAIL:

tail - output the last part of files


SYNTAX:
tail [OPTION]... [FILE]...
DESCRIPTION:
Print the last 10 lines of each FILE to standard output. With more than one
FILE, precede each with a header giving the file name. With no FILE, or when FILE
is -, read standard input.Mandatory arguments to long options are mandatory for
short options too.
-c
-n
-q
(default)

UE123029

--bytes=K output the last K bytes; alternatively, use -c +K to


output bytes starting with the Kth of each file
output the last K lines, instead of the last 10; or use -n +K to output
lines starting with the Kth
never print headers giving file names

Page 42

-v

always print headers giving file names

1.-c
gurprem@ubuntu:~/gurprem$ tail -c 5 file1
bye!
gurprem@ubuntu:~/gurprem$ tail -c +5 file1
ile1
i am a bad boy!
hello !
lets get started!
bye!
2.n
gurprem@ubuntu:~/gurprem$ tail -n 4 file1
lets get started!
bye!
gurprem@ubuntu:~/gurprem$ tail -n +4 file1
hello !
lets get started!
bye!
gurprem@ubuntu:~/gurprem$
3.-q
gurprem@ubuntu:~/gurprem$ tail -q
in file1
i am a bad boy!

file1

hello !
lets get started!
bye!
4 . v
gurprem@ubuntu:~/gurprem$ tail -v
==> file1 <==
in file1
i am a bad boy!

file1

hello !
lets get started!

UE123029

Page 43

bye!

COMMAND LESS:
less - opposite of more
OPTIONS:
option
-n
-N
--pattern=pattern
-s
+x

description
Suppress numbering
Display numbering
Match according to pattern
Squeeze the multiple blank lines into one
Skip x lines from beggining

Usage:
1 gurprem@ubuntu:~$ less myfile11 or less n myfile11
this is line 1
this is line 2
this is line 3
this is line 4
this is line 5
this is line 6

this is line 7
it is line 8
it is line 9
2 gurprem@ubuntu:~$.less N myfile11
1 this is line 1
2 this is line 2
3 this is line 3
4 this is line 4
5 this is line 5
6 this is line 6
7
8
9
10
11 this is line 7
12 it is line 8
13 it is line 9 and \n this is line 10
14

UE123029

Page 44

3. gurprem@ubuntu:~$ less s N myfile11


1 this is line 1
2 this is line 2
3 this is line 3
4 this is line 4
5 this is line 5
6 this is line 6
7
11 this is line 7
12 it is line 8
13 it is line 9
14
4 gurprem@ubuntu:~$.less s N pattern=this myfile11
1 this is line 1
2 this is line 2
3 this is line 3
4 this is line 4
5 this is line 5
6 this is line 6
7
11 this is line 7
12 it is line 8
13 it is line 9
14
5 gurprem@ubuntu:~$
less +4 N myfile11
4 this is line 4
5 this is line 5
6 this is line 6
7
8
9
10
11 this is line 7
12 it is line 8
13 it is line 9
6. gurprem@ubuntu:~$ less -4 -N +/"it" myfile11
12 it is line 8
13 it is line 9 and \n this is line 10
14
7. gurprem@ubuntu:~$ ls|less
area
Desktop
Documents
Downloads
examples.desktop
hello
hello.c

UE123029

Page 45

Music
myfile
myfile1
myfile1!
myfile11
myfile11~
Pictures
prog1
prog2
prog3
prog4
prog5
Public
Templates
Videos

EXPERIMENT 3
AIM:To implement various linux/unix commands :
Sort,comm.,diff,grep,whatis,whereis,gzip,gunzip,tar,zip,unzip,chmod
COMMAND SORT:
sort - sort lines of text files

SYNTAX:
sort [OPTION]... [FILE]...
sort [OPTION]... --files0-from=F
DESCRIPTION
Write sorted concatenation of all FILE(s) to standard output.
-b
(default)
UE123029

Ignore leading blanks

Page 46

-d
(Default)
-R
-sort==WO
RD
-c
-m
-o

consider only blanks and alphanumeric characters


Compare according to random hash of keys
sort according to WORD: general-numeric -g,
check for sorted input; do not sort
Merge sorted files, do not sort
write result to FILE instead of standard output

USAGE:
1.
gurprem@ubuntu:~/gurprem$ sort

file1

bye!
hello !
i am a bad boy!
in file1
lets get started!
gurprem@ubuntu:~/gurprem$ sort

-b file1

bye!
hello !
i am a bad boy!
in file1
lets get started!
2.
gurprem@ubuntu:~/gurprem$ sort
1bye!
hello !
i am a bad boy!
in file1
lets get started!
3.-R
gurprem@ubuntu:~/gurprem$ sort
LETS GET STARTED!
i am a bad boy!
1bye!
in file1
HELLO!

-d file1

-R file1

4.--sort

UE123029

Page 47

gurprem@ubuntu:~/gurprem$ sort
HELLO!
i am a bad boy!
in file1
LETS GET STARTED!
1bye!

--sort=g file1

5. c
gurprem@ubuntu:~/gurprem$ sort -c file1
sort: file1:2: disorder:
i am a bad boy!
gurprem@ubuntu:~/gurprem$ sort -o f11 file1
gurprem@ubuntu:~/gurprem$ sort f11
1bye!
HELLO!
i am a bad boy!
in file1
LETS GET STARTED
6. m
gurprem@ubuntu:~/gurprem$ sort -m f4 f6
1
1234
2
3
3
45
45
45
abc
b3
hey
hgj
hgn
yz

EXP -3
COMMAND WHATIS
whatis command is used to know about what a particular command is applied for, and it displays
the keyword, use of the keyword in a single line.
SYNTAX:
whatis keyword(s)
USAGE:

gurprem@ubuntu:~/gurprem$ whatis man


UE123029

Page 48

man (1)
manuals
man (7)

- an interface to the on-line reference


- macros to format man pages

Command whereis
whereis command is used to search and locate source or binary files in the system. This
command will return the path where the files are located.
SYNTAX:
whereis [OPTIONS] files
OPTIONS:
-b
-m
-s
-u
-B
-M
-S

Search only for binary file.


Locate only for manual sections.
Search only for source file.
Locate for unusual entries.
Change or otherwise limit the places where whereis searches for
binary files.
Change or otherwise limit the places where whereis searches for
manual sections.
Change or otherwise limit the places where whereis searches for
sources.

USAGE:
1.
gurprem@ubuntu:~/gurprem$ whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz
2.
gurprem@ubuntu:~/gurprem$ whereis -b ls
ls: /bin/ls
3.
gurprem@ubuntu:~/gurprem$ whereis -s ls
ls:
gurprem@ubuntu:~/gurprem$ whereis -m ls
ls: /usr/share/man/man1/ls.1.gz
gurprem@ubuntu:~/gurprem$

UE123029

Page 49

Command gzip and gunzip


gzip and gunzip are used to compress or expand files.
SYNTAX:
gzip [ -acdfhlLnNrtvV19 ] [-S suffix] [ name ... ]
gunzip [ -acfhlLnNrtvV ] [-S suffix] [ name ... ]
DESCRIPTION:
gzip reduces the size of the named files .Whenever possible, each file is
replaced by one with the extension .gz , while keeping the same ownership
modes, access and modification times.
If the compressed file name is too long for its file system, gzip truncates it.
gzip attempts to truncate only the parts of the file name longer than 3
characters. (A part is delimited by dots.) If the name consists of small parts
only, the longest parts are truncated.
By default, gzip keeps the original file name and timestamp in the
compressed file. These are used when decompressing the file with the -N
option. This is useful when the compressed file name was truncated or when
the
time
stamp
was
not
preserved
after
a
file
transfer.
Compressed files can be restored to their original form using gzip -d or
gunzip or zcat. If the original name saved in the compressed file is not
suitable for its file system, a new name is constructed from the original one
to make it legal
gunzip takes a list of files on its command line and replaces each file whose
name ends with .gz, -gz, .z, -z, or _z (ignoring case) and which begins with
the correct magic number with an uncompressed file without the original
extension. gunzip also recognizes the special extensions .tgz and .taz as
shorthands for .tar.gz and .tar.Z respectively. When compressing, gzip uses
the .tgz extension if necessary instead of truncating a file with a .tar
extension.
gunzip can currently decompress files created by gzip, zip, compress,
compress
-H
or
pack.
Files created by zip can be uncompressed by gzip only if they have a single
UE123029

Page 50

member compressed with the 'deflation' method. This feature is only


intended to help conversion of tar.zip files to the tar.gz format. To extract a
zip file with a single member, use a command like:
gunzip <foo.zip
or:
gunzip -S .zip foo.zip
To extract zip files with several members, use unzip instead of gunzip.
zcat is identical to gunzip -c. (On some systems, zcat may be installed as
gzcat to preserve the original link to compress.) zcat uncompresses either
a list of files on the command line or its standard input and writes the
uncompressed data on standard output. zcat will uncompress files that have
the correct magic number whether they have a .gz suffix or not.
OPTIONS
ASCII text mode: convert end-of-lines using local conventions.
This option is supported only on some non-Unix systems. For
-a, --ascii
MSDOS, CR LF is converted to LF when compressing, and LF is
converted to CR LF when decompressing.
Write output on standard output; keep original files unchanged.
If there are several input files, the output consists of a sequence
-c, --stdout,
of independently compressed members. To obtain better
--to-stdout
compression, concatenate all input files before compressing
them.
-d,
--decompress
Decompress.
,
--uncompress
Force compression or decompression even if the file has multiple
links or the corresponding file already exists, or if the
compressed data is read from or written to a terminal. If the
input data is not in a format recognized by gzip, and if the option
-f, --force
--stdout is also given, copy the input data without change to the
standard output: let zcat behave as cat. If -f is not given, and
when not running in the background, gzip prompts to verify
whether an existing file should be overwritten.
For each compressed file, list the following fields:
-l, --list

UE123029

compressed size
uncompressed

size of the compressed file


size of the uncompressed file
Page 51

size
compression ratio (0.0% if
unknown)
uncompressed_na name of the uncompressed
me
file
The uncompressed size is given as -1 for files not in gzip format,
such as compressed .Z files. To get the uncompressed size for
such a file, you can use:
ratio

zcat file.Z | wc -c
In combination with the --verbose option, the following fields are
also displayed:
method

compression method
the
32-bit
CRC
of
the
crc
uncompressed data
date
& time
stamp
for
the
time
uncompressed file
The compression methods currently supported are deflate,
compress, lzh (SCO compress -H) and pack. The crc is given as
ffffffff for a file not in gzip format.
-L, --license Display the gzip license and exit.
When compressing, do not save the original file name and
timestamp by default. (The original name is always saved if the
name had to be truncated.) When decompressing, do not restore
-n, --no-name the original file name if present (remove only the gzip suffix from
the compressed file name) and do not restore the original time
stamp if present (copy it from the compressed file). This option is
the default when decompressing.
When compressing, always save the original file name and time
stamp; this is the default. When decompressing, restore the
-N, --name
original file name and time stamp if present. This option is useful
on systems which have a limit on file name length or when the
time stamp has been lost after a file transfer.
-q, --quiet
Suppress all warnings.
Travel the directory structure recursively. If any of the file names
specified on the command line are directories, gzip will descend
-r, --recursive
into the directory and compress all the files it finds there (or
decompress them in the case of gunzip).
-S
.suf, When compressing, use suffix .suf instead of .gz. Any non-empty
--suffix .suf
suffix can be given, but suffixes other than .z and .gz should be
avoided to avoid confusion when files are transferred to other
UE123029

Page 52

systems.
When decompressing, add .suf to the beginning of the list of
suffixes to try, when deriving an output file name from an input
file name.
Verbose. Display the name and percentage reduction for each
-v, --verbose
file compressed or decompressed.
Version. Display the version number and compilation options
-V, --version
then quit.
Regulate the speed of compression using the specified digit #,
where -1 or --fast indicates the fastest compression method (less
-#,
--fast,
compression) and -9 or --best indicates the slowest compression
--best
method (best compression). The default compression level is -6
(that is, biased towards high compression at expense of speed).

USAGE:
1.gurprem@ubuntu:~/gurprem$ gzip backup.tar
Compresses
the
tar
archive
backup.tar,
backup.tar.gz.

and

renames

it

renaming

it

2.gurprem@ubuntu:~/gurprem$ gunzip backup.tar.gz


Uncompresses
backup.tar.

the

gzipped

file

backup.tar.gz,

3.gurprem@ubuntu:~/gurprem$ gzip -r backupfolder


Recursively compresses all files in the folder backupfolder, and all files in
any subdirectories of that folder, and gives them the extension .gz.
4.gurprem@ubuntu:~/gurprem$ gunzip -r backupfolder
Recursively uncompresses all gzipped files in the folder backupfolder, and removes the
extension .gz from the filenames.
5.gurprem@ubuntu:~/gurprem$ gzip -c myfile.txt >myfile.txt.gz
Compresses the file myfile.txt, writing the compressed file data to standard
output, which in this case is redirected to a file, myfile.txt.gz. The result is
that the original file and the gzipped file will both exist after the command is
run.

UE123029

Page 53

6.gurprem@ubuntu:~/gurprem$ gunzip -c myfile.txt.gz > myfile.txt


Uncompresses the gzipped file myfile.txt.gz to standard output, which in
this case is redirected to the file myfile.txt. If myfile.txt already exists, the
shell will prompt you to overwrite it (or not).
7.gurprem@ubuntu:~/gurprem$ ls
abd
dir f3
f8
typescript
chmodfile f1
f4
f9
typescript~
d1
f1~ f4~ file1~
typescript-lab1
d2
f10 f5
file1.gz
d4
f11 f5~ file2~
demo~
f2
f6
file file
demo3
f2~ f7
gg1

gg2

mask4

labwork

mask5

lab work 1.3

new

maks5
mask1
mask2
mask3

new$documnet#
new$documnet#~
os1~
test

gurprem@ubuntu:~/gurprem$ gzip mask4


gurprem@ubuntu:~/gurprem$ ls
abd
dir f3
f8
test
chmodfile f1
f4
f9
typescript
d1
f1~ f4~ file1~
typescript~
d2
f10 f5
file1.gz
typescript-lab1
d4
f11 f5~
demo~
f2
f6
demo3
f2~ f7

file2~
file file
gg1

gg2

mask4.gz

labwork

mask5

lab work 1.3

new

maks5

newDir

mask1
mask2
mask3

new$documnet#
new$documnet#~
os1~

8. to redirect c
gurprem@ubuntu:~/gurprem$ gzip
gurprem@ubuntu:~/gurprem$
gurprem@ubuntu:~/gurprem$ ls
abd
dir f3
f8
os1~
chmodfile f1
f4
f9
test
d1
f1~ f4~ file1~
typescript
d2
f10 f5
file1.gz
typescript~

UE123029

-c new > new.gz


gg2

mask4.gz

labwork

mask5

lab work 1.3

new

maks5

newDir

Page 54

d4
f11 f5~
typescript-lab1
demo~
f2
f6
demo3
f2~ f7

file2~

mask1

new$documnet#

file file
gg1

mask2
mask3

new$documnet#~
new.gz

9. k to keep input files

gurprem@ubuntu:~/gurprem$ gzip
gurprem@ubuntu:~/gurprem$ ls
abd
dir f3
f7
new.gz
chmodfile f1
f3.gz f8
os1~
d1
f1~ f4
f9
test
d2
f10 f4~
file1~
typescript
d4
f11 f5
file1.gz
typescript~
demo~
f2
f5~
file2~
typescript-lab1
demo3
f2~ f6
file file

-k f3
gg1

mask3

gg2

mask4.gz

labwork

mask5

lab work 1.3

new

maks5

newDir

mask1

new$documnet#

mask2

new$documnet#~

10. f for forcibly compress

gurprem@ubuntu:~/gurprem$ gzip

-k -l

f7 f8

gzip: f8: unexpected end of file


gurprem@ubuntu:~/gurprem$ gzip -k -f

f7 f8

gzip: f7: not in gzip format

11. l to display statistics of the file

gurprem@ubuntu:~/gurprem$ gzip -l file1


compressed
uncompressed
106
88

ratio uncompressed_name
6.8% file1

12. L display gzip license

gurprem@ubuntu:~/gurprem$ gzip -L file1


gzip 1.6
Copyright (C) 2007, 2010, 2011 Free Software Foundation, Inc.
Copyright (C) 1993 Jean-loup Gailly.
This is free software. You may redistribute copies of it under
the terms of

UE123029

Page 55

the
GNU
General
Public
<http://www.gnu.org/licenses/gpl.html>.
There is NO WARRANTY, to the extent permitted by law.

License

13. works for multiple files

gurprem@ubuntu:~/gurprem$
gurprem@ubuntu:~/gurprem$
abd
f1~
f4
new$documnet#~
chmodfile f10
f4~
d1
f10.gz f5
d2
f11
f5~
d4
f11.gz f6
demo~
f2
f7
typescript~
demo3
f2~
f7.gz
typescript-lab1
dir
f3
f8
f1
f3.gz
f8.gz

gzip -k f10 f11


ls
f9
maks5
file1~
file1.gz
file2~
file file
gg1

mask1
mask2
mask3
mask4.gz
mask5

new.gz
os1~
test
typescript

gg2

new

labwork
lab work 1.3

newDir
new$documnet#

14. giving our own extension

gurprem@ubuntu:~/gurprem$
gurprem@ubuntu:~/gurprem$
abd
f1~
f4
chmodfile f10
f4~
d1
f10.gz f5
d2
f11
f5~
d4
f11.gz f6
demo~
f2
f7
demo3
f2~
f7.gz
dir
f3
f8
lab1
f1
f3.gz
f8.gz

gzip -S .gg -k f9
ls
f9
lab work 1.3
f9.gg
maks5
file1~
mask1
file1.gz
mask2
file2~
mask3
file file mask4.gz
gg1
mask5
gg2
new
labwork

newDir

15. for a directory

gurprem@ubuntu:~/gurprem$ gzip -r newDir


gurprem@ubuntu:~/gurprem$ cd newDir
gurprem@ubuntu:~/gurprem/newDir$ ls
d1.gz d2.gz
10 d to decompress

gurprem@ubuntu:~/gurprem$ gzip -d f10.gz


UE123029

Page 56

new$documnet#
new$documnet#~
new.gz
os1~
test
typescript
typescript~
typescript-

gzip: f10 already exists; do you wish to overwrite (y or n)? Y


16. decompressing a directory

gurprem@ubuntu:~/gurprem$ gzip -d -r newDir


gurprem@ubuntu:~/gurprem$ cd newDir
gurprem@ubuntu:~/gurprem/newDir$ ls
d1 d2

COMMAND TAR:
Tar stores and extracts files from a tape or disk archive.
1. Cvf
c Creates a new .tar archive file.
v Verbosely show the .tar file progress.
f File name type of the archive file.

gurprem@ubuntu:~/gurprem$ tar -cvf


newDir/
newDir/d2
newDir/tarfile.tar
newDir/compressed.gz
newDir/d1
gurprem@ubuntu:~/gurprem$ ls
abd
f1
f4~
new$documnet#~
chmodfile
f1~
f5~
compressed.gz f10
f5.gz
d1
f11.gz f6
tarfile.tar
d2
f2
f7
d4
f2~
f7.gz
typescript
demo~
f3
f8
typescript~
demo3
f3.gz
f8.gz
typescript-lab1
dir
f4
f9

f9.gg

maks5

file1~
file1.gz
file2~

mask1
mask2
mask3

new.gz
os1~

file file
gg1

mask4.gz
mask5

test

gg2

new

labwork

newDir

lab work 1.3

new$documnet#

COMMAND CHMOD:
change file mode bits
SYNTAX:
chmod [OPTION]... MODE[,MODE]... FILE...

UE123029

tarfile.tar newDir

Page 57

chmod [OPTION]... OCTAL-MODE FILE...


chmod [OPTION]... --reference=RFILE FILE...

Option:
-c
-v

Description:
like verbose but report only when a change is made
output a diagnostic for every file processed

USAGE:
1.default for directory -775
drwxrwxr-x 2 gurprem gurprem

4096 Sep

7 01:43 gg1

default for file 664


2.gurprem@ubuntu:~/gurprem$ ls -l chmodfile
-rw-rw-r-- 1 gurprem gurprem 0 Sep 10 03:06 chmodfile
gurprem@ubuntu:~/gurprem$ chmod 552 chmodfile
gurprem@ubuntu:~/gurprem$ ls -l chmodfile
-r-xr-x-w- 1 gurprem gurprem 0 Sep 10 03:06 chmodfile
3.
gurprem@ubuntu:~/gurprem$ chmod a+w chmodfile
gurprem@ubuntu:~/gurprem$ ls -l chmodfile
-rwxrwx-w- 1 gurprem gurprem 0 Sep 10 03:06 chmodfile
4.
gurprem@ubuntu:~/gurprem$ chmod a=r chmodfile
gurprem@ubuntu:~/gurprem$ ls -l chmodfile
-r--r--r-- 1 gurprem gurprem 0 Sep 10 03:06 chmodfile
5.
gurprem@ubuntu:~/gurprem$ chmod a=r -c chmodfile //no message
because no change
gurprem@ubuntu:~/gurprem$ chmod a=rw -c chmodfile
mode of chmodfile changed from 0444 (r--r--r--) to 0666 (rw-rwrw-)
6. v
gurprem@ubuntu:~/gurprem$ chmod a=r chmodfile
gurprem@ubuntu:~/gurprem$ chmod a=r -v chmodfile
mode of chmodfile retained as 0444 (r--r--r--)

UE123029

Page 58

COMMAND comm
Compare two sorted files line-by-line.
SYNTAX
comm [OPTION]... FILE1 FILE2
Description
Compare sorted files FILE1 and FILE2 line-by-line.
With no options, comm produces three-column output. Column one contains
lines unique to FILE1, column two contains lines unique to FILE2, and
column three contains lines common to both files. Each of these columns can
be suppressed individually with options.

Options
-1
-2
-3
--check-order
--nocheck-order
--output-delimiter=STR
--help
--version

suppress column 1 (lines unique to FILE1)


suppress column 2 (lines unique to FILE2)
suppress column 3 (lines that appear in both files)
check that the input is correctly sorted, even if all input lines are pairable
do not check that the input is correctly sorted
separate columns with string STR
display a help message, and exit.
output version information, and exit.

USAGE:
comm -12 gur1 gur2
Print only the lines present in both gur1 and gur2.
gurprem@ubuntu:~/gurprem$ comm gur1 gur2
hello
i am from chandigarh
i am from hoshiarpur
i am not much used to crowded places.
i like travelling and explore new places
my name is gurprem
gurprem@ubuntu:~/gurprem$ comm -12 gur1 gur2
hello
UE123029

Page 59

i am not much used to crowded places.


i like travelling and explore new places
my name is gurprem
comm -3 gur1 gur2
Print only the lines that are present in gur1 and not gur2, and vice versa.
gurprem@ubuntu:~/gurprem/Desktop/os/oslab18nov$ comm -3 gur1 gur2
i am from chandigarh
i am from dehradun

UE123029

Page 60

EXPERIMENT 4
AIM:To implement various linux/unix commands :
Tty,rm,bc,tr,cut,umask,free
COMMAND TTY:
Print the file name of the terminal connected to standard input.
SYNTAX:
tty [OPTION]...
OPTIONS:
-s, --silent,
--quiet
--help
--version

Print nothing, only return an


exit status.
display this help and exit.
output version information and
exit.

USAGE:
tty
Running tty by itself will display the current tty session as shown below:
gurprem@ubuntu:~$ tty
/dev/pts/0

COMMAND: rm
The rm command removes (deletes) files or directories.
SYNTAX:
rm. [OPTION]... FILE...
Description
rm removes each specified FILE. By default, it does not remove
directories; seeRemoving Directories below for details.

UE123029

Page 61

Options
-f, --force

Ignore non-existant files, and never prompt before removing.

-i

Prompt before every removal.

-I

Prompt once before removing more than three files, or when


removing recursively.

-r, -R, --recursive

Remove directories and their contents recursively.

-v, --verbose

Verbose mode; explain at all times what is being done.

USAGE:
1.
gurprem@ubuntu:~/gurprem$ rm demo
2.
rm: cannot remove fle1: No such file or directory
gurprem@ubuntu:~/gurprem$ rm -i file1
rm: remove regular file file1? n
3.
gurprem@ubuntu:~/gurprem$ rm -I f1 f2 f3 f4
rm: remove all arguments? N
4.
gurprem@ubuntu:~/gurprem$ rm -v f3
removed f3

COMMAND bc
bc is an arbitrary-precision language for performing math calculations.

DESCRIPTION
bc is a language that supports arbitrary-precision numbers, meaning that it
delivers accurate results regardless of how large (or very very small) the
numbers are.
It has an interactive mode, accepting input from the terminal and providing
calculations on request. As a language, its syntax is similar to the C
UE123029

Page 62

programming language. A standard math library is available using a


command line option. If requested, the math library is defined before
processing any files.
bc starts by processing code from all the files listed on the command line in
the order listed. After all files have been processed, bc reads from the
standard input. All code is executed as it is read.
SYNTAX
bc [ -hlwsqv ] [long-options] [ file ... ]
USAGE:
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software
Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
12*4
48
34/5
6
5/6
0
5.0/6.0
0

Command : tr
The tr command automatically translates (substitutes) sets of characters.
Syntax
tr [-Ccsu] string1 string2
Description
The tr utility copies the standard input to the standard output with
substitution or deletion of selected characters.
UE123029

Page 63

Options
-C
-c
-d
-s
-u

Complement the set of characters in string1, that is "-C ab" includes every
character except for 'a' and 'b'.
Same as -C but complement the set of values in string1.
Delete characters in string1 from the input.
Squeeze multiple occurrences of the characters listed in the last operand
(either string1 or string2) in the input into a single instance of the
character. This occurs after all deletion and translation is completed.
Guarantee that any output is unbuffered.

When "[:lower:]" appears in string1 and "[:upper:]" appears in the same


relative position in string2, it represents the characters pairs from the
toupper mapping in the LC_CTYPE category of the current locale. When
"[:upper:]" appears in string1 and "[:lower:]" appears in the same relative
position in string2, it represents the characters pairs from the tolower
mapping in the LC_CTYPE category of the current locale.
With the exception of case conversion, characters in the classes are in
unspecified order.
For specific information as to which ASCII characters are included in these
classes, see ctype and related manual pages.
"[=equiv=]" Represents all characters belonging to the same equivalence
class as equiv, ordered by their encoded values.
[#*n] Represents n repeated occurrences of the character represented by #.
This expression is only valid when it occurs in string2. If n is omitted, or is
zero, it is be interpreted as large enough to extend string2 sequence to the
length of string1. If n has a leading zero, it is interpreted as an octal value,
otherwise, it is interpreted as a decimal value.
USAGE:
gurprem@ubuntu:~/gurprem$ tr -cs "[:alpha:]" "\n" < file1
Create a list of the words in file1, one per line, where a word
is taken to be a maximal string of letters.
gurprem@ubuntu:~/gurprem$ tr "[:lower:]" "[:upper:]" < file1
Translate the contents of file1 to uppercase.

UE123029

Page 64

gurprem@ubuntu:~/gurprem$ tr -cd "[:print:]" < file1


Remove all non-printable characters from file1.
gurprem@ubuntu:~/gurprem$ tr "[=e=]" "e"
Remove all "diacritical" marks from accented versions of the
letter e.

Command CUT
remove sections from each line of files
SYNOPSIS
cut OPTION... [FILE]...
DESCRIPTION
Print selected parts of lines from each FILE to standard output.
-c
select only these characters in that line
-d
use DELIM instead of TAB for field delimiter
-f
select only these fields; also print any line that contains
no delimiter character, unless the -s option is specified
-complement the set of selected bytes, characters or fields
complement
-s
do not print lines not containing delimiters
1. c
gurprem@ubuntu:~/gurprem$ cut -c 3 file1
i
L
T
Y

//3rd character

//3 and 4th char from each line


gurprem@ubuntu:~/gurprem$ cut -c 3,4 file1
f
i
LL
TS
ye
// 4th to 7th character
gurprem@ubuntu:~/gurprem$ cut -c 4-7 file1
file
am
LO!
S GE

UE123029

Page 65

e!
//3rd character onwards
gurprem@ubuntu:~/gurprem$ cut -c 3- file1
file1
i
am a bad boy!
LLO!
TS GET STARTED!
ye!
// upto 3rd character
gurprem@ubuntu:~/gurprem$ cut -c -3
in
i
HEL
LET
1by

file1

//whole line
gurprem@ubuntu:~/gurprem$ cut -c 1- file1
in
file1
i am a bad boy!
HELLO!
LETS GET STARTED!
1bye!
2. d f
gurprem@ubuntu:~/gurprem$ cut -d' ' -f2 file1
file1
HELLO!
GET
1bye!
//use

as delimiter

-f
gurprem@ubuntu:~/gurprem$ cut
in
i
HELLO!
LETS GET STARTED!
1bye!

-f1 file1

// use tab as delimiter


3. complement
gurprem@ubuntu:~/gurprem$ cut --complement
file1
am a bad boy!

UE123029

Page 66

-f1 file1

HELLO!
LETS GET STARTED!
1bye!
gurprem@ubuntu:~/gurprem$ cut -f1 file1
in
i
HELLO!
LETS GET STARTED!
1bye!
gurprem@ubuntu:~/gurprem$ cat file1
in
file1
i am a bad boy!
HELLO!
LETS GET STARTED!
1bye!
4. s
gurprem@ubuntu:~/gurprem$ cut -s -f1 file1
in
i
gurprem@ubuntu:~/gurprem$ cat file1
in
file1
i am a bad boy!
HELLO!
LETS GET STARTED!
1bye!
??? mention pipe command and diff btw pipe and ;

COMMAND TEE:
read from standard input and write to standard output and files
SYNOPSIS
tee [OPTION]... [FILE]...
DESCRIPTION
Copy standard input to each FILE, and also to standard output.
Option:
Description:
-a
append to the given FILEs, do not overwrite
1.-a
gurprem@ubuntu:~/gurprem$ tee -a file1
adding text to file1
adding text to file1
^C
gurprem@ubuntu:~/gurprem$ cat file1
in
file1
i am a bad boy!
HELLO!
LETS GET STARTED!
1bye!

UE123029

Page 67

adding text to file1


1. Without options overwrites.
gurprem@ubuntu:~/gurprem$ cat f2
z
k
r
h
p

q
gurprem@ubuntu:~/gurprem$ tee f2
adding to f2
adding to f2
gurprem@ubuntu:~/gurprem$ cat f2
adding to f2
2. Using cut tee
gurprem@ubuntu:~/gurprem$ cut -c 3-5 file1 | tee f2
fi
i
LLO
TS
ye!
din
ain
gurprem@ubuntu:~/gurprem$ cat f2
fi
i
LLO
TS
ye!
din
ain
3. The following command (with the help of tee command) writes the
output both to the screen (stdout) and to the file.
gurprem@ubuntu:~/gurprem$ ls -l | tee f3
total 560
-rw-rw-r-- 1 gurprem gurprem
44 Sep 2 13:15 demo~
-rw-rw-r-- 1 gurprem gurprem
48 Sep 3 02:58 f1
-rw-rw-r-- 1 gurprem gurprem
38 Sep 3 02:57 f1~
-rw-rw-r-- 1 gurprem gurprem
0 Sep 3 04:01 f10
-rw-rw-r-- 1 gurprem gurprem
58 Sep 6 12:40 f11
-rw-rw-r-- 1 gurprem gurprem
29 Sep 6 23:59 f2
-rw-rw-r-- 1 gurprem gurprem
7 Sep 2 23:29 f2~
-rw-rw-r-- 1 gurprem gurprem
0 Sep 7 00:05 f3
-rw-rw-r-- 1 gurprem gurprem
8 Sep 3 03:54 f4

UE123029

Page 68

-rw-rw-r-- 1 gurprem gurprem


21 Sep
-rw-rw-r-- 1 gurprem gurprem
24 Sep
-rw-rw-r-- 1 gurprem gurprem
18 Sep
-rw-rw-r-- 1 gurprem gurprem
37 Sep
-rw-rw-r-- 1 gurprem gurprem
18 Sep
-rw-rw-r-- 1 gurprem gurprem
0 Sep
-rw-rw-r-- 1 gurprem gurprem
0 Sep
-rw-rw-r-- 1 gurprem gurprem
103 Sep
-rw-rw-r-- 1 gurprem gurprem
58 Sep
-rw-rw-r-- 1 gurprem gurprem
35 Aug
-rw-rw-r-- 1 gurprem gurprem
0 Aug
drwxrwxr-x 2 gurprem gurprem
4096 Sep
drw-rw-r-- 2 gurprem gurprem
4096 Aug
-rw-rw-r-- 1 gurprem gurprem 26409 Aug
-rw-rw-r-- 1 gurprem gurprem 29950 Aug
-rw-rw-r-- 1 gurprem gurprem
0 Sep
-rw-rw-r-- 1 gurprem gurprem
23 Aug
-rw-rw-r-- 1 gurprem gurprem
0 Aug
-rw-rw-r-- 1 gurprem gurprem
57 Aug
dr--r--rw- 2 gurprem gurprem
4096 Sep
-rw-rw-r-- 1 gurprem gurprem 16384 Sep
-rw-rw-r-- 1 gurprem gurprem 211370 Aug
-rw-rw-r-- 1 gurprem gurprem 197214 Aug
gurprem@ubuntu:~/gurprem$

3
3
3
3
3
3
3
6
6
27
31
2
27
31
31
6
31
31
27
2
3
31
31

03:30
03:58
03:10
03:22
03:42
03:46
03:46
23:56
12:35
02:53
04:39
23:13
03:38
05:03
05:15
11:17
04:16
04:12
03:11
13:46
03:44
05:05
05:08

f4~
f5
f5~
f6
f7
f8
f9
file1
file1~
file2~
file file
gg1
gg2
labwork
lab work 1.3
new
new$documnet#
new$documnet#~
os1~
test
typescript
typescript~
typescript-lab1

4. Ech0 + tee
gurprem@ubuntu:~/gurprem$ echo " echo and tee combined" | tee f1
echo and tee combined
gurprem@ubuntu:~/gurprem$ cat f1
echo and tee combined
5. Tee with multiple files at once //without displaying on stdoutput
gurprem@ubuntu:~/gurprem$
1234
45
gurprem@ubuntu:~/gurprem$
1234
45
1234
45
1234
45
gurprem@ubuntu:~/gurprem$
f4to f5
^C
gurprem@ubuntu:~/gurprem$
f4to f5
gurprem@ubuntu:~/gurprem$
f4to f5
gurprem@ubuntu:~/gurprem$
-a f4 to f5
^C

UE123029

cat f4
cat f5

tee f4>f5
cat f4
cat f5
tee -a f4>f5

Page 69

gurprem@ubuntu:~/gurprem$ cat f4
f4to f5
-a f4 to f5
gurprem@ubuntu:~/gurprem$ tee -a f4>f5
6. Writing to multiple files without displaying again on console
gurprem@ubuntu:~/gurprem$
in f4 f5
in f4 f5
^C
gurprem@ubuntu:~/gurprem$
in f4 f5
in f4 f5
gurprem@ubuntu:~/gurprem$
again in f4 f5
again in f4 f5
^C
gurprem@ubuntu:~/gurprem$
gurprem@ubuntu:~/gurprem$
in f4 f5
again in f4 f5
in f4 f5
again in f4 f5

tee f4 f5

cat f4 f5
tee -a f4 f5

cat f4 f5

COMMAND PASTE:
merge lines of files

SYNOPSIS
paste [OPTION]... [FILE]...
DESCRIPTION :
Write lines consisting of the sequentially corresponding lines from each
FILE, separated by TABs, to standard output. With no FILE, or when FILE is -,
read standard input
Option:
-s
-d

Description:
paste one file at a time instead of in parallel
reuse characters from LIST instead of TABs

USAGE:
1.
gurprem@ubuntu:~/gurprem$ paste f1
f1 file!
welcome !
great weather!
2.s
gurprem@ubuntu:~/gurprem$ paste file1 f1
in:file1
f1 file!

UE123029

Page 70

i:am a bad boy! welcome !


HELLO!
great weather!
LETS :GET STARTED!
1bye!
adding text to file1
again adding to file1
gurprem@ubuntu:~/gurprem$ paste -s file1 f1
in:file1
i:am a bad boy! HELLO!
LETS :GET STARTED!
1bye!
adding text to file1 again adding to file1
f1 file!
welcome ! great weather!
3.-d
gurprem@ubuntu:~/gurprem$ paste -d% -s file1
in:file1%i:am a bad boy!%HELLO!%LETS :GET STARTED!%1bye!%adding text
to file1%again adding to file1%
4.paste contents of 2 files to third file in parallel form
gurprem@ubuntu:~/gurprem$ paste f1
f1 file!
welcome !
great weather!
gurprem@ubuntu:~/gurprem$ paste f2
in file f2
at home
gurprem@ubuntu:~/gurprem$ paste f3
in f3
enjoy life
great journey
gurprem@ubuntu:~/gurprem$ paste f1 f2 > f3
gurprem@ubuntu:~/gurprem$ paste f1 f2
f1 file!
in file f2
welcome ! at home
great weather!
gurprem@ubuntu:~/gurprem$ paste f3
f1 file!
in file f2
welcome ! at home
great weather!
OR
gurprem@ubuntu:~/gurprem$ paste -d '#' f1 f2 > f3
gurprem@ubuntu:~/gurprem$ paste f3
f1 file!#in file f2
welcome !#at home
great weather!#
5.Paste contents of two files to third file in serial fashion.
gurprem@ubuntu:~/gurprem$ paste -s f1 f2 > f3
gurprem@ubuntu:~/gurprem$ paste f3
f1 file!
welcome ! great weather!
in file f2 at home

UE123029

Page 71

NAME

umask - set file mode creation mask

SYNOPSIS
#include <sys/types.h>
#include <sys/stat.h>
mode_t umask(mode_t mask);
1.
gurprem@ubuntu:~/gurprem$ umask 003
gurprem@ubuntu:~/gurprem$ touch mask1
gurprem@ubuntu:~/gurprem$ touch mask2
gurprem@ubuntu:~/gurprem$ ls -l mask1 mask2
-rw-rw-r-- 1 gurprem gurprem 0 Sep 10 03:35 mask1
-rw-rw-r-- 1 gurprem gurprem 0 Sep 10 03:35 mask2
3.
gurprem@ubuntu:~/gurprem$ umask 777
gurprem@ubuntu:~/gurprem$ touch mask3
gurprem@ubuntu:~/gurprem$ ls -l mask3
---------- 1 gurprem gurprem 0 Sep 10 03:38 mask3
In umask for files if on subtracting from 666 results in activation of
x then it wont take that option but the next one .

COMMAND FREE:

free - Display amount of free and used memory in the system

SYNTAX

free [options]

DESCRIPTION

free displays the total amount of free and used physical and swap memory
in the system, as well as the buffers used by the kernel.
Option:
-b
-k
-m
-g
-c --count
-l
-s
-t
UE123029

Description:
Display the amount of memory in bytes.
Display the amount of memory in kilo-bytes.
Display the amount of memory in mega-bytes.
Display the amount of memory in giga-bytes.
Dispaly the result count number of times
Show detailed low and high memory statistics.
Continuously display the result delay seconds apart.
Display a line showing the column totals.
Page 72

1.
gurprem@ubuntu:~/gurprem$ free
total
used
cached
Mem:
2063812
1227792
758248
-/+ buffers/cache:
390340
Swap:
1046524
0

free

shared

buffers

836020

13460

79204

shared

buffers

13783040

81104896

shared

buffers

13460

79204

free

shared

buffers

816

13

77

free

shared

buffers

1673472
1046524

2.-b
gurprem@ubuntu:~/gurprem$ free -b
total
used
free
cached
Mem:
2113343488 1257250816 856092672
776462336
-/+ buffers/cache: 399683584 1713659904
Swap:
1071640576
0 1071640576
3 k default
gurprem@ubuntu:~/gurprem$ free -k
total
used
free
cached
Mem:
2063812
1227820
835992
758264
-/+ buffers/cache:
390352
1673460
Swap:
1046524
0
1046524
1. -m
gurprem@ubuntu:~/gurprem$ free -m
total
used
cached
Mem:
2015
1199
740
-/+ buffers/cache:
381
Swap:
1021
0
2. g
gurprem@ubuntu:~/gurprem$ free -g
total
used
cached
Mem:
1
1
0
-/+ buffers/cache:
0
Swap:
0
0

1634
1021

1
0

3. h
gurprem@ubuntu:~/gurprem$ free -h

UE123029

Page 73

total
used
cached
Mem:
2.0G
1.2G
740M
-/+ buffers/cache:
381M
Swap:
1.0G
0B
gurprem@ubuntu:~/gurprem$

free

shared

buffers

816M

13M

77M

shared

buffers

13M

77M

1.6G
1.0G

4. c
gurprem@ubuntu:~/gurprem$ free -h -c 3
total
used
free
cached
Mem:
2.0G
1.2G
816M
740M
-/+ buffers/cache:
381M
1.6G
Swap:
1.0G
0B
1.0G
total
cached
Mem:
2.0G
740M
-/+ buffers/cache:
Swap:
1.0G

used

free

shared

buffers

1.2G

816M

13M

77M

381M
0B

1.6G
1.0G

total
cached
Mem:
2.0G
740M
-/+ buffers/cache:
Swap:
1.0G

used

free

shared

buffers

1.2G

816M

13M

77M

381M
0B

1.6G
1.0G

shared

buffers

13M

77M

shared

buffers

13460

79204

5. l
gurprem@ubuntu:~/gurprem$ free -h -l
total
used
free
cached
Mem:
2.0G
1.2G
816M
740M
Low:
859M
546M
313M
High:
1.1G
653M
502M
-/+ buffers/cache:
381M
1.6G
Swap:
1.0G
0B
1.0G
6. s
gurprem@ubuntu:~/gurprem$ free -s 0.5 -c3
total
used
free
cached
Mem:
2063812
1227952
835860
758268
-/+ buffers/cache:
390480
1673332
Swap:
1046524
0
1046524

UE123029

Page 74

total
cached
Mem:
2063812
758268
-/+ buffers/cache:
Swap:
1046524
total
cached
Mem:
2063812
758272
-/+ buffers/cache:
Swap:
1046524

used

free

shared

buffers

1228208

835604

13460

79204

390736
0

1673076
1046524

used

free

shared

buffers

1228164

835648

13460

79204

390688
0

1673124
1046524

shared

buffers

13M

77M

7. -t
gurprem@ubuntu:~/gurprem$ free -t -h
total
used
free
cached
Mem:
2.0G
1.2G
816M
740M
-/+ buffers/cache:
381M
1.6G
Swap:
1.0G
0B
1.0G
Total:
3.0G
1.2G
1.8G

EXPERIMENT 5

AIM: To implement various linux/unix commands :


Find,df,du,,ulimit,clear,ps,shutdown,finger
COMMAND FIND:
This command is used to find files and or folders within a Linux system.
find
command you type
find /usr/bin -name filename
this will search inside the /usr/bin directory (and any sub directories within the /usr/bindirectory)
for the
file named filename. To search the entire filing system including any mounted drives use
find / -name filename
and the
find
command will search every file system beginning in the root directory.The
find
command can also be used to find command to find files by date and the
find
command happily understand wild characters such as
*
and
UE123029

Page 75

?
Usage :
1.
gurprem@ubuntu:~$ find /home/gurprem/Desktop/d1
/home/gurprem/Desktop/d1
2. find file in current dir
gurprem@ubuntu:~$ find hello
hello
3.* displays all the files and dir in current dir
/home/gurprem
gurprem@ubuntu:~$ find *
area
Desktop
Desktop/d1
Documents
Downloads
examples.desktop
hello
hello.c
Music
myfile
myfile1
myfile1!
Pictures
prog1
prog2
prog3
prog4
prog5
Public
Templates
Videos
gurprem@ubuntu:~$ cd Desktop
gurprem@ubuntu:~/Desktop$ find *
d1
COMMAND ULIMIT
ulimit - get and set user limits
COMMAND DF:
report file system disk space usage
SYNTAX:
df [OPTION]... [FILE]...
DESCRIPTION
UE123029

Page 76

df displays the amount of disk space available on the file system containing
each file name argument. If no file name is given, the space available on all
currently mounted file systems is shown. Disk space is shown in 1K
blocks by default
1.
gurprem@ubuntu:~/gurprem$ df
Filesystem
1K-blocks
Used Available Use% Mounted on
/dev/sda1
19478204 3555092 14910632 20% /
none
4
0
4
0% /sys/fs/cgroup
udev
1022332
4
1022328
1% /dev
tmpfs
206384
1112
205272
1% /run
none
5120
0
5120
0% /run/lock
none
1031904
184
1031720
1% /run/shm
none
102400
48
102352
1% /run/user

COMMAND DU:
estimate file space usage
SYNOPSIS
du [OPTION]... [FILE]...
du [OPTION]... --files0-from=F
DESCRIPTION
Summarize disk usage of each FILE, recursively for directories.
1.
gurprem@ubuntu:~/gurprem$ du
4
./test
12
./gg1
4
./gg2
572 .
2. b and -c
gurprem@ubuntu:~/gurprem$ du -b -c
4096 ./test
4142 ./gg1
4096 ./gg2
498341
.
498341
total
3. h
gurprem@ubuntu:~/gurprem$ du -h
4.0K ./test
12K ./gg1
4.0K ./gg2
572K .

COMMAND CLEAR:
UE123029

Page 77

Clear the terminal screen

After executing clear command

UE123029

Page 78

COMMAND PS:
report a snapshot of the current processes.
SYNOPSIS
ps [options]
DESCRIPTION
ps displays information about a selection of the active processes. If you want a repetitive update
of the selection and the displayed information, use top(1) instead.
Option :
Description:
-e
Select all processes. Identical to -A.
-l
To display BSD long format.
-p x1 x2 or -pid
Select only processes with id x1 x2
-M
Add a column of security data

Usage:
gurprem@ubuntu:~$ ps
PID TTY
TIME
2752 pts/1
00:00:00
2923 pts/1
00:00:00
gurprem@ubuntu:~$ ps -e
PID TTY
TIME
1 ?
00:00:05
2 ?
00:00:00
3 ?
00:00:00

UE123029

CMD
bash
ps
CMD
init
kthreadd
ksoftirqd/0

Page 79

4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
25
26
27
28
29
30
31
43
44

?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?

00:00:00
00:00:00
00:00:01
00:00:02
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00

kworker/0:0
kworker/0:0H
kworker/u16:0
rcu_sched
rcu_bh
migration/0
watchdog/0
khelper
kdevtmpfs
netns
writeback
kintegrityd
bioset
kworker/u17:0
kblockd
ata_sff
khubd
md
devfreq_wq
khungtaskd
kswapd0
ksmd
khugepaged
fsnotify_mark
ecryptfs-kthrea
crypto
kthrotld
kworker/u16:1

.
.
.
gurprem@ubuntu:~$ ps -ef
UID
PID PPID C STIME TTY
root
1
0 1 02:18 ?
root
2
0 0 02:18 ?
root
3
2 0 02:18 ?
root
4
2 0 02:18 ?
root
5
2 0 02:18 ?
root
6
2 0 02:18 ?
root
7
2 0 02:18 ?
root
8
2 0 02:18 ?
root
9
2 0 02:18 ?
root
10
2 0 02:18 ?
root
11
2 0 02:18 ?
root
12
2 0 02:18 ?
root
13
2 0 02:18 ?
root
14
2 0 02:18 ?
root
15
2 0 02:18 ?
root
16
2 0 02:18 ?
root
17
2 0 02:18 ?
root
18
2 0 02:18 ?
root
19
2 0 02:18 ?

UE123029

TIME
00:00:05
00:00:00
00:00:00
00:00:00
00:00:00
00:00:01
00:00:02
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00

Page 80

CMD
/sbin/init
[kthreadd]
[ksoftirqd/0]
[kworker/0:0]
[kworker/0:0H]
[kworker/u16:0]
[rcu_sched]
[rcu_bh]
[migration/0]
[watchdog/0]
[khelper]
[kdevtmpfs]
[netns]
[writeback]
[kintegrityd]
[bioset]
[kworker/u17:0]
[kblockd]
[ata_sff]

root
20
root
21
root
22
root
25
gurprem@ubuntu:~$

2
2
2
2

0
0
0
0

02:18
02:18
02:18
02:18

gurprem@ubuntu:~$ ps -l
F S
UID
PID PPID C PRI
0 S 1000 2752 2736 0 80
0 R 1000 3279 2752 0 80
gurprem@ubuntu:~$ ps
PID TTY
STAT
2 ?
S
3 ?
S

-p 2
TIME
0:00
0:00

gurprem@ubuntu:~$ ps -M
LABEL
unconfined
unconfined
[ksoftirqd/0]

?
?
?
?

00:00:00
00:00:00
00:00:00
00:00:00

NI ADDR SZ WCHAN
0 - 1724 wait
0 - 1248 -

[khubd]
[md]
[devfreq_wq]
[khungtaskd]

TTY
pts/1
pts/1

TIME CMD
00:00:00 bash
00:00:00 ps

3
COMMAND
[kthreadd]
[ksoftirqd/0]

-p 2 3

PID TTY
2 ?
3 ?

STAT
S
S

TIME COMMAND
0:00 [kthreadd]
0:00

COMMAND SHUTDOWN:
bring the system down
SYNOPSIS
shutdown [OPTION]... TIME [MESSAGE]
DESCRIPTION:shutdown arranges for the system to be brought down in a
safe way. All logged-in users are notified that the system is going down and,
within the last five minutes of TIME, new logins are prevented.
TIME may have different formats, the most common is simply the word
'now' which will bring the system down immediately. Other valid formats
are +m, where m is the number of minutes to wait until shutting down and
hh:mm which specifies the time on the 24hr clock.
COMMAND FINGER:
user information lookup program
SYNTAX:
finger [-lmsp] [user ...] [user@host ...]
DESCRIPTION
The finger displays information about the system users.

UE123029

Page 81

Option
-s (default)
-l

Description:
Finger displays the user's login name, real name, terminal
name and write status
Produces a multi-line format displaying all of the
information described for the -s option as well as the user's
home directory, home phone number, login shell, mail
status, and the contents of the files .plan, .project from
the user's home directory.

Usage:
gurprem@ubuntu:~$ finger
Login
Name
Tty
Idle
Office Phone
gurprem
Gurprem
*:0
gurprem
Gurprem
pts/0
gurprem
Gurprem
pts/11
gurprem@ubuntu:~$ finger -s
Login
Name
Tty
Idle
Office Phone
gurprem
Gurprem
*:0
gurprem
Gurprem
pts/0
gurprem
Gurprem
pts/11
gurprem@ubuntu:~$ finger -l
Login: gurprem
Directory: /home/gurprem
On since Sun Nov 23 05:30 (PST) on
On since Sun Nov 23 07:37 (PST) on
On since Sun Nov 23 05:34 (PST) on
3 minutes 4 seconds idle
No mail.
No Plan.
COMMAND LESS:
option
-n
-N
--pattern=pattern
-s
+x

Login Time

Office

Nov 23 05:30 (:0)


Nov 23 07:37 (:0)
Nov 23 05:34 (:0)
Login Time

Office

Nov 23 05:30 (:0)


Nov 23 07:37 (:0)
Nov 23 05:34 (:0)
Name: Gurprem
Shell: /bin/bash
:0 from :0 (messages off)
pts/0 from :0
pts/11 from :0

description
Suppress numbering
Display numbering
Squeeze the multiple blank lines into one
Skip x lines from beggining

Usage:
1 gurprem@ubuntu:~$ less myfile11 or less n myfile11
this is line 1
this is line 2
this is line 3
this is line 4
UE123029

Page 82

this is line 5
this is line 6

this is line 7
it is line 8
it is line 9
2 gurprem@ubuntu:~$.less N myfile11
1 this is line 1
2 this is line 2
3 this is line 3
4 this is line 4
5 this is line 5
6 this is line 6
7
8
9
10
11 this is line 7
12 it is line 8
13 it is line 9 and \n this is line 10
14
3. gurprem@ubuntu:~$ less s N myfile11
1 this is line 1
2 this is line 2
3 this is line 3
4 this is line 4
5 this is line 5
6 this is line 6
7
11 this is line 7
12 it is line 8
13 it is line 9
14
4 gurprem@ubuntu:~$.less s N pattern=this myfile11
1 this is line 1
2 this is line 2
3 this is line 3
4 this is line 4
5 this is line 5
6 this is line 6
7
UE123029

Page 83

11 this is line 7
12 it is line 8
13 it is line 9
14
5 gurprem@ubuntu:~$
less +4 N myfile11
4 this is line 4
5 this is line 5
6 this is line 6
7
8
9
10
11 this is line 7
12 it is line 8
13 it is line 9
6. gurprem@ubuntu:~$ less -4 -N +/"it" myfile11
12 it is line 8
13 it is line 9 and \n this is line 10
14
7. gurprem@ubuntu:~$ ls|less
area
Desktop
Documents
Downloads
examples.desktop
hello
hello.c
Music
myfile
myfile1
myfile1!
myfile11
myfile11~
Pictures
prog1
prog2
prog3
prog4
prog5
Public
Templates
Videos

UE123029

Page 84

EXPERIMENT 6
AIM: To implement various linux/unix commands :
Script,split,write,wall,dd,sleep,adduser,deluser,banner,useradd,us
erdel,usermod
COMMAND SCRIPT :
Make typescript of terminal session
SYNOPSIS
script [options] [file]
DESCRIPTION
script makes a typescript of everything displayed on your terminal. It is
useful for students who need a hardcopy record of an interactive session
as proof of an assignment, as the typescript file can be printed out later
with lpr.
OPTIONS:
-a

Append the output to file or typescript retaining the prior


contents

-f

Flush output after each write. This is nice for


telecooperation: One person does `mkfifo foo; script -f
foo' and another can supervise real-time what is being
done using `cat foo'.

-q

Be quiet.

-t

Output timeing data to standard error.

UE123029

Page 85

USAGE:
gurprem@ubuntu:~/gurprem script
Script started, file is typescript
gurprem@ubuntu:~/gurprem script -a
Script started, file is typescript
COMMAND SPLIT
split - split a file into pieces
SYNOPSIS
split [OPTION]... [INPUT [PREFIX]]
DESCRIPTION
Output fixed-size pieces of INPUT to PREFIXaa, PREFIXab, ...; default
size is 1000 lines, and default PREFIX is 'x'. With no INPUT, or when INPUT is
-, read standard input.
Options
-a

Generate suffixes of length N (default 2)

-c

Put at most size bytes of lines per output file

-d

Use numeric sufixes instead of alphabets

-l

Put the number lines per output

--verbose

Print a diagnostic just before each output file in opened

USAGE:gurprem@ubuntu:~$ split -a 3 test.txt temp


gurprem@ubuntu:~$ ls
Desktop
dir2
Downloads
Pictures
Public
Templates
Videos
dir1
Documents Music
prog.sh
tempaaa test.txt
gurprem@ubuntu:~$ split -a 3 -l 2 demofile try -d
gurprem@ubuntu:~$ ls
demofile
dir1
Downloads prog.sh tempaaa
demofile~ dir2
Music
Public
Templates
Desktop
Documents Pictures
temp
test.txt

UE123029

Page 86

try000
try001
Videos

gurprem@ubuntu:~$ split -a 3 -l 2 --verbose demofile try -d


creating file try000
creating file try001
gurprem@ubuntu:~$
COMMAND WRITE
write send a message to another user
SYNOPSIS
write user [tty]
DESCRIPTION
The write utility allows you to communicate with other users, by copying
lines from your terminal to theirs. When you run the write command, the
user you are writing to gets a message of the form:
Message from yourname@yourhost on yourtty at hh:mm
...
Any further lines you enter will be copied to the specified user's terminal. If
the other user wants to reply, they must run write as well. When you are
done, type an end-of-file or interrupt charac- ter. The other user will see the
message EOF indicating that the conversation is over.

COMMAND WALL
wall - write a message to all users
SYNOPSIS
wall [-n] [-t timeout] [message | file]
DESCRIPTION
UE123029

Page 87

wall displays a message, or the contents of a file, or otherwise its


standard input, on the terminals of all currently logged in users.
The
command will wrap lines that are longer than 79 characters. Short lines are
whitespace padded to have 79 characters. The command will always put a
carriage return and new line at the end of each line. Only the superuser can
write on the terminals of users who have chosen to deny messages or are
using a program which automatically denies messages.
Options:
-n
Supress the banner
-t

Abandom the write attempt to the terminals after timeout


seconds. This timeout must be a positive integer.

-n helps us remove the banner when broadcasting the message. But this
previledge is only with root or a superuser. So you need to be in the sudoers
to remove the banner.

COMMAND : DD
dd - convert and copy a file
SYNOPSIS
dd [OPERAND]...
dd OPTION
DESCRIPTION
Copy a file, converting and formatting according to the operands.
OPTIONS:
If

This decipts the input file/ source to copy data from

of

The output file

bs

Block size or chuck sizes in which copy takes place

Count

The size in MB upto which the copy has to take place

USAGE:
gurprem@ubuntu:~$ ls
demofile lab1 lab2

UE123029

lab3

shprog

Page 88

testfolder

gurprem@ubuntu:~$ dd if=/dev/zero of=test.txt bs=1M count=2


2+0 records in
2+0 records out
2097152 bytes (2.1 MB) copied, 0.0158048 s, 133 MB/s
gurprem@ubuntu:~$ ls
Desktop
dir1
dir2
Documents
Downloads
prog.sh Public Templates test.txt Videos
gurprem@ubuntu:~$

Music

Pictures

gurprem@ubuntu:~$ ls -l test.txt
-rw-rw-r-- 1 gurprem gurprem 2097152 Nov 24 05:41 test.txt
gurprem@ubuntu:~$
NOTE: dd should be carefully handled. It can have dire consequencies. For
eg. If we missed the count flag in the above example this continuously write
to the file till my disk is full which is undesirable.

COMMAND SLEEP
sleep - delay for a specified amount of time.
SYNOPSIS
sleep NUMBER[SUFFIX]...
sleep OPTION
DESCRIPTION
Pause for NUMBER seconds. SUFFIX may be 's' for second (the default),
'm' for minutes, 'h' for hours or 'd' for days. Unlike most implementations
that require NUMBER be an integer, here NUMBER may be an arbitrary
floating point number. Given two or more arguments, pause for the amount
of time specified by the sum of their values.
COMMAND ADDUSER
adduser, addgroup - add a user or group to the system
SYNOPSIS
adduser [options] [--home DIR] [--shell SHELL] [--no-cre
ate-home] [--uid ID] [--firstuid ID] [--lastuid ID]
[--ingroup GROUP | --gid ID] [--disabled-password]
[--disabled-login] [--gecos GECOS] [--add_extra_groups]
[--encrypt-home] user

UE123029

Page 89

adduser --system [options] [--home DIR] [--shell SHELL]


[--no-create-home] [--uid ID] [--group | --ingroup GROUP
| --gid ID] [--disabled-password] [--disabled-login]
[--gecos GECOS] user
addgroup [options] [--gid ID] group
addgroup --system [options] [--gid ID] group
adduser [options] user group
COMMON OPTIONS
[--quiet] [--debug] [--force-badname] [--help|-h] [--ver
sion] [--conf FILE]
DESCRIPTION
adduser and addgroup add users and groups to the system according
to
command
line
options
and
configuration
information
in
/etc/adduser.conf. They are friendlier front ends to the low level tools like
useradd, groupadd and usermod programs, by default choosing Debian
policy
conformant UID and GID values, creating a home director with skeletal
configuration, running a custom script, and other features. adduser and
addgroup can be run in on of five modes:
1. Add normal user
2. Add system user
3. Add user group
4. Add system group
5. Add existing user to existing group
gurprem@ubuntu:~/gurprem$ sudo adduser testuser
Adding user `testuser' ...
Adding new group `testuser' (1003) ...
Adding new user `testuser' (1001) with group `testuser' ...
Creating home directory `/home/testuser' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for testuser
Enter the new value, or press ENTER for the default
Full Name []: test
Room Number []:
Work Phone []:
Home Phone []:
Other []:
UE123029

Page 90

Is the information correct? [Y/n] y


COMMAND DELUSER
deluser, delgroup - remove a user or group from the sys
tem
SYNTAX
deluser [options] [--force] [--remove-home] [--removeall-files] [--backup] [--backup-to DIR] user
deluser --group [options] group
delgroup [options] [--only-if-empty] group
deluser [options] user group
COMMON OPTIONS
[--quiet] [--system] [--help] [--version] [--conf FILE]
Performs the following:
1. remove a normal user
2. remove a group
3. remove a user from a specific group
USAGE:
gurprem@ubuntu:~/gurprem~ sudo deluser testuser
[sudo] password for gurprem:
Removing user `testuser' ...
Warning: group `testuser' has no more members.
Done.
gurprem@ubuntu:~/gurprem~
Adduser and useradd often create confusion...so what exactly is the
difference ?

useradd is native binary compiled with the system. But, adduser is


a perl script which uses useradd binary in back-end.

adduser is more user friendly and interactive than its back-end


useradd. There's no difference in features provided.

On the same lines is userdel and deluser.


COMMAND USERMOD
UE123029

Page 91

usermod - modify a user account


SYNTAX
usermod [options] LOGIN
DESCRIPTION
The usermod command modifies the system account files to
reflect the changes that are specified on the command
line.
-a
Add the user to the supplementary group(s). Use only with -G
-c

The new value of the user's password file comment field.

-d

The user's new login directory

-m

Move the home directory contents to new location (only valid


when used along with -d)

-p

The encrypted password as returned by crypt(3)

-u

The new numerical value of the user's id

EXPERIMENT 7
AIM: To implement various linux/unix commands :
Ps,nohup,kill,nice,batch,at,crontab
COMMAND PS
report a snapshot of the current processes.
SYNTAX:
ps [options]
DESCRIPTION
ps displays information about a selection of the active processes. If you
want a repetitive update of the selection and the displayed information, use
top instead.
Options
-A

Selects all processes. Identical to -e

-a

Selects all processes except both session leaders and processes


not associated with terminal.

-d

Selects all processes except session leaders

-C

Select by command name.

-G

Select by real group ID (RGID) or name.

UE123029

Page 92

-u

Select by effective user ID(EUID).

-pid

Select process by pid

-ppid

Select parent process by process ID.

--cols

Set screen width.

--lines

Set screen height.

USAGE:
gurprem@ubuntu:~$ ps
PID TTY
TIME
2752 pts/1
00:00:00
2923 pts/1
00:00:00
gurprem@ubuntu:~$ ps -e
PID TTY
TIME
1 ?
00:00:05
2 ?
00:00:00
3 ?
00:00:00
4 ?
00:00:00
5 ?
00:00:00
6 ?
00:00:01
7 ?
00:00:02
8 ?
00:00:00
9 ?
00:00:00
10 ?
00:00:00
11 ?
00:00:00
12 ?
00:00:00
13 ?
00:00:00
14 ?
00:00:00
15 ?
00:00:00
16 ?
00:00:00
17 ?
00:00:00
18 ?
00:00:00
19 ?
00:00:00
20 ?
00:00:00
21 ?
00:00:00
22 ?
00:00:00
25 ?
00:00:00
26 ?
00:00:00
27 ?
00:00:00
28 ?
00:00:00
29 ?
00:00:00
30 ?
00:00:00
31 ?
00:00:00
43 ?
00:00:00
44 ?
00:00:00
45 ?
00:00:00
46 ?
00:00:00

UE123029

CMD
bash
ps
CMD
init
kthreadd
ksoftirqd/0
kworker/0:0
kworker/0:0H
kworker/u16:0
rcu_sched
rcu_bh
migration/0
watchdog/0
khelper
kdevtmpfs
netns
writeback
kintegrityd
bioset
kworker/u17:0
kblockd
ata_sff
khubd
md
devfreq_wq
khungtaskd
kswapd0
ksmd
khugepaged
fsnotify_mark
ecryptfs-kthrea
crypto
kthrotld
kworker/u16:1
scsi_eh_0
scsi_eh_1

Page 93

52 ?
69 ?
.
.
.
.
2464 ?
2467 ?
2471 ?
2476 ?
2508 ?
2515 ?
2556 ?
2561 ?
2569 ?
2583 ?
2588 ?
2594 ?
2604 ?
2638 ?
2660 ?
2671 ?
2686 ?
2688 ?
2702 ?
2736 ?
2751 ?
2752 pts/1
2799 pts/11
2840 ?
2841 ?
2848 ?
2883 ?
2907 pts/11
2916 pts/11
2926 pts/1

00:00:00 kworker/0:2
00:00:00 deferwq

00:00:00 gconfd-2
00:00:00 gvfs-gphoto2-vo
00:00:00 gvfs-afc-volume
00:00:00 gvfs-mtp-volume
00:00:00 gvfsd-burn
00:00:00 gvfsd-trash
00:00:00 gvfsd-metadata
00:00:00 telepathy-indic
00:00:00 mission-control
00:00:00 zeitgeist-datah
00:00:00 zeitgeist-daemo
00:00:00 zeitgeist-fts
00:00:00 cat
00:00:00 update-notifier
00:00:00 deja-dup-monito
00:00:00 unity-scope-hom
00:00:04 unity-scope-loa
00:00:00 unity-files-dae
00:00:00 unity-music-dae
00:00:05 gnome-terminal
00:00:01 gnome-pty-helpe
00:00:00 bash
00:00:00 bash
00:00:00 sh
00:00:00 run-parts
00:00:00 apt
00:00:00 sleep
00:00:00 man
00:00:00 pager
00:00:00 ps

gurprem@ubuntu:~$ ps -ef
UID
PID PPID C STIME TTY
root
1
0 1 02:18 ?
root
2
0 0 02:18 ?
root
3
2 0 02:18 ?
root
4
2 0 02:18 ?
root
5
2 0 02:18 ?
root
6
2 0 02:18 ?
root
7
2 0 02:18 ?
root
8
2 0 02:18 ?
root
9
2 0 02:18 ?
root
10
2 0 02:18 ?
root
11
2 0 02:18 ?
root
12
2 0 02:18 ?

UE123029

TIME
00:00:05
00:00:00
00:00:00
00:00:00
00:00:00
00:00:01
00:00:02
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00

Page 94

CMD
/sbin/init
[kthreadd]
[ksoftirqd/0]
[kworker/0:0]
[kworker/0:0H]
[kworker/u16:0]
[rcu_sched]
[rcu_bh]
[migration/0]
[watchdog/0]
[khelper]
[kdevtmpfs]

root
13
root
14
root
15
root
16
root
17
root
18
root
19
root
20
root
21
root
22
root
25
root
26
root
27
root
28
root
29
root
30
root
31
root
43
root
44
root
45
root
46
root
52
root
69
root
70
root
125
root
126
root
127
root
128
root
137
root
138
root
294
--daemon
root
299
udevd --dae
message+
397
--fork
root
421
root
434
logind
syslog
448
root
451
avahi
452
[ubuntu.lo
avahi
457
helper
root
482
--daemon
root
491
root
626
root
679
root
721
1/polkitd --n

UE123029

2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1

02:18 ?
02:18 ?
02:18 ?
02:18 ?
02:18 ?
02:18 ?
02:18 ?
02:18 ?
02:18 ?
02:18 ?
02:18 ?
02:18 ?
02:18 ?
02:18 ?
02:18 ?
02:18 ?
02:18 ?
02:18 ?
02:18 ?
02:18 ?
02:18 ?
02:18 ?
02:18 ?
02:18 ?
02:18 ?
02:18 ?
02:18 ?
02:18 ?
02:18 ?
02:18 ?
0 02:18 ?

00:00:00 [netns]
00:00:00 [writeback]
00:00:00 [kintegrityd]
00:00:00 [bioset]
00:00:00 [kworker/u17:0]
00:00:00 [kblockd]
00:00:00 [ata_sff]
00:00:00 [khubd]
00:00:00 [md]
00:00:00 [devfreq_wq]
00:00:00 [khungtaskd]
00:00:00 [kswapd0]
00:00:00 [ksmd]
00:00:00 [khugepaged]
00:00:00 [fsnotify_mark]
00:00:00 [ecryptfs-kthrea]
00:00:00 [crypto]
00:00:00 [kthrotld]
00:00:00 [kworker/u16:1]
00:00:00 [scsi_eh_0]
00:00:00 [scsi_eh_1]
00:00:00 [kworker/0:2]
00:00:00 [deferwq]
00:00:00 [charger_manager]
00:00:00 [mpt_poll_0]
00:00:00 [mpt/0]
00:00:00 [kpsmoused]
00:00:00 [scsi_eh_2]
00:00:00 [jbd2/sda1-8]
00:00:00 [ext4-rsv-conver]
00:00:00 upstart-udev-bridge

0 02:18 ?

00:00:00 /lib/systemd/systemd-

0 02:18 ?

00:00:00 dbus-daemon --system

1 0 02:18 ?
1 0 02:18 ?

00:00:00 /usr/sbin/bluetoothd
00:00:00 /lib/systemd/systemd-

1 0 02:18 ?
2 0 02:18 ?
1 0 02:18 ?

00:00:00 rsyslogd
00:00:00 [krfcommd]
00:00:01 avahi-daemon: running

452
1
2
1
1

0 02:18 ?

00:00:00 avahi-daemon: chroot

0 02:18 ?

00:00:00 upstart-file-bridge

0 02:18 ?
0 02:18 ?
0 02:18 ?
1 0 02:18 ?

00:00:00 [ttm_swap]
00:00:00 /usr/sbin/ModemManager
00:00:00 NetworkManager
00:00:00 /usr/lib/policykit-

Page 95

root
778
1 0 02:18 ?
--daemon
root
823
679 0 02:18 ?
-sf /usr/lib/N
root
852
1 0 02:18 tty4
tty4
root
857
1 0 02:18 tty5
tty5
root
863
1 0 02:18 tty2
tty2
root
864
1 0 02:18 tty3
tty3
root
867
1 0 02:18 tty6
tty6
root
910
1 0 02:18 ?
root
915
1 0 02:18 ?
kernoops
927
1 0 02:18 ?
whoopsie
937
1 0 02:18 ?
root
955
1
0 02:18
/etc/acpi/events -s /va
root
960
1 0 02:18 ?
root
994
960 10 02:18 tty7
-seat seat0
root
999
1
0
/usr/lib/accountsservice/account
root
1010
1 0 02:18 ?
nobody
1015
679
0 02:18 ?
--no-resolv -root
1036
1 0 02:18 tty1
tty1
root
1401
2 0 02:18 ?
root
1568
960 0 02:19 ?
child 12 19
root
1596
1
0
/usr/lib/upower/upowerd
rtkit
1635
1 0 02:19 ?
daemon
root
1892
1 0 02:19 ?
colord
1907
1 0 02:19 ?
lp
1922
1892
0
/usr/lib/cups/notifier/dbus dbus
lp
1923
1892
0
/usr/lib/cups/notifier/dbus dbus
root
1958
1 0 02:19 ?
gurprem
2023
1
0 02:19 ?
keyring-daemon -gurprem
2030 1568 0 02:19 ?
gurprem
2122 2030 0 02:19 ?
gurprem
2127
2030
0 02:19 ?
--session --a
gurprem
2135 2030 0 02:19 ?

UE123029

00:00:00 upstart-socket-bridge
00:00:00 /sbin/dhclient -d
00:00:00 /sbin/getty -8 38400
00:00:00 /sbin/getty -8 38400
00:00:00 /sbin/getty -8 38400
00:00:00 /sbin/getty -8 38400
00:00:00 /sbin/getty -8 38400
00:00:00
00:00:00
00:00:00
00:00:00
?

anacron -s
cron
/usr/sbin/kerneloops
whoopsie
00:00:00 acpid -c

00:00:00 lightdm
00:00:47 /usr/bin/X -core :0
02:18

00:00:00

00:00:00 /usr/sbin/cups-browsed
00:00:00 /usr/sbin/dnsmasq
00:00:00 /sbin/getty -8 38400
00:00:00 [kauditd]
00:00:00 lightdm --session02:19

00:00:00

00:00:00 /usr/lib/rtkit/rtkit00:00:00 /usr/sbin/cupsd -f


00:00:00 /usr/lib/colord/colord
02:19 ?
00:00:00
02:19

00:00:00

00:00:00 tpvmlpd2
00:00:00 /usr/bin/gnome00:00:00 init --user
00:00:00 ssh-agent -s
00:00:01 dbus-daemon --fork
00:00:00 upstart-event-bridge

Page 96

gurprem
2138 2030 0 02:19 ?
gnu/hud/wind
gurprem
2152 2030 0 02:19 ?
--daemon --s
gurprem
2154 2030 0 02:19 ?
--daemon --s
gurprem
2156 2030 0 02:19 ?
--daemon --u
gurprem
2158 2030 0 02:19 ?
--daemonize
gurprem
2173
2030
0 02:19 ?
settings-daemon/u
gurprem
2177 2030 0 02:19 ?
gnu/hud/hudgurprem
2180
2030
0 02:19 ?
core/at-spi-bus
gurprem
2181
2030
0 02:19 ?
--session=ubuntu
gurprem
2183 2030 0 02:19 ?
panel-servi
gurprem
2187
2180
0 02:19 ?
--config-file=/
gurprem
2193 2030 0 02:19 ?
gurprem
2195
2030
0 02:19 ?
core/at-spi2-re
gurprem
2200 2030 0 02:19 ?
fuse /run/us
gurprem
2202 2158 0 02:19 ?
dconf
gurprem
2203 2158 0 02:19 ?
gtk3
gurprem
2205 2030 0 02:19 ?
gnu/bamf/bam
gurprem
2211 2030 0 02:19 ?
--kill-da
gurprem
2239 2030 0 02:19 ?
gnu/indicato
gurprem
2241 2030 0 02:19 ?
gnu/indicato
gurprem
2246 2030 0 02:19 ?
gnu/indicato
gurprem
2250 2030 0 02:19 ?
gnu/indicato
gurprem
2251 2030 0 02:19 ?
gnu/indicato
gurprem
2257 2030 0 02:19 ?
gnu/indicato
gurprem
2258 2030 0 02:19 ?
gnu/indicato
gurprem
2262 2030 0 02:19 ?
gnu/indicato

UE123029

00:00:00 /usr/lib/i386-linux00:00:00 upstart-dbus-bridge


00:00:00 upstart-dbus-bridge
00:00:00 upstart-file-bridge
00:00:00 /usr/bin/ibus-daemon
00:00:00 /usr/lib/unity00:00:00 /usr/lib/i386-linux00:00:00 /usr/lib/at-spi200:00:00 gnome-session
00:00:00 /usr/lib/unity/unity00:00:00 /bin/dbus-daemon
00:00:00 /usr/lib/gvfs/gvfsd
00:00:00 /usr/lib/at-spi2-

Page 97

00:00:00 /usr/lib/gvfs/gvfsd00:00:00 /usr/lib/ibus/ibus00:00:00 /usr/lib/ibus/ibus-ui00:00:00 /usr/lib/i386-linux00:00:00 /usr/lib/ibus/ibus-x11


00:00:00 /usr/lib/i386-linux00:00:00 /usr/lib/i386-linux00:00:00 /usr/lib/i386-linux00:00:00 /usr/lib/i386-linux00:00:00 /usr/lib/i386-linux00:00:00 /usr/lib/i386-linux00:00:00 /usr/lib/i386-linux00:00:00 /usr/lib/i386-linux-

gurprem
2271 2030 0 02:19 ?
00:00:00 /usr/lib/i386-linuxgnu/indicato
gurprem
2295
2030
0 02:19 ?
00:00:00
/usr/lib/evolution/evolution-sou
gurprem
2306 2030 0 02:19 ?
00:00:01 /usr/bin/pulseaudio
--start --lo
gurprem
2332 2030 0 02:19 ?
00:00:00 /usr/lib/i386-linuxgnu/notify-o
gurprem
2338 2158 0 02:19 ?
00:00:00 /usr/lib/ibus/ibusengine-simple
gurprem
2339 2181 3 02:19 ?
00:00:14 compiz
/etc/cron.dai
root
2848
2841
0 02:23 ?
00:00:00 /bin/sh
/etc/cron.daily/apt
gurprem
2907 2799 0 02:25 pts/11
00:00:00 man ps
gurprem
2916 2907 0 02:25 pts/11
00:00:00 pager -s
root
2963 2848 0 02:26 ?
00:00:00 apt-get -qq -y update
root
2966
2963
0 02:26 ?
00:00:00
/usr/lib/apt/methods/http
root
2967
2963
0 02:26 ?
00:00:00
/usr/lib/apt/methods/http
root
2968
2963
0 02:26 ?
00:00:00
/usr/lib/apt/methods/http
root
2970
2963
0 02:26 ?
00:00:00
/usr/lib/apt/methods/gpgv
root
2973
2963 38 02:26 ?
00:00:04
/usr/lib/apt/methods/copy
root
2979
2963
6 02:26 ?
00:00:00
/usr/lib/apt/methods/bzip2
gurprem
2981 2752 0 02:26 pts/1
00:00:00 ps -ef
gurprem@ubuntu:~$
gurprem@ubuntu:~$ ps -l
F S
UID
PID PPID C PRI
0 S 1000 2752 2736 0 80
0 R 1000 3279 2752 0 80

NI ADDR SZ WCHAN
0 - 1724 wait
0 - 1248 -

TTY
pts/1
pts/1

TIME CMD
00:00:00 bash
00:00:00 ps

COMMAND NOHUP
run a command immune to hangups, with output to a non-tty
SYNTAX:
nohup COMMAND [ARG]...
nohup OPTION
DESCRIPTION
Run COMMAND, ignoring hangup signals.
If standard input is a terminal, redirect it from /dev/null. If standard
output is a terminal, append output to 'nohup.out' if
possible,
UE123029

Page 98

'$HOME/nohup.out' otherwise. If standard error is a terminal, redirect it to


standard output. To save output to FILE, use 'nohup COMMAND > FILE'.
gurprem@ubuntu:~/gurpremrm nohup.out
gurprem@ubuntu:~/gurprem nohup ls -l *.c
nohup: ignoring input and appending output to nohup.out
gurprem@ubuntu:~/gurprem cat nohup.out
-rw-rw-r-- 1 gurprem gurprem 1519 Nov 21 17:49 arrow.c
-rw-rw-r-- 1 gurprem gurprem 625 Oct 24 23:22 can.c
-rw-rw-r-- 1 gurprem gurprem 137 Nov 7 19:55 fact.c
-rw-rw-r-- 1 gurprem gurprem 71 Nov 12 18:06 hello.c
-rw-rw-r-- 1 gurprem gurprem 866 Oct 25 10:59 pay.c
-rw-rw-r-- 1 gurprem gurprem 477 Nov 1 20:35 test.c
gurprem@singh ~$
COMMAND KILL
send a signal to a process
SYNTAX:
kill [options] <pid> [...]
DESCRIPTION
The default signal for kill is TERM. Use -l or -L to list available signals.
Particularly useful signals include HUP, INT, KILL, STOP, CONT, and 0.
Alternate signals may be specified in three ways: -9, -SIGKILL or-KILL.
Negative PID values may be used to choose whole process groups; see the
PGID column in ps command output. A PID of -1 is special; it indicates all
processes except the kill process itself and init.
OPTIONS
<pid> [...]
Send signal to every <pid> listed.
-<signal>
-s <signal>
--signal <signal>
Specify the signal to be sent. The signal can be specified by
using name or number. The behavior of signals is explained in
signal(7) manual page.
-l, --list [signal]
List signal names. This option has optional argument, which will
convert signal number to signal name, or other way round.
UE123029

Page 99

-L, --table
List signal names in a nice table.
NOTES Your shell (command line interpreter) may have a built-in kill
command. You may need to run the command described here
as
/bin/kill to solve the conflict.
USAGE:
kill -9 -1
Kill all processes you can kill.
kill -l 11
Translate number 11 into a signal name.
kill -L
List the available signal choices in a nice table.
kill 123 543 2341 3453
Send the default signal, SIGTERM, to all those processes.
COMMAND : NICE
run a program with modified scheduling priority
SYNTAX:
nice [OPTION] [COMMAND [ARG]...]
DESCRIPTION :
Run COMMAND with an adjusted niceness, which affects process
scheduling.
With no COMMAND, print the current niceness. Niceness values range
from
-20 (most favorable to the process) to 19 (least favorable to the
process).
Mandatory arguments to long options are mandatory for short options
too.
-n, --adjustment=N
add integer N to the niceness (default 10)
gurprem@ubuntu:~/gurpremnice -n 10 ls
foo bar

UE123029

Page 100

COMMAND :AT and BATCH


At is used to schedule a single command for a single use whereas batch is
used to schedule/execute multiple tasks for single use.
-m

Mail the user when job has been completed even if there was no
output

-M

Never send mail to the user

-f

Reads the job from the file rather than standard input

-t

Run the job at time, given in format [[CC]YY]MMDDhhmm[.ss]

To execute a one-time task when the load average is below 0.8 (for RH
systems) 1.5 (Ubuntu), use the batch command.
After typing the batch command, the at> prompt is displayed. Type the
command to execute, press , and type Ctrl-D. More than one command can
be specified by typing each command followed by the key. After typing all
the commands, press to go to a blank line and type Ctrl-D. Alternatively, a
shell script can be entered at the prompt, pressing after each line in the
script, and typing Ctrl-D on a blank line to exit. If a script is entered, the shell
used is the shell set in the user's SHELL environment, the user's login shell,
or /bin/sh (whichever is found first). As soon as the load average is below
0.8/1.5, the set of commands or script is executed.
If the set of commands or script tries to display information to standard out,
the output is emailed to the user.

COMMAND CRONTAB
-maintain crontab files for individual users (Vixie Cron)
SYNTAX:
crontab [ -u user ] file
crontab [ -u user ] [ -i ] { -e | -l | -r }
DESCRIPTION:
crontab is the program used to install, deinstall or list the tables used to
drive the cron daemon in Vixie Cron. Each user can have their own crontab,
and though these are files in /var/spool/cron/crontabs, they are not intended
to be edited directly.
If the /etc/cron.allow file exists, then you must be listed (one user per line)
therein in order to be allowed to use this command. If the
UE123029

Page 101

/etc/cron.allow file does not exist but the /etc/cron.deny file does exist,
then you must not be listed in the /etc/cron.deny file in order to use this
command.
USAGE:
gurprem@ubuntu:~/gurprem crontab -e
no crontab for gurprem - using an empty one
crontab: installing new crontab
gurprem@ubuntu:~/gurprem date
Mon Nov 24 00:38:12 IST 2014
gurprem@ubuntu:~/gurpremcat ./hello.txt
cat: ./hello.txt: No such file or directory
gurprem@ubuntu:~/gurpremdate
Mon Nov 24 00:39:04 IST 2014
gurprem@ubuntu:~/gurpremcat ./hello.txt
hello linux

EXPERIMENT 8
Aim: Introduction to the vi editor
The vi editor is a command-based editor used by many Linux users. The vi
editor has powerful features to aid programmers, but many beginning users
avoid using vi because the plentiful features overwhelm them. Although
there are quite a lot of graphical based text editor provided by different
distributions of Linux, learning vi is still important because in many
situations, such as for server configuration, the graphical user interface is
not available. Text editing has to be done with a command-based editor like
vi.
vi is a screen-oriented text editor originally created for the Unixoperating
system. The portable subset of the behavior of vi and programs based on it,
and the ex editor language supported within these programs, is described by
(and thus standardized by) the Single Unix Specification andPOSIX.
The original code for vi was written by Bill Joy in 1976, as the visual mode for
aline editor called ex that Joy had written with Chuck Hal Bill Joy's ex 1.1 was
released as part of the first BSD Unix release in March, 1978. It was not until
version 2.0 of ex, released as part of Second Berkeley Software Distribution
in May, 1979 that the editor was installed under the name vi (which took
UE123029

Page 102

users straight into ex's visual mode), and the name by which it is known
today. Some current implementations of vi can trace their source code
ancestry to Bill Joy; others are completely new, largely compatible
reimplementations.

The name vi is derived from the shortest unambiguous abbreviation for the
command visual in ex; the command in question switches the line editor ex
to visual mode. The name vi is pronounced /via/ (as in the discrete English
letters v and i), or, much less commonly, /va/, but never "six" as in the
Roman numeral VI.[]
In addition to various non-free software implementations of vi distributed
with proprietary implementations of Unix, several free and open source
software implementations of vi exist. A 2009 survey of Linux Journalreaders
found that vi was the most widely used text editor among respondents,
beating gedit, the second most widely used editor by nearly a factor of two
(36% to 19%)
Starting the vi Editor
The vi editor lets a user create new files or edit existing files. The command
to start the vi editor is vi, followed by the filename.
Usage: vi <filename>
Example: vi test.txt
When you start vi for the first time, you will see a screen filled with tildes (A
tilde looks like this: ~ ) on the left side of the screen. Any blank lines beyond
the end of the file are shown this way. At the bottom of your screen, the
filename should be shown if you have specified an existing file, and the size
UE123029

Page 103

of the file will be shown as well, like this:


"filename" 21 lines, 385 characters
If the file you specified does not exist, it will tell you that it is a new file, like
this:
"newfile" [New file]

The two modes of vi

The two modes of vi are command mode and insert mode. The command
mode allows the entry of commands to manipulate text. These commands
are usually one or two characters long, and can be entered with few
keystrokes. The insert mode puts anything typed on the keyboard into the
current file. vi starts out in command mode. There are several commands
that put the vi editor into insert mode. The most commonly used commands
to get into insert mode are a and i. Once you are in insert mode, you get out
of it by hitting the escape key. You can hit escape two times in a row and vi
would definitely be in command mode. Hitting escape while you are already
in command mode does not take the editor out of command mode. It may
beep to tell you that you are already in that mode.
Getting out of vi
The vi editor has two modes and in order to get out of vi, you have to be in
command mode. Hit the key labeled "Escape" or "Esc" to get into command
mode.
Some simple vi commands
1. Cutting, Deleting and Undo commands
The command commonly used for cutting is d. This command deletes text
from the file. The command is preceded by an optional count and followed
by a movement specification. If you double the command by typing dd, it
deletes the current line. Here are some combinations of
these:
x : delete character under the cursor.
d^ : deletes from current cursor position to the beginning of the line.
d$ : deletes from current cursor position to the end of the line.
dw : deletes from current cursor position to the end of the word.
3dd : deletes three lines from current cursor position downwards.
u : undo the last change to the file. Typing u again will re-do the change.
2. Copying and pasting commands
y : copy selected characters to the system buffer
p : paste the system buffer to the current cursor position
3. Search commands
The vi editor has two kinds of searches: string and character. For string
search, the / and ? commands are used. When you use these commands in
the command mode, the command just typed will be shown on the bottom
UE123029

Page 104

line. You should then type the string to search for. These two commands
differ only in the direction where the search takes place. The / command
searches forwards (downwards) in the file, while the ? command searches
backwards (upwards) in the file. The n and N commands repeat the previous
search command in the same or opposite direction, respectively.

EXPERIMENT 9
AIM: Introduction to
statements in vi Editor.

command

line

arguments

and

logical

Different vi clones of course have different ways of starting the program


(invocation). Usually, however, command-line versions of vi share a common
basic set of command line options. These following command line options
and flags are typically available. In addition, vi can be started under different
names. Depending on the name used to start vi, it may either behave
slightly differently or load a different vi clone.
You will be able to do much of your editing using the following commands.
Once you are familiar with the list below, read through the rest of the article
to pick up additional useful commands.
<Esc> Return to command mode.
<Ret> orGo to the first non-blank character of the next line.
+
Go to the first non-blank character of the previous line (- is
the minus sign).
a
Append text after the cursor.
A
Append text at the end of the current line.
dd
Delete the current line.
D or d$ Delete from the cursor to the end of the line.
u
Undo the last change.
U
Undo all changes to the current line.
x
Delete the current character (the one at the cursor).
nx
Delete n characters to the right, starting with the current
character.
ZZ
Save changes and exit.
:
Save changes and exit.
wq<Ret>
:w<Ret> Save changes without exiting.
:q!<Ret> Quit without saving changes.
Text Input Commands
The following commands enter insert mode. To return to command mode,
UE123029

Page 105

press the <Esc> key.


a Append text after the cursor.
n Append after the cursor. Whatever you type until you press
a <Esc>gets replicated n times. For example: if you type 3a, then
12<Esc>, the string 121212 is inserted.
A Append text at the end of the current line.
i Insert text before the cursor.
I Insert text at the beginning of the current line.
o Open a new line below the current line.
O Open a new line above the current line.
Moving Through Your File by Positioning the Screen
Note: <Ctrl> represents the Control key. <Ctrl>f means hold the Control
key, then press the f key.
Move forward (downward) by 1 screen.
<Ctrl>f
n<Ctrl
Move forward by n screens.
>f
<Ctrl>
Move backward (upward) by 1 screen.
b
<Ctrl>
Move downward by 1/2 screen.
d
<Ctrl>
Move upward by 1/2 screen.
u
<Ctrl>
Scroll downward one line.
e
<Ctrl>
Scroll upward one line.
y
H
Go to the first line on the screen.
M
Go to the middle line on the screen.
L
Go to the last line on the screen.
Shifts the current line to the top of the
z<Ret>
screen.
Shifts the current line to the bottom of
zthe screen.
Shifts the current line to the center of
z.
the screen.
Cursor Movement by Characters and Lines
h
or
left
Move left one character.
arrow
nh
Move left n characters.
j or down
Move down one line.
arrow
k or up arrow Move up one line.
l
or
right
Move right one character.
arrow
UE123029

Page 106

G
1G
nG
0
^
$
<Ret> or +
-

Go to end of the file.


Go to the top of the file.
Go to line n.
Go to the beginning of the current line (0 is the digit zero).
Go to the first non-blank character in the current line.
Go to the end of the current line.
Go to the first non-blank character of the next line.
Go to the first non-blank character of the previous line (- is
the minus sign).
n|
Go to column n (| is the vertical bar).
Note: If you moved away from a spot by using a search or G command, you
can return to your previous location by typing two single quotes:
''
Cursor Movement by Words
In vi, a "word" is either a group of letters, digits and _; or a group of
punctuation marks (including special characters other than _). For example:
one
1 word: one
one!
2 words: one and !
one!). Next 3 words: one and !). and Next
one!). "Next 4 words: one and !). and " and Next
A "blank delimited word" (called WORD in the explanations below of
commands that use it) includes adjacent punctuation. WORDs are separated
by spaces, tabs or newlines (what you get when you type <Ret>). For
example:
one
1 WORD: one
one!
1 WORD: one!
one!). Next
2 WORDs: one!). and Next
one!). "Next 2 WORDs: one!). and "Next
w Move right one word or group of punctuation marks.
b Move left one word or group of punctuation marks.
Move right to the end of the next word or group of
e
punctuation marks.
Move right one WORD (see above for the definition of
W
WORD).
B Move left to the beginning of the next WORD.
E Move left to the end of the next WORD.
Moving by Sentence or Paragraph
In vi, a sentence ends with a ., !, or ?, followed by TWO BLANK SPACES
(one blank space doesn't cut it!) or a newline (what you get when you type
<Ret>). Paragraphs are separated by one or more blank lines.
) Move to the next sentence.
( Move to the beginning of the current sentence, (or to the beginning of the
previous sentence if you are already at the beginning of a sentence).
UE123029

Page 107

} Move to next paragraph.


{ Move to the end of the previous paragraph.
Shifting Text
>> Shift right one tab stop.
n> Shift n lines right one tab
> stop.
<< Shift left one tab stop.
n< Shift n lines left one tab
< stop.
Deletion Commands
x
Delete the current character (the one at the cursor).
nx
Delete n characters to the right, starting with the current character.
X
Delete the character to the left of the cursor.
nX
Delete n characters to the left of the cursor (starting with the character
to the left of the cursor).
dd
Delete the current line.
ndd
Delete n lines (from the current line through n lines below the current
line.
d<Ret Beware: this deletes 2 lines!!
>
D
orDelete from the cursor to the end of the line.
d$
dw
Delete from the cursor to end of the current word.
ndw Delete n words starting at the cursor and deleting rightward.
db
Delete from the character to the left of the cursor leftward to the start of
the current word.
d/strin Deletes from (and including) the cursor position until, but not including,
g
string
dL
Delete all lines from the current line downward till the bottom of the
screen.
dH
Delete all lines from the current line upward till the top of the screen.
dM
Delete lines from the current line to the middle of the screen (either
direction depending on current location).
dG
Delete from the current line through the end of the file.
Change Commands
Change commands effectively insert data. With the exception of r, all the
change commands listed below will put you in insert mode. To return to
command mode use the <Esc> key.
rx Replace the current character (where the cursor is positioned) with the
character x.
R Replace all the characters starting with the current character until you
press the <Esc> key. Characters after the character at which you press
<Esc> are unchanged.
UE123029

Page 108

Replace all the characters from the cursor to end of line. A $ will appear as
the last character of the line (to show you what you are replacing).
cw Change to end of word. A $ will appear at the end of the word.
ncwChange to end of n'th word. A $ will appear at the end of the n'th word.
cW Change to end of WORD (including punctuation). A $ will appear at the end
of the WORD.
cc Change (replace) the current line. The line disappears and the cursor is
positioned at the beginning of the line.
ncc Change n lines.
s
Substitute character. A $ will appear at the cursor location. You replace the
current character and append additional text until you press <Esc>.
ns Substitute n characters. A $ will replace the nth character.
Undo Commands
u Undo the last change.
Undo all changes to the
U
current line.
Save and Exit Commands
ZZ
Save changes and exit.
:wq<Ret>
Save changes and exit.
:w<Ret>
Save changes without exiting.
:q!<Ret>
Quit without saving changes.
:q<Ret>
Try to quit. Invokes a warning message if there are any changes
in the edit buffer and does not quit.
:w
Save changes to the file newfile without exiting.
newfile<Ret>
:w!
filename<Ret Overwrite the existing file filename.
>

How to print or access value of UDV (User defined variables)

To print or access UDV use following syntax


Syntax:
$variablename
Define variable vech and n as follows:
$ vech=Bus
$ n=10
To print contains of variable 'vech' type
$ echo $vech
Shell Arithmetic
Use to perform arithmetic operations.
UE123029

Page 109

Syntax:
expr

op1

math-operator

Examples:
$
expr
1
$
expr
2
$
expr
10
$
expr
20
$
expr
10
$ echo `expr 6 + 3`
Note:
expr 20 %3 - Remainder read as 20 mod 3
expr 10 \* 3 - Multiplication use \* and not * since its wild card.
The read Statement

op2
+
/
%
\*
and

3
1
2
3
3
remainder

is

2.

Use to get input (data from user) from keyboard and store (data) to variable.
Syntax:
read variable1, variable2,...variableN
if and test command or [ expr ]
test command or [ expr ] is used to see if an expression is true, and if it is true it return zero(0),
otherwise returns nonzero for false.
Syntax:
test expression OR [ expression ]
Comparison operators:
For [ expr ]
statement with
if command

-eq

is equal to

5 == 6

if [ 5 -eq 6 ]

-ne

is not equal to

5 != 6

if [ 5 -ne 6 ]

-lt

is less than

5<6

if [ 5 -lt 6 ]

-le

is less than or
5 <= 6
equal to

if [ 5 -le 6 ]

-gt

is greater than

if [ 5 -gt 6 ]

-ge

is greater than or
5 >= 6
equal to

5>6

if [ 5 -ge 6 ]

if...else...fi

If given condition is true then command1 is executed otherwise command2 is executed.


Syntax:
if condition
then
condition is zero (true - 0)
UE123029

Page 110

execute all commands up to else statement


else
if condition is not true then
execute all commands up to fi

fi
for Loop

Syntax:
for { variable name } in { list }
do
execute one for each item in the list until the list is
not finished (And repeat all statement between do and done)
done
Even you can use following syntax:
Syntax:
for
((
expr1;
expr2;

expr3

))
do

.....
...
repeat all statements between do and
done until expr2 is TRUE
Done
In above syntax BEFORE the first iteration, expr1 is evaluated. This is usually used to initialize
variables for the loop.
All the statements between do and done is executed repeatedly UNTIL the value of expr2 is
TRUE.
AFTER each iteration of the loop, expr3 is evaluated. This is usually use to increment a loop
while loop
Syntax:
while [ condition ]
do
command1
command2
command3
..
....
done
The case Statement
The case statement is good alternative to Multilevel if-then-else-fi statement. It enable you to
match several values against one variable. Its easier to read and write.
Syntax:
case $variable-name in
pattern1) command
...
..
UE123029

Page 111

command;;
pattern2) command
...
..
command;;
patternN) command
...
..
command;;
*)
command
...
..
command;;
esac
The $variable-name is compared against the patterns until a match is found.
The shell then executes all the statements up to the two semicolons that are
next to each other. The default is *) and its executed if no match is found.

EXPERIMENT 10
AIM: To implement following programs in vi-editor
1)To input length, breadth and radius and calculate area,perimeter
and circumference.
Code:
echo "Enter the lenght: "
read len
echo "Enter the breadth: "
read bre
echo "Enter radius of radius: "
read radius
echo "Area rectangle: " `echo $len*$br | bc`
echo "Perimeter rec: " `echo 2*\($len+$ber\) | bc`
echo "Area circle: " `echo 3.14*$radius*$radius | bc`
OUTPUT:

UE123029

Page 112

2)To change temperature from Farenhiet to Celcius


echo "Enter temp in farenhiet: "
read far
cel=`echo 1.8*\($far-32\)|bc`
echo "Temp in celcius is: "
echo $cel
OUTPUT:

3)To change temperature from Celcius to Farenhiet


Code:
echo "Enter temp in celcius: "
read c
far=`expr \(5.0/9.0\)*$c+32|bc`
echo "Temp in farenhiet is: "
echo $far

OUTPUT:

UE123029

Page 113

4)To find total salary if DA is 40% of basic pay and Hr is 20% of basic
pay
Code:
echo "enter salary"
read sal
total=`echo $sal+0.40*$sal+0.20*$sal|bc`
echo $total
OUTPUT:

5). To find given num is odd or even


Code:
echo "enter number"
read num
rem=$(( $num % 2 ))
if [ $rem -eq 0 ]
then
echo " even number "
else
echo " odd number "
fi
OUTPUT:

UE123029

Page 114

EXPERIMENT 11
AIM: To implement following programs in vi-editor
1).Factorial of a number :
Code :
echo "enter number"
read num
result=1
for i in $(seq 1 1 $num)
do
result=`echo $result*$i|bc`
done
echo "factorial of num "$num" is "$result""
OUTPUT:

2)Fibonacci series :
Code:
echo "Enter the number of terms "
read t
a=1
b=1
echo $a
echo $b
for i in $(seq 1 1 $t)
do
UE123029

Page 115

x=$a
a=$b
b=`echo
echo $b

$b+$x | bc`

done
OUTPUT :

3)Convert entered distance from kms to m,cm,mm .


Code:
echo "Enter the distance in km: "
read km
echo "Converted values are: "
echo "meters: " `expr 1000*$km| bc`
echo "centimeters: " `expr $km*100000 | bc`
echo "milimeters: " `expr $km*1000000 | bc`
OUTPUT:

4)Generate Prime numbers between 1 and 100.


Code:
UE123029

Page 116

# Find prime nos

from 1 to 100

for i in $(seq 2 1 100)


do
isprime=1
till=`echo $i - 1 | bc`
for j in $(seq 2 1 $till)
do
mod=`echo $i % $j | bc`
if [ $mod -eq 0 ]
then
isprime=0
break
fi
done
if [ $isprime -eq 1 ]
then
echo $i "number is prime"
fi
done
OUTPUT :

UE123029

Page 117

EXPERIMENT 12
AIM: To implement following programs in vi-editor.
1)Find the entered year is a leap year or not.
Code:
echo "enter year "
read year
year4=`echo $year%4|bc`
year400=`echo $year%400|bc`
if [ $year4 -eq 0 -a $year400 -ne 0 ]
then
echo "$year" "is leap year""
else
echo "$year" "is not leap year""
fi
OUTPUT:
UE123029

Page 118

2)# to generate combinations of 1,2,3


CODE:
for i in $(seq 1 1 3)
do
for j in $(seq 1 1 3)
do
if [ $i -eq $j ]
then
continue
else
for k in $(seq 1 1 3)
do
if [ $i -ne $k -a $j -ne $k ]
then
echo $i $j $k
fi
done
fi
done
done
OUTPUT:

3)Find reverse of a three digit number .


Code:
echo "enter a three digit number : "
read num
sum=0
rem=0
rev=0
UE123029

Page 119

rem=`echo
sum=`echo
rev=`echo
num=`echo
rem=`echo
sum=`echo
rev=`echo
num=`echo
rem=`echo
sum=`echo
rev=`echo
num=`echo
echo "sum
echo "the

$num%10|bc`
$sum+$rem|bc`
$rev*10+$rem|bc`
$num/10|bc`
$num%10|bc`
$sum+$rem|bc`
$rev*10+$rem|bc`
$num/10|bc`
$num%10|bc`
$sum +$rem|bc`
$rev*10+$rem|bc`
$num/10|bc`
of the digits of three digit number is " " $sum" "
reverse of number is " "$rev""

Output:

4)Find max and min from a given list of numbers.


Code :
min=$1
max=$1
for i in $*
do
if test $min -gt $i
then
min=$i
fi
if test $max -lt $i
then
max=$i
fi
done
echo "minimum:"$min
echo "maximum:"$max
Output:

UE123029

Page 120

5)Implement bubble sort :


Code:
echo "enter no of elements"
read n
echo "enter $n numbers"
for i in $(seq 1 1 $n)
do
read x$i
done
for i in $(seq $n -1 1)
do
for j in $(seq 1 1 $i)
do
j1=`echo $j+1|bc`
eval temp1=\$x$j1
eval temp2=\$x$j
if [ $temp2 -gt $temp1 ]
then
eval x$j1=$temp2
eval x$j=$temp1
fi
done
done
echo " after sorting: "
for k in $(seq 1 1 $n)
do
eval echo \$x$k
done
Output:

UE123029

Page 121

6) Implement binary search.


Code:
echo "enter no. of elements"
read m
echo "enter the $m numbers in sorted order"
for i in $(seq 1 1 $m)
do
read x$i
done
echo "enter number to b searched in above list"
read n
low=1
high=$m
mid=`echo $low+$high|bc`
mid=`echo $mid/2|bc`
while test $low -le $high
do
eval temp=\$x$mid
if test $temp -eq $n
then
echo "position is "$mid
break
else if test $x$i -gt $n
then
high=`echo $mid+1|bc`
mid=`echo $low+$high|bc`
mid=`echo $mid/2|bc`
else
low=`echo $mid-1|bc`
UE123029

Page 122

mid=`echo $low+$high|bc`
mid=`echo $mid/2|bc`
fi
fi
done
if test $low -gt $high
then
echo "number is not in the list"
fi
Output:

EXPERIMENT 13
AIM: To implement following programs in vi-editor
1.Find if the entered integer is palindrome or not .
Code:
echo "enter the number"
read n
m=$n
temp=$n
i=0
while test $m -ne 0
do
i=`echo $i+1|bc`
UE123029

Page 123

m=`echo $m/10|bc`
done
rev=0
while test $i -ne 0
do
eval rem=`echo $temp%10|bc`
eval rev=`echo $rev*10+$rem|bc`
eval temp=`echo $temp/10|bc`
eval i=`echo $i-1|bc`
done
if test $rev -eq $n
then
echo " Palindrome "
else
echo " Not Palindrome"
fi
Output:

2.Find if the entered string palindrome or not.


Code:
echo "Enter a string to be checked:"
read str
len=`echo $str | wc -c`
len=`echo $len - 1|bc`
i=1
j=`echo $len / 2|bc`
while [ $i -le $j ]
do
k=`echo $str | cut -c $i`
l=`echo $str | cut -c $len`
if test $k != $l
then
echo "$str is not palindrome"
exit
fi
i=`echo $i + 1|bc`
len=`echo $len - 1|bc`
done
UE123029

Page 124

echo "$str is palindrome"


OUTPUT:

3.Find the overtime rate for 5 employees ,given that if they work
for more than 40 hrs/day then the overtime rate is Rs.12/hr.
Code:
echo "enter time taken by 5 employees :"
for i in $(seq 1 1 5)
do
read x$i
done
echo "overtime rate for employees is :"
for i in $(seq 1 1 5)
do
eval temp=\$x$i
overtime=`echo $temp-40|bc`
overrate=`echo $overtime*12|bc`
echo $overrate
done
OUTPUT:

4 . Use case to make a menu for any five commands.

UE123029

Page 125

Code:
echo " enter option "
echo "press 1 cd .. 2) echo pwd 3) echo whoami
4) echo who 5) echo ls "
read option
case $option in
1) echo `cd ..`;;
2) echo `pwd`;;
3) echo `whoami`;;
4) echo `who`;;
5) echo `ls`;;
*) echo "default option";;
Esac
OUTPUT:

EXPERIMENT 14
AIM:To implement following programs.
Q) To get user to enter string of ten char and if 10 chr not entered print erorr msg
Code:
i=1
while [ $i ]
do
read -p "Enter a String of length 10 : " string
length=`echo $string | wc -m`
if [ $length -eq 11 ]
then
echo "lenght is right"
break
else
echo "lenght is not right enter again"
fi
done
UE123029

Page 126

OUTPUT:

Q) .Remove common files from directory


Code:
#!/bin/bash
# remove common files in one directory
clear
echo -en "\033[0m"
echo -en "Enter the first location \033[5m:\e[0m "
read path1
echo -en "Enter the second location \033[5m:\e[0m "
read path2
cd $path1
for i in *
do
cd $path2
for j in *
do
if [ $i = $j ]
then
rm $j
UE123029

Page 127

fi
done
cd - > /dev/null
done

EXPERIMENT 15
AIM :- To implement the following programs in vi Editor:
1)To rename every file of directory.
1.

CODE:
for i in *
do
d=new$i
mv $i $d
done
OUTPUT:

UE123029

Page 128

2)To generate backup of a directory.


Code:
for i in *
do
cp -rv $i backup1
done

Output:

UE123029

Page 129

EXPERIMENT 16
AIM :- To implement the following programs in vi Editor:
1)To display all files in the current directory to which you have read , write
and execute permissions.
CODE:
for i in *
do
if [ -r $i -a -w $i -a -x $i ]
then
echo $i
fi
done
OUTPUT:

UE123029

Page 130

2)Write a script that accepts any number of files as arguments , check if it is


a file or directory , if directory then report it back and if it is a file then give
the file name and the number of lines in it.
CODE:
for i in $*
do
if [ -f $i ]
then
echo " $i is file and no of lines in this file are:"
n=`wc -l $i|cut -f 1 -d ' '|bc`
echo $n
else if [ -d $i ]
then
echo "$i is directory"
else
UE123029

Page 131

echo "$i is neither file nor directory"


fi
fi
done
OUTPUT:

UE123029

Page 132

Você também pode gostar