Unix Assignment 1&2
Unix Assignment 1&2
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.
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:
File permissions control who can read, write, or execute a file. Permissions are set using the
chmod command.
Syntax:
Permission Types:
Modes:
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).
File ownership determines who owns a file and which group it belongs to. Ownership is changed
using the chown command.
Syntax:
Examples:
3. Additional Options
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.
for file in *; do
if [ -f "$file" ]; then
if [ ! -s "$file" ]; then
rm "$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
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)
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:
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.
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.
colors["green"] = "#00FF00"
numbers[1] = "one"
numbers[2] = "two"
You can access and manipulate array elements using the index:
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.
You can remove a specific element from an array using the delete statement:
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
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.
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:
2. Input Redirection
Syntax:
3. Appending Output
Syntax:
4. Error Redirection
Syntax:
Syntax:
command > file 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
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:
lines[NR] = $0
END {
print lines[i]
}
Explanation
1. Reading Lines:
EX. lines[NR] = $0
You can run the script and provide the file whose contents you want to reverse:
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: