Smit Donga C4 Finallll - OS
Smit Donga C4 Finallll - OS
1
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
Practical – 1
Study of Basic commands of Linux/UNIX:
Linux:
Linux is an open-source, Unix-like operating system kernel created by Linus Torvalds in 1991.
It forms the backbone of numerous Linux distributions, such as Ubuntu, Fedora, and Debian.
Known for its robustness, flexibility, and security, Linux is widely used in various fields,
including servers, desktops, embedded systems, and cloud computing.
With support for multitasking and multi-user functionality, Linux operates on a wide range
of hardware, from small IoT devices to powerful supercomputers. Its open-source nature
allows developers and users to modify and tailor the system to their specific needs, making
it a reliable and versatile choice for both personal and enterprise use.
About terminal:
Terminal:
1. A terminal is a command-line interface that allows users to interact with the
operating system using text-based commands.
2. It provides a way to execute programs, manage files, and automate tasks on Unix-
like systems.
BonTerminal:
1. BonTerminal is a simple and lightweight terminal emulator for Linux-based systems.
2. It focuses on providing a minimalistic, fast, and user-friendly experience for terminal-
based tasks.
Bash:
1. Bash (Bourne Again Shell) is a popular Unix shell and command language used for
interacting with the system.
2. It supports scripting, job control, and provides powerful features for automating
tasks and managing the system.
Directory commands:
Directory commands are commands that deals with all the directories.
⮚ ls command:
ls command is used to list out all the files present in the directory
Usage:
ls –[OPTIONS] [FILE]
Options:
-a, --all do not ignore entries starting with .
2
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
Examples:
3
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
mkdir command:
mkdir command is used create a new directory
Usage:
mkdir –[OPTIONS] [DIRECTORY NAME]
Options:
-p no error if existing, make parent directories as needed
4
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
Examples:
⮚ rmdir command:
rmdir command is used to remove directory.
Usage:
rmdir –[OPTIONS] [DIRECTORY]
Options:
-p remove DIRECTORY and its ancestors; e.g., 'rmdir -p a/b/c' is
similar to 'rmdir a/b/c a/b a'
Examples:
5
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
⮚ cd command
cd command is used to change directory
Usage:
cd [DIRECTORY PATH]
Options:
(None commonly used with this command; it simply takes a directory path as an
argument.)
Examples:
⮚ pwd command
pwd command is used to know present working directory
Usage:
pwd -[OPTIONS]
6
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
Options:
(None commonly used with this command; it simply takes a directory path as an
argument.)
Examples:
Filesystem commands:
Filesystem commands are used to manage, manipulate, and interact with files and
directories, including operations like creating, deleting, and modifying them.
⮚ cat command
cat command is used to see content inside the file.
Usage:
cat -[OPTIONS] [FILE NAME]
Options:
(None commonly used with this command; it simply takes a directory path as an
argument.)
Examples:
⮚ rm command
rm command is used to remove files or directories.
7
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
Usage:
rm –[OPTIONS] [FILE NAME]
Options:
-r remove directories and their contents recursively
-f ignore non-existent files and arguments, never prompt
-i prompt before every removal
Examples:
⮚ touch command
touch command is used to create file.
Usage:
touch –[OPTIONS] [FILENAME]
Options:
-a change only the access time
-c do not create any files
Examples:
8
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
⮚ find command
find command is used to search for files in a directory hierarchy
Usage:
find –[OPTIONS] [FILENAME] –[ACTOIN]
Options:
-print display matching files or directories
-exec execute a command on matching files
-name search for files or directories by name
-user search for files owned by a user
-type search by file type (f: files, d: directories)
-size +n or -n search files by size (+n: larger, -n: smaller, n: exact)
-"a*.txt" and a*.txt search files starting with a and ending with .txt
-mtime +n or n or -n search files by modification time (+n: older,
-n :recent, n: exact days)
-mmin +n or n or -n search files by modification time in minutes
-atime +n or n or -n search files by access time
Examples:
9
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
⮚ cp command:
cp command is used to copy files and directories.
Usage:
cp –[OPTIONS] [FILENAME] [PATH TO COPY]
Options:
-i overwrite files without prompting
-r copy directories recursively
-u copy only when the source file is newer than the destination
file or when the destination file is missing
-v verbose mode, show files being copied
-f force overwrite without prompting
-a preserve file attributes while copying
Examples:
10
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
⮚ mv command:
mv command is used to move or rename files and directories.
Usage:
mv –[OPTIONS] [FILENAME] [NEWNAME]
Options:
(None commonly used with this command; it simply takes a directory path as an
argument.)
Examples:
⮚ wc command:
wc command is used to count lines, words, and bytes in a file.
11
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
Usage:
wc -[OPTIONS] [FILENAME]
Options:
-l count lines in a file
-w count words in a file
-c count characters in a file
Examples:
⮚ file command:
file command is used to determine the file type.
Usage:
File –[OPTIONS] [FILENAME]
Options:
* match all files
12
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
Examples:
⮚ cmp command:
cmp command is used to compare two files byte by byte.
Usage:
cmp –[OPTIONS] [FILE1] [FILE2]
Options:
(None commonly used with this command; it simply takes a directory path as an
argument.)
Examples:
⮚ comm command:
comm command is used to compare two sorted files line by line.
Usage:
Comm –[OPTIONS] [FILE1] [FILE2]
Options:
13
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
Examples:
14
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
Practical 2
Study of Advance commands and filters of Linux/UNIX.
File Permissions Overview:
File permissions in Linux are represented in a format like -rwxrw-r—. This format helps in
understanding how a file or directory is accessible by different users.
● -: Indicates a regular file (If it's d, it means a directory).
● rwx: Permissions for the user (owner of the file).
○ r (read): Permission to read the file.
○ w (write): Permission to modify the file.
○ x (execute): Permission to run the file as a program.
● rwx: Permissions for the group (users in the same group as the file).
○ r (read): Permission to read the file.
○ w (write): Permission to modify the file.
○ x (execute): Permission to run the file as a program.
● r--: Permissions for others (all other users on the system).
○ r (read): Permission to read the file.
○ - (write/execute): No write or execute permission.
Ø chmod command:
chmod command is used to change the permissions of a file or directory.
Usage:
chmod [user-symbolset<set/deny symbol>] accesssymbol filename
Examples:
This adds the execute permission (`x`) for the user (`u`) on the file
Octal Notations:
· 0: No permission.
15
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
· 4: Read permission.
· 2: Write permission.
· 1: Execute permission.
· Combine values for multiple permissions:
o chmod 333 file.txt gives read (r), write (w), and execute (x) permission to
user, group, and others.
Ø touch command:
touch command is used to create an empty file or update the access and
modification timestamps of an existing file.
Usage:
touch MMDDHHmm filename
Example:
Ø dd command:
dd command is used for low-level copying and conversion of files.
16
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
Usage:
dd if=source_file of=destination_file conv=lcase/ucase/ascii/ebcdic
Example:
Ø tail command:
tail command is used to display the last few lines of a file.
Usage:
tail +n filename
Example:
17
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
o Terminal View:
o Terminal View:
18
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
o Terminal View:
19
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
o Terminal View:
o
ubuntu@ubuntu:~/lab$ wc notes.txt
3 12 85 notes.txt
20
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
21
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
Explanation
● Collects input for five subjects instead of three for a more comprehensive
marksheet.
● Uses bc for precise percentage calculation with two decimal places.
22
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
Explanation
● Takes a number and a custom range as input.
23
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
Terminal Output
24
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
Detailed Explanation
● Shebang (#!/bin/bash): Specifies that this is a Bash script, ensuring it
runs in the Bash shell.
● Header: Prints a decorative header to make the script user-friendly and
indicate its purpose.
● Input Prompt: Asks the user to enter a number and stores it in the
variable n using read.
● Validation:
25
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
Shell Script:
26
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
Terminal Output:
Detailed Explanation
● Shebang (#!/bin/bash): Ensures the script runs in Bash.
o b: date shows the current date and time (e.g., Fri Apr 4 14:30:00
UTC 2025).
o c: who lists logged-in users with their terminal, login time, and IP (if
remote).
27
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
28
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
Terminal Output
Detailed Explanation
● Shebang (#!/bin/bash): Specifies the Bash shell for execution.
29
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
Shell Script:
Terminal Output
30
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
Detailed Explanation
● Shebang (#!/bin/bash): Runs the script in Bash.
31
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
Shell Script
Terminal Output
Detailed Explanation
● Shebang (#!/bin/bash): Specifies Bash as the interpreter.
32
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
● Sorting: Converts the array to a newline-separated list with tr, then uses
sort -nr (n for numeric, r for reverse) to sort in descending order.
● Output: Shows the original numbers and the sorted result with
formatting.
33
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
Aim
To develop a shell script that lists all executable files, directories, and zero-sized
files in the current directory, categorizing and displaying them separately.
Shell Script
Ouptut
Detailed Explanation
● Shebang (#!/bin/bash): Runs in Bash.
● Header: Shows the script’s purpose and current directory path with pwd.
34
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
35
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
To create a shell script that reads a string from the user and determines whether
it is a palindrome (reads the same forward and backward, e.g., "radar") by
reversing it and comparing.
Shell Script
Output:
Detailed Explanation
● Shebang (#!/bin/bash): Specifies Bash execution.
36
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
Shell Script
37
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
Terminal Output
Detailed Explanation
38
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
● Validation:
o Adjusts max days for April, June, September, November (30 days).
Awk Script
Awk Script
#!/usr/bin/awk -f
# This awk script capitalizes each word in the input text.
BEGIN {
print "========================================="
39
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
END {
print "========================================="
}
Terminal Output
Detailed Explanation
● Shebang: Runs with awk -f to interpret the script.
40
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
Aim
To write a C program that demonstrates the use of fork (create a child process),
exec (replace process image), wait (synchronize parent and child), and implicitly
join processes.
C Program
#include <stdio.h>
#include <unistd.h>
41
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
#include <sys/wait.h>
#include <stdlib.h>
int main() {
pid_t pid;
printf("=========================================\n");
printf(" Process Demonstration\n");
printf("=========================================\n");
printf("Parent process starting (PID: %d)\n", getpid());
if (pid < 0) {
// Fork failed
printf("Error: Fork failed!\n");
exit(1);
} else if (pid == 0) {
// Child process
printf("Child process created (PID: %d, Parent PID: %d)\n", getpid(),
getppid());
printf("Child executing 'ls -l' using exec...\n");
execlp("ls", "ls", "-l", NULL); // Replace child with ls -l
printf("Error: exec failed!\n"); // This won’t run if exec succeeds
exit(1);
} else {
// Parent process
printf("Parent (PID: %d) waiting for child (PID: %d) to finish...\n",
getpid(), pid);
wait(NULL); // Wait for child to complete
printf("Child process completed. Parent resuming.\n");
printf("----------------------------------------\n");
}
printf("=========================================\n");
return 0;
}
Terminal Output
42
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
Detailed Explanation
● Header: Displays program purpose.
● Child: Uses execlp to replace its image with ls -l; if it fails, prints an error.
● Parent: Uses wait to pause until the child finishes, simulating a join.
43
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
Terminal Output
44
Operating system (202040402) CP Vansh Lunagariya(12302040601061)
Detailed Explanation
45