0% found this document useful (0 votes)
11 views12 pages

Linux Answer

use full

Uploaded by

kachariyaaadarsh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views12 pages

Linux Answer

use full

Uploaded by

kachariyaaadarsh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

LINUX QUESTION WITH ANSWER

Q-1.Explain features of Linux operating system.

 Linux is a powerful, open-source operating system known for its flexibility, stability, and security

features of Linux

1. Open Source and Free

-> Linux is free and open-source software.

->This means its source code is publicly available, allowing anyone to view, modify, and distribute it.

2. Multi-User and Multitasking

 Multi-user: It allows multiple users to access and use the same system simultaneously. Each user has
their own private space and permissions, preventing them from interfering with other users' files or
processes.

 Multitasking: It can run multiple applications and processes at the same time. The Linux
kernel's scheduler efficiently manages and allocates CPU time to each process, ensuring smooth
and stable performance.

3. Security

 User and File Permissions: The system uses a robust permissions model that controls who
can read, write, and execute files.

 Sudo: The sudo (superuser do) command allows an authorized user to temporarily run a
command with the privileges of another user, typically the root user.

 Security-Enhanced Linux (SELinux) and AppArmor: These are mandatory access control
(MAC) mechanisms that provide an additional layer of security by creating security policies for
applications and restricting their capabilities.

4. Portability

Linux is highly portable, meaning it can run on a wide variety of hardware architectures.

5. Command-Line Interface (CLI) and Graphical User Interface (GUI)

Linux offers both a powerful command-line interface (CLI) and a user-friendly graphical user
interface (GUI).

 Shell: The CLI, or shell, is a text-based interface that allows users to interact with the
system by typing commands. It is efficient for automation and scripting.
 GUI: Linux supports various desktop environments like GNOME, KDE, and XFCE,
which provide a graphical, point-and-click experience similar to Windows or macOS.
LINUX QUESTION WITH ANSWER

Q-2. Explain the directory structure of Linux.

->The Linux directory structure is a hierarchical tree where every file and directory branches out from a
single top-level directory called the root directory

Key Directories and Their Purpose

Here's a breakdown of some of the most important directories:

 (The Root Directory) This is the base of the entire file system. All other directories and
files are located within this directory.
 home This directory contains the personal directories for all non-root users. Each user
can only write to their own home directory.
 /root This is the home directory for the root user (the system administrator). It is kept
separate from the other user directories in /home for security reasons.
 /bin and /sbin These directories contain essential binary executables or programs.
o /bin (binaries) holds common commands that are available to all users, such as
ls, cp, mv, and cat.
o /sbin (system binaries) contains commands for system administration, which are
typically only used by the root user, such as reboot, fdisk, and mount.
 /etc This directory contains system-wide configuration files. Think of it as the control
center for your system
 /usr This directory, which stands for "Unix System Resources," contains the majority of
user-related programs and data.
 /var This directory contains variable data that changes frequently during system
operation. This includes log files (/var/log), mail queues (/var/mail), and temporary
files that persist between reboots (/var/tmp).
 /boot This directory holds the static boot files required to start the system. This includes
the Linux kernel and the initial RAM disk, which are essential for the system to boot up.
 /dev This directory contains special device files that represent hardware and pseudo-
devices.
 /proc and /sys These are virtual file systems that contain information about the
running system and kernel.
 /tmp This directory is for temporary files created by users and applications. It is often
cleared out on reboot, so you should not store any important files here.

Q-3What is shell? Explain Types of shell in details.

A shell is a command-line interpreter that acts as an interface between a user and the
operating system's kernel.

Types of Shells
LINUX QUESTION WITH ANSWER

Bourne Shell Family

 Bourne Shell (sh): The original Unix shell created by Stephen Bourne at Bell Labs. It was
the first shell to introduce features like control flow (if/else statements, loops) and functions in
scripts.

Prompt: $ for regular users, # for root.

 Executable path: /bin/sh

 Bourne-Again Shell (Bash): Developed for the GNU Project, Bash is the default shell for
most Linux distributions and macOS.

 Prompt: often displays username@hostname:current_directory$


 Executable path: /bin/bash

 KornShell (ksh): Developed by David Korn, it's a powerful shell that combines the best
features of both the Bourne and C shells.

 Executable path: /bin/ksh

 Z Shell (zsh): A highly customizable and modern shell. It's an extended version of Bash with
a massive community and a rich ecosystem of themes and plugins. It is the default shell on
modern macOS versions.

 Features: Spelling correction, advanced command completion, better theme support, and
powerful globbing (pathname expansion).
 Executable path: /bin/zsh

 Friendly Interactive Shell (fish): As its name suggests, this shell is designed to be user-
friendly and interactive.

 Features: Autosuggestions, syntax highlighting, and a user-friendly configuration.


 Executable path: /usr/bin/fish

Q-4 Which command is useful to change the file permission? Explain it giving by example.

The Linux command used to change file permissions is chmod. Its name stands for "change mode.

1. Symbolic Mode

This method uses letters and symbols to add (+), remove (-), or set (=) permissions. It's often
easier to remember and is great for making specific, targeted changes.

The syntax is chmod [who][operator][permissions] [filename].


LINUX QUESTION WITH ANSWER

 Who:
o (user/owner)
u
o (group)
g
o (others)
o
o (all users - u, g, and o combined)
a
 Operator:
o + adds a permission.
o - removes a permission.
o = sets the permissions exactly, overriding all previous permissions for that class.
 Permissions:
o r (read)
o w (write)
o x (execute)

Example: Using Symbolic ModeBash


$ ls -l report.txt
-rw-r--r-- 1 user group 0 Aug 24 10:30 report.txt

The permissions are rw- for the owner, r-- for the group, and r-- for others.

 Add execute permission for the owner:

Bash

$ chmod u+x report.txt


$ ls -l report.txt
-rwxrw-r-- 1 user group 0 Aug 24 10:30 report.txt

 Remove write permission for the group:

Bash

$ chmod g-w report.txt


$ ls -l report.txt
-rwxr--r-- 1 user group 0 Aug 24 10:30 report.txt

 Set permissions for all (a) to be read-only (r):

Bash

$ chmod a=r report.txt


$ ls -l report.txt
-r--r--r-- 1 user group 0 Aug 24 10:30 report.txt

2. Octal (Numeric) Mode

This method uses a three-digit number to represent the permissions for the owner, group, and
others. It's a faster way to set multiple permissions at once.
LINUX QUESTION WITH ANSWER

Each permission has a numerical value:

 r (read) = 4
 w (write) = 2
 x (execute) = 1
 no permission = 0

You add these numbers for each category (user, group, others) to create a three-digit code.

Example: Using Octal Mode

Let's use the same report.txt file.

 Give the owner full permissions (read, write, execute), the group read and execute,
and others only read access.
o Owner permissions: rwx = 4 + 2 + 1 = 7
o Group permissions: r-x = 4 + 0 + 1 = 5
o Others permissions: r-- = 4 + 0 + 0 = 4

The final code is 754.

Bash

$ chmod 754 report.txt


$ ls -l report.txt
-rwxr-xr-- 1 user group 0 Aug 24 10:30 report.txt

 Give everyone read and write permissions, but no execute permissions.


o Owner: rw- = 4 + 2 + 0 = 6
o Group: rw- = 4 + 2 + 0 = 6
o Others: rw- = 4 + 2 + 0 = 6

The final code is 666.

Bash

$ chmod 666 report.txt


$ ls -l report.txt
-rw-rw-rw- 1 user group 0 Aug 24 10:30 report.txt

Q-5 Explain chmod command using relative and absolute method.


The chmod command is used in Linux to change file permissions. There are two
main methods to use chmod: symbolic mode (relative) and octal mode (absolute).
LINUX QUESTION WITH ANSWER

Symbolic Mode (Relative)

Symbolic mode uses letters and symbols to add, remove, or set permissions relative to the
existing ones.

The basic syntax is chmod [who][operator][permissions] [filename].

 Who: Specifies who the permissions apply to.


o u (user/owner)
o g (group)
o o (others)
o a (all users - u, g, and o combined)
 Operator: Defines the action to perform.
o + adds the specified permissions.
o - removes the specified permissions.
o = sets the permissions exactly as specified, overwriting any previous permissions.
 Permissions: The specific permissions to change.
o r (read)
o w (write)
o x (execute)

Example: Symbolic Mode

 To add execute permission for the owner:

Bash

$ chmod u+x script.sh

The new permissions will be -rwxr--r--.

 To remove write permission from the group and others:

Bash

$ chmod go-w script.sh

The new permissions will be -rw-r--r--.

 To set the permissions for all to be exactly read and write, removing any execute
permissions:

Bash

$ chmod a=rw script.sh


LINUX QUESTION WITH ANSWER

The new permissions will be -rw-rw-rw-.

Octal Mode (Absolute)

Octal mode uses a three-digit number to represent the absolute permissions for the owner, group,
and others.

The syntax is chmod [octal-code] [filename].

The numerical values are:

 r (read) = 4
 w (write) = 2
 x (execute) = 1
 no permission = 0

To get the octal code, you add the values for the desired permissions for each user class.

Permissions Value

rwx 4+2+1=7

rw- 4+2+0=6

r-x 4+0+1=5

r-- 4+0+0=4

-wx 0+2+1=3

-w- 0+2+0=2

--x 0+0+1=1

--- 0+0+0=0

Export to Sheets

Example: Octal Mode

Let's use the same file, script.sh.

 To give the owner read, write, and execute permissions (rwx), the group read and
execute (r-x), and others only read access (r--):
o Owner: rwx = 7
o Group: r-x = 5
o Others: r-- = 4

The final octal code is 754.


LINUX QUESTION WITH ANSWER

Bash

$ chmod 754 script.sh

The new permissions will be -rwxr-xr--.

 To make a file executable by everyone:


o Owner: rwx = 7
o Group: rwx = 7
o Others: rwx = 7

The final octal code is 777.

Bash

$ chmod 777 script.sh

The new permissions will be -rwxrwxrwx.

Q-6 Write note on ls command with options and example.

The ls (list) command is one of the most fundamental and frequently used commands in
Linux.

Common Options for ls

The power of the ls command lies in its various options, which modify its behavior to provide
more detailed or specific information. You can combine multiple options by placing them
together (e.g., ls -la).

 -l (long listing format): This is one of the most useful options. It displays a detailed list
of files, including their permissions, number of links, owner, group, size, and last
modification date.
 -a (all files): Lists all files, including hidden files. In Linux, a file is considered hidden if
its name starts with a dot (.). The output will include the special directories . (current
directory) and .. (parent directory).
 -h (human-readable): Used with -l, this option displays file sizes in a more readable
format (e.g., 1K, 234M, 2G) instead of in bytes.
 -t (time sort): Sorts files and directories by their last modification time, with the newest
files listed first.
 -r (reverse sort): Reverses the order of the sort. For example, when used with -t, it will
list the oldest files first.
 -F (classify): Adds a special character to the end of each entry to indicate its type. For
instance, a / is added to directories, a * to executable files, and an @ to symbolic links.
LINUX QUESTION WITH ANSWER

Examples

 List all files in the current directory, including hidden ones:

Bash

ls -a

 List all files in the current directory with detailed information:

Bash

ls -l

 List all files and directories in a human-readable, long-listing format, sorted by


modification time (newest first):

Bash

ls -lht

 List the contents of a specific directory (e.g., /home/user/Documents):

Bash

ls /home/user/Documents

This shows that you don't have to be in the directory to list its contents.

 List a detailed, human-readable list of all files, with the type of each file indicated:

Bash

ls -lahF

Q-7 Explain Insert, Delete and Change command for sed.

The sed (stream editor) command is a powerful tool for manipulating text streams and files.

1. Insert (i)

The i command inserts new text before a specified line. It's used to add content that doesn't
already exist in the file.
LINUX QUESTION WITH ANSWER

Syntax:

sed 'line_number i\ text to insert' filename

Example:
Bash
sed '1 i\
--- Start of Report ---' report.txt

2. Delete (d)

The d command deletes entire lines. You can specify a single line, a range of lines, or lines that
match a certain pattern.

Syntax:

sed 'address d' filename

Examples:

 To delete the third line:

Bash

sed '3d' filename.txt

3.Change (c)

The c command changes or replaces an entire line or a range of lines with new text.

Syntax:

sed 'address c\ new text to replace with' filename

Examples:

 To replace line 3 with the new text "This is the new third line.":

Bash

sed '3c\
This is the new third line.' filename.txt

Q-8 Explain expr command giving example for string manipulation.


LINUX QUESTION WITH ANSWER

The expr command in Linux evaluates an expression and prints the result to standard output.

String Manipulation with expr

Here are some common string manipulation operations using expr:

 String Length (length): The length operator returns the number of characters in a
string.
o Example: To find the length of the string "hello":

Bash

expr length "hello"

Output:

 Substring (substr): The substr operator extracts a substring from a string, starting at a
specific position for a given length.
o Syntax: expr substr string position length
o Example: To extract 3 characters from the string "expression" starting at position
2:

Bash

expr substr "expression" 2 3

Output:

xpr

 Finding a Substring (index): The index operator returns the position of the first
character of the substring within a larger string. If the substring is not found, it returns 0.
o Syntax: expr index string substring
o Example: To find the position of "world" in "hello world":

Bash

expr index "hello world" "world"

Output:

 Regular Expression Match (:): The colon (:) operator performs a regular expression
match. It returns the length of the matched part of the string.
LINUX QUESTION WITH ANSWER

o Syntax: expr string : "regexp"


o Example: To find the length of the number at the beginning of a string:

Bash

expr "123hello" : "[0-9][0-9]*"

Output:

Here, [0-9] matches a digit, and [0-9]* matches zero or more digits, so it
matches the "123" and returns its length.

 Matching and Extracting Substrings: You can use parentheses to create a capture
group in the regular expression. expr will then return the captured substring instead of its
length.
o Syntax: expr string : "regexp(capture_group)"
o Example: To extract the text "linux" from a string:

Bash

expr "i love linux" : ".*[ ]\([^ ]*\)"

Output: linux

Q-9 Explain redirection in Linux operating system.

You might also like