Unix Pyq
Unix Pyq
Shell Types
In Unix, there are two major types of shells −
Bourne shell − If you are using a Bourne-type shell, the $ character is the default
prompt.
C shell − If you are using a C-type shell, the % character is the default prompt.
3. How to display system date in unix?
date command is used to display the system date and time. date command is also used
to set date and time of the system. By default the date command displays the date in the
time zone on which unix/linux operating system is configured.You must be the super-
user (root) to change the date and time.
Syntax:
date [OPTION]... [+FORMAT]
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]].
Simply type the following command at the shell prompt to get the current date
in MM-DD-YYYY format: $ NOW = $(date +"%m-%d-%Y")
Attributes in Linux
Some filesystems support additional attributes (other than those described in the
preceding sections). In particular, some Linux-native filesystems support several
attributes that you can adjust with the chattr command. The files and directories
can have the following attributes:
On UNIX-based systems, a hard link is just another name for a file that already exists. It's
typically found in file systems that allow various hard links to the same file. Hard links
have the same Inode value, unlike soft links, but they point to the file location rather
than the directory.
You may also use soft links to link files across the file system. Generally, the soft link is
an alias for the original file or directory. It helps to redirect to the specified file and
directory when accessed via the pathname indicated in the soft link's subject.
It is a copy of the original file that serves as a It is a short pointer file that links a filename to a
pointer to the same file, allowing it to be pathname. It's nothing more than a shortcut to the
accessed even if the original file is deleted or original file, much like the Windows OS's shortcut
relocated. option.
It has a similar inode number to the target file. It has a different inode number.
It is not allowed the relative path. It allows both relative and absolute paths.
It cannot be established outside the file system. It may be established in the file system.
It is faster. It is slower.
The "ln" command is used to make a hard link The "ln -s" command is used to make a soft link in
in Linux. Linux.
It has an additional name for the original file It is different from the original file and is an
that references to the target file through inode. alternative for it, but it does not use inode.
It remains valid even if the target file is deleted. It becomes invalid when the originating file is
deleted.
cp : cp stands for a copy. This command is used to copy files or groups of files
or directories. It creates an exact image of a file on a disk with a different file
name. cp command requires at least two filenames in its arguments.
Syntax:
cp [OPTION] Source Destination
cp [OPTION] Source Directory
cp [OPTION] Source-1 Source-2 Source-3 Source-n Directory
Cat : (concatenate) command is very frequently used in Linux. It reads data from the
file and gives their content as output. It helps us to create, view, concatenate files. So let
us see some frequently used cat commands.
9. Aliases in UNIX
alias command instructs the shell to replace one string with another string while
executing the commands.
When we often have to use a single big command multiple times, in those cases, we
create something called as alias for that command. Alias is like a shortcut command
which will have same functionality as if we are writing the whole command.
Syntax:
alias [-p] [name[=value] ... ]
Aliases are mostly used to replace long commands, improving efficiency and avoiding
potential spelling errors. Aliases can also replace commands with additional options,
making them easier to use.
A path is a unique location to a file or a folder in a file system of an OS.A path to a file
is a combination of / and alpha-numeric characters.
Absolute Path-name
An absolute path is defined as specifying the location of a file or directory from the
root directory(/). In other words,we can say that an absolute path is a complete path
from start of actual file system from / directory.
cat /home/kt/abc.sql
In the above example, if the first character of a pathname is /, the file’s location
must be determined with respect to root. When you have more than one / in a
pathname, for each such /, you have to descend one level in the file system like
in the above kt is one level below home, and thus two levels below root.
Relative path
Relative path is defined as the path related to the present working directly(pwd). It starts
at your current directory and never starts with a / .
To be more specific let’s take a look on the below figure in which if we are looking for
photos then absolute path for it will be provided as : /home/jono/photos but assuming that
we are already present in jono directory then the relative path for the same can be written
as simple photos.
However on two different filesystems inode numbers can be the same. You’d
need to combine the inode with the device ID to be sure to have a unique
identifier.
And device ID can repeat on different systems. For that you’d need to combine it
with some unique system identifier.
fork()
pid_t pid = fork();
fork() creates a new process by duplicating the calling process, The new process,
referred to as child, is an exact duplicate of the calling process, referred to as parent,
except for the following :
1. The child has its own unique process ID, and this PID does not match the ID
of any existing process group.
2. The child’s parent process ID is the same as the parent’s process ID.
3. The child does not inherit its parent’s memory .
exec()
The exec() family of functions replaces the current process image with a new
process image.
fork vs exec
fork starts a new process which is a copy of the one that calls it, while exec
replaces the current process image with another one.
Both parent and child processes are executed simultaneously in case of fork()
while Control never returns to the original program unless there is an exec()
error.
What is an inode?
An inode is a file data structure that stores information about any Linux file except its
name and data.
What are inodes used for?
Data is stored on your disk in the form of fixed-size blocks. If you save a file that exceeds a
standard block, your computer will find the next available segment on which to store the rest of
your file. Over time, that can get super confusing.
That’s where inodes come in. While they don’t contain any of the file’s actual data, it stores the
file’s metadata, including all the storage blocks on which the file’s data can be found.
Role of set-user-id
Setuid, which stands for set user ID on execution, is a special type of file permission in
Unix and Unix-like operating systems such as Linux and BSD. It is a security tool that
permits users to run certain programs with escalated privileges.
When an executable file's setuid permission is set, users may execute that program with a
level of access that matches the user who owns the file. For instance, when a user wants
to change their password, they run the passwd command. The passwd program is owned
by the root account and marked as setuid, so the user is temporarily granted root access
for that limited purpose.
/lib: This directory contains all the information of system libraries functions and some
critical files such as kernel modules or device drivers. It also contains the system calls
that the compiler can include in a program.
/bin: This directory contains the system's binary files and certain fundamental utilities. It
is the directory for admin-level commands such as ls or cp. The path (var) always shows
this directory in the list.
/dev: It stands for "devices" and contains all the information about the device files used
in the UNIX system. These files do not occupy any space on the disk. It contains al;
information about hard disks, file representations of peripheral devices, floppy disks,
device information, and pseudo-devices.
/tmp: This directory contains the information of all the temporary files created by UNIX
or by the user in the UNIX system.
/usr: This directory contains all the information related to the users in a UNIX system. It
means it contains all the user account such as user1, user2, and so on.
Multi-programming VS Multi-tasking
CPU is a super fast device and keeping it occupied for a single task is never a good
idea. Considering the huge differences between CPU speed and IO speed, many
concepts like multiprogramming, multitasking, multithreading, etc have been
introduced to make better CPU utilisation.
Multi programming:-
Multi-programming increases CPU utilisation by organising jobs (code and data) so that
the CPU always has one to execute. The idea is to keep multiple jobs in main memory.
If one job gets occupied with IO, CPU can be assigned to other job.
Multi-tasking:-
Multi-tasking is a logical extension of multiprogramming. Multitasking is the ability of
an OS to execute more than one task simultaneously on a CPU machine. These
multiple tasks share common resources (like CPU and memory). In multi-tasking
systems, the CPU executes multiple jobs by switching among them typically using a
small time quantum, and the switches occur so quickly that the users feel like interact
with each executing task at the same time.
Note: 1. A task in a multitasking system is not whole application program but it can
refers to a “thread of execution” when one process is divided into sub-tasks.
2. Multitasking OS are time sharing systems.
Difference between Multiprogramming and Multi-tasking
Sr.no Multiprogramming Multi-tasking
It includes the single CPU to execute It uses multiple tasks for the task
1.
the program. allocation.
5. The idea is to reduce the CPU idle The idea is to further extend the CPU
Sr.no Multiprogramming Multi-tasking
What is a daemon?
A daemon is also called background processes. It is a UNIX or Linux program that
executes inside the background. Almost every daemon contains names that finish with
"d" the letter. For example, sshd, this manages connections of SSH remote access, or the
httpd daemon that manages the Apache server. Often, Linux begins daemons at starting
time.