Unix File
Unix File
Unix File
Indore (M.P.)
Department of Computer Science Engineering
Lab Manual
.Session: 2023-2024
10. Write a shell script to find the smallest of three numbers that
are read from the keyboard.
11. Write a shell script for calculator to perform basic arithmetic
operations.
12. Write shell scripts to understand different types of loops with
break and continue statements.
13. Write a shell script that takes input from file and remove
identical lines (or duplicate lines from the file).
Lab : 01
To study the Unix architecture, features & different types of shells.
Unix is an operating system that was first developed in the late 1960s at Bell Labs. It was designed to be
a portable, multitasking, and multi-user operating system. The architecture of Unix is based on several
key principles, including simplicity, modularity, and openness.
The architecture of Unix can be divided into several layers, each of which performs specific functions.
These layers are:
1. Hardware Layer: This layer of UNIX consists of all hardware-related information in the UNIX
environment.
2. Kernel Layer: The kernel is the central component of the Unix operating system. It manages the
system resources, including the CPU, memory, and input/output devices. The kernel is responsible for
providing a secure and stable environment for applications to run in.
3. Shell Layer: The shell is the interface between the user and the operating system. It allows users to
enter commands and interact with the system. There are several different shells available for Unix,
including the Bourne shell (sh), the C shell (csh), and the Korn shell (ksh).
4. Utility Layer: The utility layer consists of a set of standard utilities that are provided with the Unix
operating system. These utilities include commands for managing files and directories, manipulating
text, and controlling processes.
5. Application Layer: The application layer consists of the user-level programs and applications that run
on top of the Unix operating system. These applications can be developed using a variety of
programming languages, including C, C++, Perl, and Python.
Overall, the architecture of Unix is designed to be modular and flexible, allowing users to customize and
configure the system to meet their specific needs. The open nature of Unix also makes it easy to
integrate with other systems and technologies, making it a popular choice for server and embedded
applications.
1. Multiuser and Multitasking: Unix is designed to be a multiuser and multitasking operating system. It
allows multiple users to access the system simultaneously and provides each user with their own login
and workspace. It also allows multiple processes to run at the same time, making it possible to perform
several tasks simultaneously.
2. Command Line Interface: Unix is primarily a command-line operating system, which means that users
interact with the system by typing commands into a shell. This makes it easy to automate tasks and
perform complex operations.
3. Portability: Unix is designed to be highly portable, meaning it can be run on a wide range of hardware
platforms. This makes it a popular choice for embedded systems, scientific research, and other
specialized applications.
4. Security: Unix is known for its security features, including file permissions, access control lists, and
process isolation. It also includes a number of built-in security tools, such as firewalls and intrusion
detection systems.
5. Modularity: Unix is designed to be highly modular, which means that different components of the
operating system can be customized and replaced without affecting the overall system. This makes it
easy to configure and customize the system to meet specific needs.
6. Networking: Unix includes a wide range of networking features, including support for TCP/IP, remote
login, file sharing, and email. This makes it a popular choice for networked environments.
In Unix, a shell is a command-line interface that allows users to interact with the operating system.
There are several different types of shells available in Unix, each with its own set of features and
capabilities. The most commonly used shells are:
1. Bourne shell (sh): The Bourne shell was the original Unix shell, developed by Stephen Bourne in the
late
1970s. It is a simple and lightweight shell that is still widely used today.
2. C shell (csh): The C shell was developed by Bill Joy in the late 1970s. It is designed to be more user-
friendly than the Bourne shell and includes features such as command history, aliases, and command
line editing.
3. Korn shell (ksh): The Korn shell was developed by David Korn in the 1980s. It is designed to be more
powerful than the Bourne shell and includes features such as command line editing, command history,
and job control.
4. Bash (Bourne-Again SHell): Bash is a popular shell that is based on the Bourne shell but includes
many additional features, such as command line editing, command history, and programmable
completion.
5. Zsh (Z shell): Zsh is a shell that includes many features of the Bourne shell, the C shell, and the Korn
shell, as well as many additional features, such as spelling correction, globbing, and file name
completion.
Each shell has its own syntax and set of commands, so users may prefer one shell over another based on
their personal preferences and requirements. However, all of the shells in Unix provide a powerful
command-line interface that allows users to interact with the operating system in a flexible and efficient
manner.
Lab : 02
To study the Unix general purpose utility commands.
1. cd:
The "cd" command stands for "change directory" and is used to navigate to a different directory/folder
within the file system.
The syntax of the command is simply "cd" followed by the path of the directory you want to move to.
Here are a few examples:
2. whoami:
The "whoami" command is a simple Unix utility that prints the username of the current user who is
logged in to the terminal or shell.
When you type "whoami" into a terminal or shell and press Enter, the command will output the name of
the user who is currently logged in. This can be useful for verifying your current user identity, especially
when you are working in a multi-user environment or when you have switched to a different user
account.
3. pwd:
The "pwd" command stands for "print working directory". When you type "pwd" into a terminal or shell
and press Enter, the command will display the absolute path of the current working directory.
For example, if you are currently in the home directory of your user account, typing "pwd" will display
something like "/home/yourusername". If you change to a different directory using the "cd" command,
and then type "pwd" again, it will display the absolute path of that directory.
4. mkdir:
The "mkdir" command in Unix stands for "make directory", and it is used to create a new directory or
folder in
By default, the new directory will have the same permissions as the parent directory. If you need to set
specific permissions for the new directory, you can use the "chmod" command.
5. rmdir:
The "rmdir" command is a Unix utility used to remove an empty directory from the file system. It stands
for
"remove directory".
The syntax of the command is simply "rmdir" followed by the name of the directory you want to
remove. For example:
Note that the directory must be empty before it can be removed with this command. If the directory is
not empty, you can use the "rm" command with the "-r" (recursive) option to remove the directory and
its contents.
However, be careful when using the "rm" command as it can permanently delete files and directories
without confirmation.
Also, the user running the "rmdir" command must have appropriate permissions to remove the
directory. If you do not have the necessary permissions, you will receive an error message.
6. man:
The "man" command is a Unix utility used to display the manual page for a given command or topic. The
word "man" is short for "manual".
The syntax of the command is simply "man" followed by the name of the command or topic you want to
learn about. For example:
7. ps:
The "ps" command is a Unix utility used to display information about running processes on the system.
The word "ps" stands for "process status".
The syntax of the command is simply "ps" followed by various options to customize the output. For
example:
This will display a list of all running processes, including system processes, along with their process IDs
(PIDs), CPU and memory usage, and other details.
• "f": displays a full listing that includes the process hierarchy and relationships.
8. cat:
The "cat" command is a Unix utility used to display the contents of one or more files on the terminal.
The name "cat" stands for "concatenate".
The syntax of the command is simply "cat" followed by the name of the file(s) you want to display. For
example:
In addition, you can use the "cat" command in combination with the output redirection operator (">") to
create a new file containing the contents of one or more input files.
9. echo:
The "echo" command is a Unix utility used to display a message or value on the terminal. It can be used
to print the contents of variables, display text on the screen, or create output that can be used as input
to other commands.
The syntax of the command is simply "echo" followed by the message or value you want to display. For
example:
In addition, you can use the "echo" command in combination with the output redirection operator (">")
to create a new file containing the output.
This will create a new file called "myfile.txt" containing the message " Written by echo command”.
10. date:
The "date" command is a Unix utility used to display or set the system date and time. It can be used to
display the current date and time, as well as to modify the date and time settings of the system.
The syntax of the command is simply "date" followed by various options to display or set the date and
time in different formats. For example:
• "+format": displays the date and time in a specific format. For example, "date +'%Y-%m-%d
%H:%M:%S'" will display the date and time in the format "YYYY-MM-DD HH:MM:SS".
• "-d": displays the date and time for a specific time or time interval. For example, "date -d '2 hours
ago'" will display the date and time 2 hours ago from the current time.
11. ls:
The "ls" command is a Unix utility used to list the files and directories in the current working directory or
in a specified directory. It can be used to display information about the file or directory, such as its
name, size, owner, permissions, and modification time.
The syntax of the command is simply "ls" followed by various options and arguments to specify the files
or directories to list and how to display the information. For example:
Some common options for the "ls" command include:
• "-l": displays the files and directories in a long format, including detailed information about the file or
directory such as its size, owner, permissions, and modification time.
• "-a": displays all files and directories, including hidden files that start with a dot (".") character.
• "-h": displays the file sizes in a human-readable format, such as "1K" for 1 kilobyte.
• "-t": sorts the files and directories by modification time, with the most recently modified files or
directories listed first.
Lab : 03
To study the Unix file system
Unix file system is a logical method of organizing and storing large amounts of information in a way that
makes it easy to manage. A file is a smallest unit in which the information is stored. Unix file system has
several important features. All data in Unix is organized into files. All files are organized into directories.
These directories are organized into a tree-like structure called the file system. Files in Unix System are
organized into multi-level hierarchy structure known as a directory tree. At the very top of the file
system is a directory called “root” which is represented by a “/”. All other files are “descendants” of
root.
• / : The slash / character alone denotes the root of the filesystem tree.
• /bin : Stands for “binaries” and contains certain fundamental utilities, such as ls or cp, which are
generally needed by all users.
• /boot : Contains all the files that are required for successful booting process.
• /dev : Stands for “devices”. Contains file representations of peripheral devices and pseudo-devices.
• /etc : Contains system-wide configuration files and system databases. Originally also contained
“dangerous maintenance utilities” such as init,but these have typically been moved to /sbin or
elsewhere.
• /home : Contains the home directories for the users.
• /lib : Contains system libraries, and some critical files such as kernel modules or device drivers.
• /media : Default mount point for removable devices, such as USB sticks, media players, etc.
• /mnt : Stands for “mount”. Contains filesystem mount points. These are used, for example, if the
system uses multiple hard disks or hard disk partitions. It is also often used for remote (network)
filesystems, CDROM/DVD drives, and so on.
• /root : The home directory for the superuser “root” – that is, the system administrator. This account’s
home directory is usually on the initial filesystem, and hence not in /home (which may be a mount point
for another filesystem) in case specific maintenance needs to be performed, during which other
filesystems are not available. Such a case could occur, for example, if a hard disk drive suffers physical
failures and cannot be properly mounted.
• /tmp : A place for temporary files. Many systems clear this directory upon startup; it might have tmpfs
mounted atop it, in which case its contents do not survive a reboot, or it might be explicitly cleared by a
startup script at boot time.
• /usr : Originally the directory holding user home directories,its use has changed. It now holds
executables, libraries, and shared resources that are not system critical, like the X Window System, KDE,
Perl, etc. However, on some Unix systems, some user accounts may still have a home directory that is a
direct subdirectory of /usr, such as the default as in Minix. (on modern systems, these user accounts are
often related to server or system use, and not directly used by a person).
• /usr/bin : This directory stores all binary programs distributed with the operating system not residing
in /bin, /sbin or (rarely) /etc.
• /usr/include : Stores the development headers used throughout the system. Header files are mostly
used by the #include directive in C/C++ programming language.
• /usr/lib : Stores the required libraries and data files for programs stored within /usr or elsewhere.
• /var : A short for “variable.” A place for files that may change often – especially in size, for example e-
mail sent to users on the system, or process-ID lock files.
• /var/mail : The place where all the incoming mails are stored. Users (other than root) can access their
own mail only. Often, this directory is a symbolic link to /var/spool/mail.
• /var/spool : Spool directory. Contains print jobs, mail spools and other queued tasks.
• /var/tmp : A place for temporary files which should be preserved between system reboots.
Types of Unix files
1. Regular files: Regular files contain user data and are stored in the file system. They can be text files,
binary files, or any other type of file that contains data.
2. Directories: Directories are special files that contain lists of other files and directories. They are used
to organize and manage the files in the file system.
3. Special files: Special files are used to represent system resources, such as devices, sockets, and
named pipes. These files appear as regular files to user applications but have special properties that
allow them to communicate with the system.
a) Character special files: Character special files represent devices that handle data as a stream of bytes,
such as keyboards, mice, and printers.
b) Block special files: Block special files represent devices that handle data in fixed-size blocks, such as
hard disk drives and CD-ROMs.
c) Named pipes: Named pipes are special files that allow two or more processes to communicate with
each other by sending and receiving data through a common file. They are also known as FIFOs (First In,
First Out).
There are several types of file systems used in Unix-based operating systems. Here are some of the most
commonly used ones:
1. Ext2 (Second Extended File System): This is the default file system used in many Linux distributions. It
is a mature and stable file system that supports large files and partitions. However, it does not support
journaling, which means that the file system may be vulnerable to data corruption in case of a system
crash or power failure.
2. Ext3 (Third Extended File System): Ext3 is an enhanced version of the Ext2 file system that adds
support for journaling, which makes it more robust and reliable. It is also backward-compatible with
Ext2, which means that Ext2 partitions can be upgraded to Ext3 without data loss.
3. Ext4 (Fourth Extended File System): Ext4 is the latest version of the Ext file system family. It includes
many performance and scalability improvements, such as support for larger files and partitions, faster
file system checks, and more efficient disk usage. It also supports journaling and can handle up to 64-bit
file systems.
4. XFS (eXtended File System): XFS is a high-performance file system that is optimized for large files and
highspeed data transfers. It supports journaling, dynamic resizing, and other advanced features. It is
commonly used in enterprise environments that require high throughput and low latency.
5. Btrfs (B-tree File System): Btrfs is a modern file system that was designed to address some of the
limitations of traditional file systems. It includes features such as built-in support for snapshots,
compression, and checksumming. It is still under development and not as widely used as other file
systems.
6. ZFS (Zettabyte File System): ZFS is a high-end file system that was originally developed by Sun
Microsystems (now Oracle). It includes advanced features such as data compression, data deduplication,
and support for multiple storage pools. It is commonly used in enterprise and data center environments.
Each file system has its own advantages and disadvantages, and the choice of file system depends on
factors such as performance requirements, data integrity needs, and available hardware resources.
Lab : 04
To study and use of file and directory handling commands (touch, cat, cp, rm, mv, more,
file, ls, wc, pg, comm, gzip, tar, zip, df, du.)
1. touch Command: It is used to create a new file or update the timestamp of an existing file.
2. cat Command: It is short for "concatenate" and is used to display the contents of a file on the
terminal. It can also be used to combine multiple files into a single file.
3. cp Command: It is used to copy files or directories. It takes two arguments: the source file/directory
and the destination where the file/directory will be copied.
4. rm Command: It is used to remove or delete files and directories. By default, it does not remove
directories, unless you specify the -r or -rf option to remove directories recursively.
5. mv Command: It is used to move or rename files and directories. It takes two arguments: the source
file/directory and the destination where the file/directory will be moved or renamed.
6. more Command: It is a command used for pagination. It displays the contents of a file one page at a
time and waits for user input to display the next page.
7. file Command: It is used to determine the type of a file. It examines the file's content and provides
information about the file type, such as whether it is a text file, binary file, or a specific file format.
8. ls Command: It is used to list files and directories in a directory. By default, it displays the names of
files and directories in the current directory.
9. wc Command: It is short for "word count" and is used to count the number of lines, words, and
characters in a file or input provided through the command line.
10. pg Command: It is a command used for pagination similar to more. It displays the contents of a file
one page at a time, but unlike more, it provides additional functionality like scrolling up and down
through the file.
11. comm Command: It is used to compare two sorted files line by line. It displays lines that are
common, unique, or different between the two files.
12. gzip Command: It is used to compress files. It reduces the file size and appends the extension ".gz"
to the compressed file. Compressed files can be decompressed using the gunzip command.
13. tar Command: It is used to create an archive file, often called a "tarball," by combining multiple files
and directories into one file. It does not compress the files, so it is often used with the gzip command to
create compressed tar archives (.tar.gz files).
14. zip Command: It is used to create compressed archive files. It combines multiple files and directories
into one compressed file with the extension ".zip". Compressed files can be extracted using the unzip
command.
15. df Command: It is used to display information about the disk space usage on a file system. It shows
the total, used, and available disk space for each mounted file system.
16. du Command: It is used to estimate disk space usage of files and directories. It provides information
about the disk space consumed by individual files and directories.
Lab : 05
To study of Networking commands (ping, telnet, ftp, finger, arp, rlogin.)
1. ping Command: It is used to check the reachability and latency of a remote host by sending Internet
Control Message Protocol (ICMP) echo request packets. It is a basic tool for troubleshooting network
connectivity.
2. telnet Command: It is a client-server protocol used to establish a connection to a remote host over
the network. The telnet command allows you to log in to a remote system and interact with it as if you
were physically present at the remote machine.
3. ftp Command: It stands for File Transfer Protocol and is used to transfer files between a local host
and a remote host. It provides a command-line interface for uploading, downloading, and managing files
on a remote server.
4. finger Command: It is used to retrieve information about a user on a remote system. By providing a
username or user ID, the finger command can display details such as the user's login name, home
directory, last login time, and more.
5. arp Command: It stands for Address Resolution Protocol and is used to view and manage the mapping
between IP addresses and MAC (Media Access Control) addresses on a local network. The arp command
can display the ARP cache and perform operations such as adding or deleting entries.
6. rlogin Command: It is a remote login protocol that allows you to log in to a remote system and
execute commands. The rlogin command establishes a remote session similar to telnet, but it
authenticates the user based on the local user account without requiring a password.
Lab : 06
To study various file attributes and permission related commands for directories and files
(chmod, umask).
1. chmod Command: It is used to change the permissions of files and directories. It allows you to modify
the read, write, and execute permissions for the owner, group, and others. The chmod command can be
used with symbolic notation (e.g., u+rwx) or octal notation (e.g., 755) to set permissions.
2. umask Command: It is used to set the default permissions for newly created files and directories. The
umask command modifies the permission mask that is applied when a new file or directory is created. It
works by subtracting the specified mask from the default permissions.
Lab : 07
To study vi editor and its operating modes. Create and edit a text file using the vi editor
commands.
To create and edit text file using the vi editor:
1. Open the terminal and type vi filename to create or open a file named "filename" with vi.
2. Once vi opens, you will be in Command Mode. Press "i" to switch to Insert Mode and start
typing to enter your desired text.
3. Use the navigation keys (arrow keys or "h," "j," "k," "l") to move the cursor within the file. Press
"Esc" to return to Command Mode.
4. While in Command Mode, you can issue various commands. For example:
5. To delete characters or lines, use the "x" key to delete a single character, or "dd" to delete a
line.
6. To copy, cut, and paste text, use the following commands in Command Mode:
• Copy: Move the cursor to the desired starting point, press "v" to enter Visual Mode,
navigate to select the desired text, and press "y" to copy.
• Cut: Similar to copying, but press "d" instead of "y" to cut the selected text.
• Paste: Move the cursor to the desired location and press "p" to paste the copied or cut
text.
7. To search for text, press "/" in Command Mode, enter the search pattern, and press Enter.
8. To save the changes and exit, press "Esc" to enter Command Mode and then type :wq followed
by Enter.
MODES:
i: Insert mode
p: Paste content
r: Replace
u: Undo
/: Forward Search
x: Remove
Lab : 08
Execution of various system administrative commands.
2. su: It is used to switch user or become another user. By typing su followed by a username, you
can switch to that user's account and execute commands as that user. The root user can be
accessed by typing su without specifying a username.
3. passwd: It is used to change the password of the current user account or another user account.
When executed without any arguments, it prompts you to enter your current password and
then allows you to set a new password.
4. useradd: It is used to create a new user account. When executed with appropriate options and
arguments, it creates a new user with the specified username, user ID, home directory, and
other settings.
5. usermod: It is used to modify user account properties. With this command, you can change
settings such as the username, user ID, home directory, login shell, and more for an existing user
account.
6. userdel: It is used to delete a user account. When executed with the appropriate options and
arguments, it removes the specified user account from the system, including its home directory
and associated files.
7. groupadd: It is used to create a new group. With this command, you can create a new group
with the specified group name and group ID.
8. groupmod: It is used to modify group properties. It allows you to change settings such as the
group name or group ID for an existing group.
9. groupdel: It is used to delete a group. When executed with the appropriate options and
arguments, it removes the specified group from the system.
10. shutdown: It is used to shut down the system. By running shutdown -h now, the system is
powered off immediately. You can specify a future time or a delay to schedule the shutdown.
11. reboot: It is used to restart the system. Running reboot causes the system to reboot
immediately.
Lab : 09
Write a shell script to display list of users currently logged in.
Save the script in a file, for example, users.sh. Then, make the file executable using the following
command:
The script will display the list of currently logged-in users in your system.
Explanation:
• The who command is used to retrieve the list of currently logged-in users.
• The output of the who command is piped to awk to extract the first field (username).
• The sort -u command is used to sort the usernames and remove duplicates.
Note: Running the script requires appropriate permissions, and it may require administrative privileges
to view the list of all logged-in users.
Lab : 10
Write a shell script to find the smallest of three numbers that are read from the keyboard.
Lab : 11
Write a shell script for calculator to perform basic arithmetic operations
Lab : 12
Write shell scripts to understand different types of loops with break and continue
statements.
Save the script in a file, for example, remove_duplicates.sh. Then, make the file executable using the
following command:
Create a text file with some duplicate lines. For example, let's create a file named input.txt with the
following content:
The script will prompt you to enter the input file name. In this case, enter input.txt and press Enter.
Next, the script will prompt you to enter the output file name. Enter a desired name, for example,
output.txt, and press Enter.
The script will execute and remove the duplicate lines from the input.txt file, saving the result in the
output.txt file. After completion, the script will display a message indicating the removal of duplicate
lines and the name of the output file. In this case, it will display:
You can verify the result by opening the output.txt file, which should contain the unique lines from the
original input.txt file, removing the duplicates. In this case, the output.txt file will contain: