UNIX FILE
INTRODUCTION:-
UNIX is a powerful Operating System initially developed by Ken Thompson, Dennis Ritchie at
AT&T Bell laboratories in 1970. It is prevalent among scientific, engineering, and academic
institutions due to its most appreciative features like multitasking, flexibility, and many more. In
UNIX, the file system is a hierarchical structure of files and directories where users can store and
retrieve information using the files.
➢UNIX is an operating system that is a software that acts as an interface between the user and
computer h/w.
➢ An operating system acts as a resource manager.
➢ Here resources mean hardware resources like the processor, the main memory, the hard disk,
i/o devices and other peripherals.
➢ In addition to being a multi-user operating system UNIX gives its users, the feeling of
working an independent computer system. UNIX also provides communication facility with
other users who are connected to the system either directly or indirectly via certain sort of
networking.
FEATURES OF UNIX OPERATING SYSTEM:
• Multitasking
• Multi-user
• Portability
• File Security and Protection
• Command Structure
• Communication
• Open Source
• Accounting
• UNIX Tools and Utilities
Animesh Yadav 1 BCA 5 TH SEM
STRUCTURE OF UNIX OS LAYERS:-
Layer-1: Hardware -
This layer of UNIX consists of all hardware-related information in the UNIX environment.
Layer-2: Kernel -
The core of the operating system that's liable for maintaining the full functionality is named the
kernel. The kernel of UNIX runs on the particular machine hardware and interacts with the
hardware effectively.
Layer-3: The Shell -
The Shell is an interpreter that interprets the command submitted by the user at the terminal, and
calls the program you simply want.
Layer-4: Application Programs Layer -
It is the outermost layer that executes the given external applications. UNIX distributions
typically come with several useful applications programs as standard. For Example: emacs
editor, StarOffice, xv image viewer, g++ compiler etc.
Animesh Yadav 2 BCA 5 TH SEM
COMMANDS IN UNIX:
Unix has large number of commands.
➢ There are 2 types of Unix commands.
1. External commands
2. Internal commands
1. External commands:
A command with an independent existence in the form of a separate file is called an external
command.
Ex: cat and ls . these are independently existed in a directory called the / bin directory . When
external commands and given the shell reaches these command files with the help of ‘PATH’
variable.
2. Internal commands:
A command that does not have an independent existence is called an internal command.
Ex. Echo,mkdir,cd,etc; The routines for internal commands will be a part of another program (or)
routine. Ex. Echo command is internal command will be a part of another program(or) routine is
the part of shell’s routine “sh”.
Animesh Yadav 3 BCA 5 TH SEM
Question:1
Execute the following list of basic commands in UNIX: (i) pwd (ii) mkdir (iii) cd.
Answer:-
1. pwd - Print Working Directory
Purpose: Displays the current working directory (the directory you are currently
in).
Command: pwd
2. mkdir - Make Directory
Purpose: Creates a new directory (folder) at the specified location.
Command: mkdir new_directory
3. cd - Change Directory
Purpose: Changes the current working directory to the specified directory.
Command: cd target_directory
Animesh Yadav 4 BCA 5 TH SEM
Question:2
Execute the following list of basic commands in UNIX: (i) who (ii) echo (iii) cat.
Answer:-
1. who - Display Who Is Logged In
Purpose: Shows the users currently logged into the system, along with related
information.
Command: who
Usage: Displays login name, terminal, login time, and the remote host (if
applicable).
2. echo - Display a Line of Text
Purpose: Prints text or variables to the terminal.
Command: echo "Your message here"
3. cat - Concatenate and Display File Contents
Purpose: Reads and displays the content of a file. Can also be used to concatenate
multiple files.
Command: cat filename
Making new file and displaying file content.
Animesh Yadav 5 BCA 5 TH SEM
Question:3
Execute the following list of basic commands in UNIX: (i) rm (ii) mv (iii) wc
(iv) cp
Answer:-
1. rm - Remove Files or Directories
Purpose: Deletes files or directories.
Command: rm filename
2. mv - Move or Rename Files
Purpose: Moves a file to a new location or renames a file.
Rename a file: mv oldname.txt newname.txt
3. wc - Word, Line, and Byte Count
Purpose: Displays the number of lines, words, and bytes in a file.
Command: wc filename
4. cp - Copy Files or Directories
Purpose: Copies files or directories to a new location.
Animesh Yadav 6 BCA 5 TH SEM
Command: cp source destination
Animesh Yadav 7 BCA 5 TH SEM
Question:4
Execute the basic file attributes with all possible options: (i) ls (ii) chmod.
Answer:-
1. ls - List Files and Directories
Purpose: Displays the contents of a directory and the attributes of files and
directories.
Common Options for ls:
Option Description
ls Basic listing of files and directories in
the current directory.
ls -a Lists all files, including hidden files
(those starting with .).
ls -l Displays detailed file information
(permissions, owner, size, etc.).
ls -R Recursively lists contents of
subdirectories.
ls -t Sorts files by modification time,
newest first.
ls -r Reverses the order of listing (e.g.,
with -t, oldest files first).
ls -i Displays the inode number of each
file.
ls -s Shows file sizes in blocks.
Animesh Yadav 8 BCA 5 TH SEM
2. chmod - Change File Permissions
Purpose: Modifies the permissions of a file or directory.
File Permissions Overview:
Permissions are represented as rwx (Read, Write, Execute) for three groups:
Owner (u): User who owns the file.
Group (g): Users in the file's group.
Others (o): Everyone else.
Symbol Permission Numeric Value
r Read 4
w Write 2
x Execute 1
- No permission 0
Animesh Yadav 9 BCA 5 TH SEM
Question:5
Execute basic commands using vi editor:
a. input mode commands
b. saving text and quitting
Answer:-
The vi editor is a powerful text editor available in UNIX/Linux systems. Here’s
how to execute basic commands in vi, specifically focusing on input mode
commands and saving text/quitting.
Input Mode Commands
Allows you to insert and edit text.
To enter input mode, press one of these keys while in command mode:
Command Description
i Insert at the current cursor position.
I Insert at the beginning of the line.
a Append after the current cursor.
A Append at the end of the line.
o Open a new line below the current line.
O Open a new line above the current line.
Command: vi file_name.txt
Animesh Yadav 10 BCA 5 TH SEM
Saving Text and Quitting
To save changes or quit the vi editor, you need to return to Command Mode by
pressing Esc.
Commands for Saving and Quitting:
Command Description
:w Save (write) changes to the file.
:w filename Save changes to a new file (rename/save-as).
:q Quit (only if no changes were made).
:wq or :x Save changes and quit the editor.
:q! Quit without saving changes.
Save and quit in one step:
Press Esc, then type: :wq
Animesh Yadav 11 BCA 5 TH SEM
Question:6
Execute basic commands using vi editor
a. navigation
b. editing text
c. searching pattern
Answer:-
Navigation in vi
When you open a file in vi, you're in Command Mode by default. Use the
following commands to navigate efficiently:
Moving the Cursor
Command Action
h Move left.
l Move right.
j Move down.
k Move up.
0 Move to the beginning of the current line.
^ Move to the first non-whitespace character.
$ Move to the end of the current line.
Ctrl-d Scroll half a screen down.
Ctrl-u Scroll half a screen up.
Animesh Yadav 12 BCA 5 TH SEM
Editing Text
To edit text, switch to Input Mode from Command Mode by pressing one of the
following keys:
Inserting or Appending Text
Command Action
i Insert before the current cursor position.
I Insert at the beginning of the current line.
a Append after the current cursor position.
A Append at the end of the current line.
o Open a new line below the current line.
O Open a new line above the current line.
Deleting Text
Command Action
x Delete the character under the cursor.
dd Delete the current line.
d$ Delete from the cursor to the end of the line.
d^ Delete from the cursor to the start of the line.
dw Delete the current word.
Copying, Cutting, and Pasting
Command Action
yy or Y Copy (yank) the current line.
yw Copy the current word.
p Paste after the cursor.
P Paste before the cursor.
Undo and Redo
Command Action
u Undo the last change.
Ctrl-r Redo the last undone change.
Animesh Yadav 13 BCA 5 TH SEM
Searching Patterns
Basic Search
To search for a pattern, press / in Command Mode and type the search term, then
press Enter: /pattern
Navigating Search Results
Command Action
n Jump to the next occurrence of the search term.
N Jump to the previous occurrence.
Search and Replace
To replace text globally or within a range:
Command Action
:%s/old/new/g Replace all occurrences of old with new.
:n,m s/old/new/g Replace old with new between lines n and m.
:%s/old/new/gc Replace all occurrences with confirmation.
Animesh Yadav 14 BCA 5 TH SEM
Question:7
Execute the following filters using regular expressions with all possible options:
(i) grep
(ii) sed
Answer:-
grep (Global Regular Expression Print)
Purpose: grep is used to search for patterns in files or standard input using regular
expressions.
Syntax: grep [options] "pattern" filename
Options for grep:
Option Description
-i Case-insensitive search.
-v Invert match: show lines that do not match the pattern.
-c Display the count of matching lines.
-n Display line numbers with matching lines.
-l Display only the names of files with matching lines.
-r Search recursively in directories.
-e Specify multiple patterns to match.
--color=auto Highlight matching patterns in the output.
Animesh Yadav 15 BCA 5 TH SEM
sed (Stream Editor)
Purpose: sed is a stream editor used for finding, replacing, and transforming text in
files or standard input.
Syntax: sed [options] 'command' filename
Common Commands:
Command Action
s/pattern/replacement/ Substitute pattern with replacement.
/pattern/d Delete lines matching the pattern.
p Print lines matching the pattern.
Options for sed:
Option Description
-i Edit the file in place.
-n Suppress automatic printing of lines.
-e Allow multiple editing commands in one invocation.
-r or -E Enable extended regular expressions.
Animesh Yadav 16 BCA 5 TH SEM
Question:8
Write a shell script to display current date and calendar.
Answer:-
Steps to Use the Script
Create the Script: Open a terminal and create the script file:
Nano date.sh
Save the File: Save and exit the editor (in nano, press CTRL + X, then press Y to
confirm, and Enter to save).
Make the Script Executable: Run the following command to make the script
executable:
chmod +x date.sh
Run the Script: Execute the script by running:
./date.sh
Animesh Yadav 17 BCA 5 TH SEM
Animesh Yadav 18 BCA 5 TH SEM