0% found this document useful (0 votes)
2 views

Unix Assignment 1&2

The document outlines key features of the Unix operating system, including its multiuser and multitasking capabilities, hierarchical file system, and robust security model. It also explains commands for changing file permissions and ownership, and provides a bash script to remove empty files from the current directory. Additionally, it covers concepts of arrays in awk, redirection techniques, and an awk script to display file contents in reverse order.

Uploaded by

siddharthch1612
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Unix Assignment 1&2

The document outlines key features of the Unix operating system, including its multiuser and multitasking capabilities, hierarchical file system, and robust security model. It also explains commands for changing file permissions and ownership, and provides a bash script to remove empty files from the current directory. Additionally, it covers concepts of arrays in awk, redirection techniques, and an awk script to display file contents in reverse order.

Uploaded by

siddharthch1612
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

UNIX ASSIGNMENT(1)

1)List the characteristics/features of unix system.

Unix is a powerful, multiuser, multitasking operating system with a variety of features that have
made it highly influential in the development of modern operating systems. Here are some of its
key characteristics and features:

1. Multiuser Capability: Unix allows multiple users to use the system simultaneously, each
with their own environment and permissions.
2. Multitasking: Unix supports concurrent execution of multiple processes or tasks,
allowing users to run multiple programs at the same time.
3. Portability: Unix is designed to be portable across different hardware platforms. Its code
is written in the C programming language, which enhances its portability.
4. Hierarchical File System: Unix features a tree-like directory structure with a single root
directory (/), which contains all other directories and files. This hierarchy makes file
management and navigation intuitive.
5. Security and Permissions: Unix employs a robust permission and ownership model.
Files and directories have associated permissions (read, write, execute) that control
access for the owner, group, and others.
6. Process Management: Unix provides tools to manage processes, including the ability to
start, stop, and prioritize processes. It also supports background and foreground process
execution.
7. Shell: Unix uses command-line interfaces (shells) such as sh, bash, ksh, and zsh, which
allow users to interact with the system through commands and scripts.
8. Utilities and Tools: Unix includes a rich set of utilities and tools for various tasks, such
as text processing (e.g., grep, awk, sed), file manipulation (e.g., cp, mv, rm), and system
monitoring (e.g., ps, top).
9. File and I/O Redirection: Unix supports input and output redirection, allowing users to
redirect the output of a command to a file or another command and to read input from
files.
10. Pipes and Filters: Unix provides mechanisms for chaining commands together using
pipes (|), enabling powerful combinations of commands to process data in stages.
11. Networking: Unix has built-in networking support, including tools for network
communication (e.g., ssh, telnet, ftp) and configuration.
12. Inter-process Communication: Unix supports various forms of inter-process
communication (IPC), such as pipes, message queues, and semaphores.
13. Modularity: Unix is designed to be modular, with small, single-purpose programs that
can be combined to perform complex tasks.
14. Stability and Reliability: Unix systems are known for their stability and reliability,
making them suitable for both server and desktop environments.
15. Scripting: Unix supports scripting languages (e.g., shell scripts, Perl, Python) that can
automate tasks and create custom workflows.
16. Development Environment: Unix provides a robust environment for software
development, with a range of development tools, compilers, and debuggers.

2)Explain commands for changing file permissions and ownership.

In Unix and Unix-like systems, file permissions and ownership are critical for managing access
control. You use commands to modify these attributes. Here’s a breakdown of how to change file
permissions and ownership:

1. Changing File Permissions

File permissions control who can read, write, or execute a file. Permissions are set using the
chmod command.

Syntax:

chmod [options] mode file

Permission Types:

 r (read): Allows reading the file or listing directory contents.


 w (write): Allows modifying the file or adding/removing files in a directory.
 x (execute): Allows running the file as a program or script. For directories, it allows
entering the directory.

Modes:

 Symbolic Mode: Uses letters to specify permissions.

chmod u+x file

chmod go-r file

chmod u=rwx,g=rx,o=r file


Numeric (Octal) Mode: Uses three-digit numbers to specify permissions.

 Each digit represents permissions for the user, group, and others, respectively.
 4 stands for read (r), 2 stands for write (w), 1 stands for execute (x).

chmod 755 file

chmod 644 file

chmod 777 file

2. Changing File Ownership

File ownership determines who owns a file and which group it belongs to. Ownership is changed
using the chown command.

Syntax:

chown [options] user:group file

 user: The new owner of the file.


 group: The new group for the file. If you omit the group, only the owner is changed.
 file: The file or directory whose ownership you want to change.

Examples:

 Change Owner Only:

EX. chown username file

Change Group Only:

EX. chown :groupname file

Change Both Owner and Group:

EX. chown username:groupname file

3. Additional Options

1.-R (Recursive): Applies changes to directories and their contents.

chmod -R 755 directory

chown -R username:groupname directory


2.-v (Verbose): Shows detailed information about the changes made.

chmod -v 644 file

chown -v username file

3)Write a script that removes only empty files from current directory.

To write a script that removes only empty files from the current directory, you can use a
combination of Unix commands within a shell script. Below is a simple bash script that
accomplishes this task.

Bash Script to Remove Empty Files

for file in *; do

if [ -f "$file" ]; then

if [ ! -s "$file" ]; then

rm "$file"

echo "Removed empty file: $file"

fi

fi

done

Explanation

1. #!/bin/bash: This is the shebang line that tells the system to use the Bash shell to
interpret the script.
2. for file in *; do: This loop iterates over each item in the current directory. The *
wildcard matches all files and directories.
3. if [ -f "$file" ]; then: This condition checks if the current item is a regular file
(not a directory).
4. if [ ! -s "$file" ]; then: This checks if the file is empty. The -s flag returns true
if the file is not empty, so ! -s means the file is empty.
5. rm "$file": This command removes the empty file.
6. echo "Removed empty file: $file": This outputs a message to indicate which file
was removed.
Running the Script

1. Save the script to a file, for example, remove_empty_files.sh.


2. Make the script executable with the following command:

chmod +x remove_empty_files.sh

3. Run the script from the directory where you want to remove empty files:

./remove_empty_files.sh

Unix assignment(2)

1) Explain concept of array in awk utility

In awk, arrays are a fundamental concept used to store and manipulate collections of data. Arrays
in awk are associative arrays, meaning they use string indices rather than just numerical indices,
which makes them very flexible and powerful for various tasks. Here’s a detailed explanation of
arrays in awk:

Key Concepts of Arrays in awk

1. Array Declaration and Initialization

You don’t need to explicitly declare an array before using it in awk. Arrays are created
automatically when you first assign a value to an index.

EX. myArray["key"] = "value"

2)Index Types
awk arrays are associative, meaning you can use both strings and numbers as indices. The index
type determines how the array elements are accessed and manipulated.

EX. colors["red"] = "#FF0000"

colors["green"] = "#00FF00"

numbers[1] = "one"

numbers[2] = "two"

3)Accessing Array Elements

You can access and manipulate array elements using the index:

EX. print colors["red"]

print numbers[1]

4)Array Traversal

To process or print all elements in an array, you can use a for loop. The loop iterates over all
indices of the array.

EX. (key in colors) {

print key ": " colors[key]

5)Deleting Array Elements

You can remove a specific element from an array using the delete statement:

EX. delete colors["red"]

6)Array Size and Checking

awk does not have a built-in function to directly get the size of an array, but you can use the for
loop to count elements. The length function, when used with an array, provides the length of the
array.

EX. count = 0

for (key in colors) {


count++

print "Number of elements in colors: " count


7)Arrays with Functions

Arrays can be passed to functions and manipulated within those functions.

EX. function printArray(arr) {

for (key in arr) {

print key ": " arr[key]

BEGIN {

colors["blue"] = "#0000FF"

printArray(colors)

8)Multidimensional Arrays

awk supports arrays of arrays, which can be used to create multidimensional arrays. Although
awk does not have explicit multidimensional array syntax, you can simulate it using nested
associative arrays.

EX. multiArray["row1", "col1"] = "value1"

multiArray["row1", "col2"] = "value2"

multiArray["row2", "col1"] = "value3"

for (key in multiArray) {

print key ": " multiArray[key]

2)Explain redirection using example.

Redirection in Unix-like operating systems is a powerful feature that allows you to control where
the output of commands goes and where the input comes from. By using redirection, you can
send output to files, read input from files, and more. Here’s a detailed explanation with
examples:
Types of Redirection

1. Output Redirection
2. Input Redirection
3. Appending Output
4. Error Redirection

1. Output Redirection

Syntax:

command > file

Example: ls > filelist.txt

2. Input Redirection

Syntax:

command < file

Example: sort < unsorted.txt

3. Appending Output

Syntax:

command >> file

Example: echo "New entry" >> log.txt

4. Error Redirection

Syntax:

command 2> file

Example: ls /nonexistent-directory 2> error.log

To append errors: ls /nonexistent-directory 2>> error.log

5. Redirecting Both Output and Errors

Syntax:
command > file 2>&1

Example: ls /some-directory > output.log 2>&1

6. Using Pipes

Pipes (|) allow you to send the output of one command as input to another command, effectively
chaining commands together.

Syntax:

command1 | command2

Example: ps aux | grep firefox

3)Write an awk script to display file contents in reverse, (i.e. last line should be displayed
first, first line should be displayed last.)

To display the contents of a file in reverse order using awk, you can utilize its associative array
capabilities to store lines as you read them and then print them in reverse order. Here’s a step-by-
step awk script that accomplishes this:

awk Script to Display File Contents in Reverse Order

# Define an associative array to store lines

lines[NR] = $0

# After reading all lines, print them in reverse order

END {

for (i = NR; i >= 1; i--) {

print lines[i]
}

Explanation

1. Reading Lines:

EX. lines[NR] = $0

2.Printing Lines in Reverse Order:

EX. END { for (i = NR; i >= 1; i--) { print lines[i] } }

1 Save the Script:

Save the script to a file, for example, reverse_lines.awk.

2 Make the Script Executable:

EX. chmod +x reverse_lines.awk

3 Run the Script:

You can run the script and provide the file whose contents you want to reverse:

EX . awk -f reverse_lines.awk input_file.txt

Alternative Approach Using a Pipeline

If you want a one-liner solution without a separate script file, you can use a combination of tac
(concatenate and print files in reverse) and awk:

EX. tac input_file.txt | awk '{ print }'

You might also like