Operating System Practical File

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 37

OPERATING NAME: MITALI SHARMA

CLASS: B. TECH 3rd YEAR


SEC: B
SYSTEM ROLL NO: 1913024
ID: BTBTC19215

PRACTICAL FILE Email: btbtc19215_mitali@banasthali.in


CONTENTS Page No
1. What is UNIX? 2
2. UNIX Vs WINDOWS 3
3. LINUX 4
4. ls Command 5
5. Long listing format 7
6. Show hidden files 8
7. Sorting the output 8
8. Creating and viewing files 8
9. Deleting files 9
10. Creating and viewing directories 10
11. The History command 10
12. The Clear command and some other commands 11
13. UNIX Processes 12
14. Control commands and some other commands 13
15. Grep Command 15
16. Tar Command 16
17. Zip Command 17
18. vi Editor 18
19. Programming Language 22
20. Scripting Language 22
21. Shell Scripting 22
22. Shebang 23
23. Arithmetic Operator 24
24. Relational Operator 24
25. Decision Making 24
26. Loops 24
27. Shell Scripting Questions 25
Q1. Operators in shell scripting using expr command.
Q2. Operators in shell scripting using pipe command.
Q3. Check if a number is positive or negative.
Q4. Check if a number is odd or even.
Q5. Calculate the roots of the quadratic equations
Q6. Find out the largest of 4 distinct integers.
Q7. Find out the largest of 4 integers.
Q8. Print numbers from 1 to n.
Q9. Print a ‘*’ matrix.
Q10. Print a triangular star matrix.
Q11.Sum of the digits of a number.
Q12. Print the reverse of a number.
Q13. Check if the number is a palindrome or not.

1
WHAT IS UNIX?
 The Unix operating system is the set of programs that act as an interface
between the computer and user, which makes user experience or expertise
better and efficient.
 Unix was originally developed in 1969 by a group of AT&T employees
ken Thompson, Dennis Ritchie, Douglas Mcllroy, and Joe Ossanna.
 Several people can use Unix operating system at the same time; hence
Unix is multiuser system.
 A user can also run multiple programs at the same time; hence it is also
multitasking system.

 Kernel-It interacts with the hardware, coordinates with all the details of
computer’s details and performs most of the tasks like memory
management, task scheduling and file management.
 Shell-The shell is the utility that processes the user’s request.
-When a command is typed in the terminal, the shell interprets the
command and calls the program that user wants.
 Command and Utilities-There are various commands and utilities which
user can make use of in day to day activities. All the commands come
with options to give user more efficient output.
 Files and Directories-Data of Unix is organized into files.
-All the files are then organized into directories.
-These directories are then further organized into tree like structure called
the filesystem.

2
UNIX Vs WINDOWS
 LICENCING:
-Unix was developed as an open source OS using C and assembly
languages. Since being open source Unix and all its Linux distributions
are available in general purpose licence.
-Windows operating system is propriety software owned by Microsoft,
meaning its source code is not available to the public.
 USER INTERFACE:
-Unix operating systems are comparatively tougher to grasp and give
away significant barriers for newcomers.
-Windows OS on the other hand is designed with the offset of keeping the
UI simple and user friendly as attainable, so that non-IT people can easily
use computers for their work.
 PROCESSING:
-Unix OS supports multiprocessing, meaning many processes are
executed simultaneously.
-Windows Support multithreading, meaning many threads are created
from a single process for increasing computing power.
 FILE SYSTEM:
-Unix OS uses STD.ERR and STD.IO file systems or the UFS (Unix file
system) and treats all physical drives as one logical drive.
-Windows on the opposite hand, uses file allocation Table (FAT32) and
new Technology file system (NTFS) systems for managing files and
needs the owner of the executables before executing, files store in folders
on different data drive like C, D, E.
 USERS ACCOUNTS:
-In the Linux system there are three types of user account
>Regular >Root >Service
-In Windows, there are three types of user account
>Administrator >Child >Guest
 SECURITY:
-UNIX is a lot securable by design. The primary protection on a Unix
system is that executing a “.exe” file is way harder. Here executables
cannot be processed in Unix are treated equally.
-Windows is less secure in the sense that it is simple and straightforward
to execute files with minimal permission. These can further lead to the
installation of Malwares unintentionally on the system.
 DATA BACKUP AND RECOVERY:
In the earlier version of Unix, salvaging data from a system when
3
the device housing the aforementioned data is inaccessible, wasn’t
attainable. However, in some distribution of Linux like Ubuntu, a new
feature is introduced called Live distribution can be used to recover data
from an inaccessible device.
-Recovery and Backup of data in the Windows system is a lot less
complicated and can be done using its inbuilt GUI for the same. It has an
integration with OneDrive for data backup and recovery too.
 CASE SENSITIVITY:
-Unix is completely case sensitive, meaning the files with names
“GFG.txt” and “gfg.txt” can be considered two different files.
-Windows, on the other hand have nonobligatory case sensitivity.
 HARDWARE:
-Hardware support is limited in Unix system. Some hardware might not
have driver built for them.
-Windows has all the drivers for all the available hardware.
 RELIABILITY:
-Unix is known for being very stable to run.
-Although the stability of windows has significantly improved in recent
years, most Unix system are still far superior in this respect.

LINUX
 Richard Stallman was looking to create a truly free and open source
alternative to the proprietary Unix system.
 Although, there was a kernel project underway, it turned out to be
difficult going, and without a kernel, the free and open source operating
system dream could not be realized.
 It was Linus Torvalds’s work-producing a working and viable kernel that
he called Linux- that brought the complete operating system to life.
 Linux distributions came to life with the components of GNU, the Linux
kernel, MIT’s X-Windows GUI, and other BSD components that could be
used under the open source BSD license.
 Because of the free open and open source standing of all the Linux
components, anyone could create a Linux distribution with a bit of effort,
and soon the total no of distros reached into the hundreds.
 Many developers utilize Linux either via cloud providers or by using
popular free distributions like Fedora, Canonicals’ Ubuntu, Debian, Arch
Linux, Gentoo, and many other variants.

4
ls COMMAND
 The ls command lists files and directories within the file system, and
shows detailed information about them.
 The syntax for the ls command is as follows-
ls [options] [files] Copy
 When used with no options and arguments, ls displays a list of the names
of all files in current working directory.
 To list files in specific directory, pass the directory path as an argument to
the ls command. For ex, to list the contents of the /etc directory, user
would type: ls/etc
 If the user logged in doesn’t have the logged in permissions to the
directory, user will get the message that it can’t open the directory.
 Some of the ls command options are-

1. ls -a This command enlists the whole list of current


directories including all the hidden files.
2. ls -l It will show the list in long list format.
3. ls -lh This command will show file in human readable form.
4. ls -n It is used to print group ID and owner ID instead of
name
5. ls -r It is used to print list I reverse order.
6. ls -R It is used to print the content of sub-directories.
7. ls ~ It will display the contents of home directory
8. ls -li This command prints the index number if file is in the
first column.
9. ls -lt It will sort the list by displaying recently modified filed
at top.
10.ls -p It is used to identify the directory easily by marking the
directories with a slash (/) line sign.
11.ls -d */ Command to list only directories
12.ls * Lists the contents of the directory with its
subdirectories
13.ls -s List files or directories with their sizes

5
6
LONG LISTING FORMAT
 The default output of the ls command shows only the names of the files
and directories, which is not very informative.
 The -l option tells ls to print files in a long listing format.
 When the long listing format is used following file, information is
displayed-
>file type >file permission >Number of hard links to file
>File owner >File group >File size >Date and Time >File name
 Some of the file types are-
1. - Regular File
2. b Block Special File
3. c Character special file
4. d Directory
5. l Symbolic Link
6. n Network File
7. p FIFO
8. s Socket
7
 The permission character can take the following value-
r-Permission to read the file
w-Permission to write
x-Permission to execute the file
s-Setgid bit
t-Sticky bit

SHOW HIDDEN FILES


 In Linux, a hidden file is any file that begins with a (.)
 To display all the hidden files ls -a option is used.
 Ex- $ ls -a
 Output- .bash_logout, .bash_history, .profile etc

SORTING THE OUTPUT


 The --sort option allows user to sort the output by extension, size, time
and version.
 --sort =extension (or -X)-sort alphabetically by extension
 --sort=size (or -S)-sort by file size
 --sort=time (or -t)-sort by modification time
 --sort=version (or -v)-Natural sort of version numbers
 To get the result in reverse sort order -r option is used.

CREATING AND VIEWING FILES

8
 The ‘cat’ server command is used to display text files. It can also be used
for copying, combining and creating new text files.
 To create a new file command:
cat >filename is used, for ex- cat >1d.txt
 Then add content and press ‘ctrl+d’ to return to command prompt.
 To view a file, use the command-
cat filename
 The syntax to combine two files is-
cat file1 file2 > newfilename

DELETING FILES
 The ‘rm’ command removes files from the system without confirmation.
 Syntax- rm filename
 rm *.* -delete all files
 rm *.c-deletes c extension files
 rm one.*-deletes file name one of any extension
 For renaming file
-mv filename newfilename -ex $ mv demo.txt demo2.txt

9
CREATING AND DELETING DIRECTORIES
 Directories can be created on Linux operating system using the following
command-
mkdir directoryname
 If user wants to create a directory in different location other than ‘Home
directory’, you could use the following command.
For ex- mkdir /tmp/demoDir
 To remove directory command used is-
rmdir directoryname

THE HISTORY COMMAND


 History command shows all the basic commands in Linux that user has
used in past for the current terminal session.
 This can help in referring to old command user has entered and can reuse
them in operations again.

10
THE CLEAR COMMAND
 This command clears all the clutter on the terminal and gives user a clean
window to work on just like it was when terminal was launched in the
beginning.

OTHER COMMANDS
Some other commands are-
1.cal -Displays the calendar
Syntax: cal[[month]year]
Ex-Display the calendar for October 2022
$ cal 10 2022
2.date -Displays the system date and time
Syntax: date[+format]
Ex-Display the date in dd/mm/yy format
$ date + %d/%m/%y
3.who -Displays the list of users currently logged in
Syntax: who[option]…. [file][arg1]
Ex-List all currently logged in users
$ who
4.whoami -Displays the user id of the currently logged in user
Syntax: whoami[option]
Ex- List currently logged in user
$ whoami
5.touch -Create a new file or update it’s timestamp
Syntax: touch[option]…[file]
11
Ex- Create empty files called ‘mf1’ and ‘mf2’
$ touch mf1 mf2
6.cat -Concatenate files and print to stdout
Syntax: cat[option]…[file]
Ex-Create a file with entered content
$ cat > mf1
-Hi , Welcome to ubuntu
-^D

UNIX PROCESSES
 A process is a context in which a program executes.
 Every time when a command or a program is run, a new process is
created.
 The process is active for as long as the program is in active state.
 For ex, if we are executing ‘cat’ command then the process named ‘cat’ is
generated.
 Each time a process is created the kernel assigns a unique identification
number called PID (Process identification number) which lies between 0
and 32,767.
 Other properties of processes include PPID (Parent PID), TTY (the
controlling terminal from where they are launched), UID (the user ID that

12
owns this process) and GID (the group that is associated with the
process).
1.FOREGROUND PROCESS
- A process that is launched from a terminal and disallows any commands till
it completes.
-In such a process, the stdin and stdout are attached to the terminal by
default.
2.BACKGROUND PROCESS
-It is a process that was launched from a terminal but runs in the background,
thus allowing further commands while it runs.
-In such a process, the stdin and stdout should typically be redirected so they
don’t interfere with other foreground processes.
3.DAEMON PROCESS
-It is the process that is not associated with a terminal session.
-Such processes are usually launched for system services.

CONTROL COMMANDS
 These commands are a two-key combination where a letter is
simultaneously with the ‘ctrl’ key.
-Control-C: This command terminates the currently running foreground
process.
-Control-D: This command terminates the presently running login or
terminal session.
-Control-Z: This command suspends the currently running foreground
process to the background.

1.ls -Lists directory content


Syntax: ls[option][file]
Ex-List all the files (including hidden) directory contents,
in reverse format, sorted by time
$ ls -art
2.which -Locate a command
Syntax: which [-a] filename
Ex-List all the paths from where ‘ls’ can run
$ which -a ls

13
3.man -Interface for working with online reference manual
Syntax: man [-s section] item
Ex-Show manual page for ‘cat’ command
$ man cat
4.su -Changes user-id or become super user
Syntax: su [option] [username]
Ex- Change user-id to ‘user1’ (if it exit)
$ su user1
5.sudo -Execute a command as some other user or super user
Syntax: sudo [options] [command]
Ex-Get a file listing of an unlisted directory
$ sudo ls/usr/local/protected
6.find -Used to search for files and directories as mentioned in
the ‘expression’
Syntax: find [starting-point] [expression]
Ex-In ‘/usr’ folder, find character device files, of name
‘backup’
$ find /usr-type c-name backup
7.du -Estimate disk usage in blocks
Syntax: du [options] [file]
Ex-Show number of blocks occupied by files in the
current directory
$ du
8.df -Show number of free blocks for mounted file system
Syntax: df [options] [file]
Ex-Show number of free blocks in open file system
$ df -l

14
where manual

15
GREP COMMAND IN UNIX
 Grep stands for globally search for regular expression and print out. grep
[options] [pattern] [file] the pattern is specified as a regular expression.
 The grep filter searches a file for selected pattern of characters, and
display all the lines that contain that string. The pattern that’s searched
within the file is referred to as regular expression.
 The regular expression is a string of characters that is used to specify a
pattern matching rule.
 Special characters are used to define the matching rules and position.
Ex- grep -i “Hello” mits.txt

16
TAR COMMAND IN UNIX
 The archive format preserves the directory structure, and the file system
attributes such as permissions and dates.
 The UNIX ‘tar’ command stands for tape archive, its purpose is to create
archive and extract archive files. Tar command in UNIX is among the
vital commands that provide practicality in UNIX; We use UNIX tar
command to make compressed or uncompressed archive files and
additionally maintain and modify them.
 Tar Syntax-
tar [functions] [options] [paths]Tar options
 The tar command supports the following functions:
tar -c: create a new archive
tar -A: Append a tar file to another archive
tar -r: Append a file to an archive
tar -u: Update files in an archive if the one in the filesystem is newer
tar -d: Find the difference between an archive and the filesystem
tar -t: List the contents of an archive
tar -x: Extract the contents of an archive
 Some of the supported options include:
-j: Read or write archives using the bzip2 compression algorithm
-J: Read or write archives using the xz compression algorithm
-z: Read or write archives using the gzip compression algorithm
17
-a: Read or write archives using the compression algorithm determined by
the archive file name
-v: Perform the operations verbosely
-f: Specify the file name for the archive

ZIP
 Zip is the most widely used archive file format that uses lossless file data
compression.
 A zip file is a data container containing one or more compressed files or
directories. Zipped files take up less disk space and can be transferred
from one to another machine more quickly than uncompressed files.
Zipped files can be easily extracted in Windows, macOS, and Linux
using the utilities available for all the operating systems.
 ZIP COMMAND-
Zip command is a line utility that helps you create zip archives.
The zip command takes the subsequent syntax form:
zip OPTIONS ARCHIVE_NAME FILES Copy
To create a Zip archive in a specific directory, the user needs to have
write permissions there on directory.
 HOW TO ZIP FILES AND DIRECTORIES

18
-To zip one or more files, specify the files user wish to add to archive
separated by space as shown below:
-zip archivename.zip filename1 filename2 filename3
-adding: filename1 (deflated 63%)
-adding: filename2 (stored 0%)
-adding: filename3 (deflated 38%)

vi EDITOR
 We apprehend the information that data of Unix is organised into files
and editing files could be a requirement or necessity, vi editor enables the
user to edit lines in context with other lines in the file.
 An improved and upgraded version of vi known as VIM which stands for
vi Improved.
 vi is generally thought as the de facto standard in Unix editors because
>It is usually available on all the flavors of Unix system.
>Its implementations are quite across the broad.
>It needs only a few resources.
>It is an easy to use editor.
 Vi editor serves the purpose of editing the existing file or creating a new
file from scratch, can also be used to just read the text file.
 Starting the vi editor-

19
$ vi filename: Creates a new file if it already does not exist, otherwise
opens an existing file.
$ vi -R filename: Opens an existing file in the read only mode.
$ view filename: Opens an existing file in the read-only mode.
Ex –$ vi demo1.txt
In the above example the file demo1.txt does not exist already in the
system and hence this command creates a new file.
Note-When this file will open up there will be a tilde (~) on each line
following the cursor, if it is not present it signifies that there is space, tab,
newline or some other non-viewable character present.
 MODES-
There are basically two modes we come across:
COMMAND MODE:
-When vi starts up, it’s in command mode. This mode permits user to
perform administrative task whatever we type in this mode is interpreted
as a command.
-This mode is where vi interprets any characters we type as commands
and thus does not display them in window.
-This mode permits user to manoeuvre through a file, and to delete, copy
or paste a piece of text.
-To enter into the command mode from any other mode, it requires
pressing the [esc] key.
INSERT MODE:
-This mode enables user to insert text into the file.
-Everything that’s inserted in this mode is interpreted as input and finally
it is put in the file.
-The vi editor always starts in command mode.
-To come into the insert mode, user ought to sort i to enter the text.
- To induce out of the insert mode, press the [esc] key.
LAST LINE MODE (ESCAPE MODE):
-Line mode is invoked by typing a colon [:], while vi is in command
mode.
-The cursor will jump to the last line of the screen and vi can await for a
command.
-This mode permits user to perform tasks such as saving files, and
executing commands.

20
GETTING OUT OF vi

COMMANDS ACTIONS
1. :wq Save and Quit
2. :w Save
3. :q Quit
4. :w fname Save as fname
5. ZZ Save and Quit
6. :q! Quit discarding changes
made
7. :w! Save and write to non-
writable files.

21
EDITING FILES
To edit files, user need to be in the insert mode –
1. i Text is inserted before the current cursor location.
2. I Text is inserted at the beginning of the current line.
3. a Text is inserted after the current cursor location.
4. A Text is inserted at the end of the current line.
5. o New line for text entry is created below the cursor loc.
6. O New line for text entry is created above the cursor loc.
7. r Characters under the cursor are replaced and returns to
the command mode.
8. R Overwrites multiple character beginning with the
character under the cursor (press esc key to stop
overwriting).
9. s Replaces the current character with the character user
types. Afterward the user is left in the insert mode.
10. S Deletes the line cursor is on and replaces it with new text
and vi remains in the insert mode.

DELETING CHARACTERS
1. x The character under the cursor location is deleted
2. X The character before the cursor location is deleted.
3. dw Deletes the current cursor location to the next word.
4. d^ Deletes from the current cursor position to the beginning of
the line
5. d$ Deletes from the current cursor position to the end of the line
6. D Deletes from the cursor position to the end of the current line
7. dd Deletes from the line where cursor in on

COPY AND PASTE COMMANDS


Copy and paste commands-
1. yy Copies the current line
2. yw Copies the current word from the character the lowercase
cursor is on, until the end of the word
3. p Puts the copied text after the cursor
4. P Puts the yanked text before the cursor

22
PROGRAMMING LANGUAGE
 Programming Language is a way by which users can easily communicate
with the computers through a set of instructions known as code/program.
 Programming languages are generally compiled languages, the source
code is compiled altogether to convert into machine code.
 These languages enable the developers to create desktop applications,
mobile applications, web applications, implements machine learning
algorithms and many more tasks.
 Some popular programming languages are- C, C++, Pascal, COBOL,
java etc.

SCRIPTING LANGUAGE
 Scripting language is a type of programming language which does not
require explicit compilation step because it is interpreted and it is
designed for a runtime system to automate the execution of steps.
 These are also known as very high programming language because of the
working at high level of abstraction.
 These are interpreted at runtime; interpretation of code works line by line.
 It means to convert source code to machine code it uses interpreter.
 A scripting language can assist in different environments such as
application software, webpages, operating system shells, computer games
etc.
 The scripting language refers to dynamic high level, general purpose
interpreted languages such as Python, Perl etc.

SHELL SCRIPTING
 Shell scripting is an open-source computer program designed to be run by
Unix/Linux shell.
 Shell scripting is a program to write a series of commands for the shell to
execute and produce the output.
 What is shell?
-Shell is an interface between a user and an operating system,
it is a command line interpreter that translates the command
entered by user into language that is understood by kernel.
-Shell provides users with an interface and accepts commands into the
system and executes those commands and give the program’s output in
a shell script.

23
 An Operating System is made of many components, but it’s two prime
components are-Kernel and Shell.
 Kernel-A kernel is present at the nucleus of a computer, because of
kernel only communication between the hardware and software are
possible. It performs most of the task such as memory management, task
scheduling and file management.
 Shell-A Shell is the utility that processes our requests, shell in the
LINUX operating system takes input from the user in form of commands
and then processes it, and produces output. Shell is also accessed by the
terminal which runs it.
 The output is displayed on the terminal.
 Moving onto the two major types of shells in Unix-
-Bourne Shell: if the user is using a Bourne-type shell, the $ character
is the default prompt. It has following subcategories- Bourne shell (sh), korn
shell (ksh), Bourne again shell (bash) and POSIX shell (sh).
-C Shell: if the user is using a C-type shell, the % character is the
default prompt. Its subcategories are- C shell (csh) and TENEX/TOPS C
shell (tcsh).

SHEBANG
 The shebang is the special character sequence in a script file that specifies
which program should be called to run the script.
 The shebang is always on the first line of the file, and is composed of the
characters #! followed by the path to the interpreter program.
 The shebang interpreter directive takes the following form:
#!interpreter [arguments] Copy
 The directive must be the first line in the script.
 If the shebang is not specified and the user running the Bash Script is
using another shell the script will be parsed by whatever the default
interpreter is used by that shell.

ARITHMETIC OPERATORS
+ (ADDITION) `expr $a + $b`
- (SUBTRACTION) `expr $a - $b`
* (MULTIPLICATION) `expr $a \* $b`
/ (DIVISION) `expr $a / $b`
% (MODULUS) `expr $a % $b`

24
RELATIONAL OPERATOR
CONDITION IN C IN SHELL
1. Equal to == -eq
2. Not Equal to != -ne
3. Greater than > -gt
4. Greater than or >= -ge
equal to
5. Less than < -lt
6. Less than or equal <= -le
to

DECISION MAKING
 In Unix, while writing shell script a situation may occur where user needs
to choose one path or the other, in such cases conditional statements can
be used to make correct decisions.
 The if else statement are very useful in decision making-
>if…fi statement
>if…else…fi statement
>if…elif…else…fi statement
 Relational operators play an important role in decision making.

LOOPS
 Loops are very important to execute commands repeatedly.
 Loops available in shell scripting are-
>The while loop
>The for loop
>The until loop
>The select loop
 The while loop executes the chunk of code until the condition remains
true.
 Nesting of while loop is possible in Unix, which means that the
programmer can use while loop inside the body of another while loop.
 Syntax-
while command1
do
Statement
done

25
SHELL SCRIPTING PROGRAMS
Q1. Operators in shell scripting using expr command.

Q2. Operators in shell scripting using pipe command.

26
Q3. Check if a number is positive or negative.

Q4. Check if a number is odd or even.

27
Q5. Calculate the roots of the quadratic equations.

28
Q6. Find out the largest of 4 distinct integers.

29
Q7. Find out the largest of 4 integers.

30
31
32
Q8. Print numbers from 1 to n.

Q9. Print a ‘*’ matrix.

33
Q10. Print a triangular star matrix.

Q11.Sum of the digits of a number.

34
Q12. Print the reverse of a number.

Q13. Check if the number is a palindrome or not.

35
BIBLIOGRAPHY
 https://www.tutorialspoint.com/unix/index.htm
 https://www.geeksforgeeks.org/grep-command-in-
unixlinux/
 https://www.geeksforgeeks.org/tar-command-linux-
examples/

36

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy