Linux Notes - Merged
Linux Notes - Merged
Linux Notes - Merged
On the other hand, Linux OS is one of the famous versions of the UNIX OS. It is developed
to provide a low-cost or free OS for several personal computer system users. Remarkably, it
is a complete OS Including an X Window System, Emacs editor, IP/TCP, GUI (graphical
user interface), etc.
o Monolithic Kernel
o Micro kernels
o Exo kernels
o Hybrid kernels
4. Hardware layer:- Linux operating system contains a hardware layer that consists
of several peripheral devices like CPU, HDD, and RAM.
5. Shell:- It is an interface among the kernel and user. It can afford the services of
kernel. It can take commands through the user and runs the functions of the kernel.
The shell is available in distinct types of OSes. These operating systems are
categorized into two different types, which are the graphical shells and command-
line shells.
The graphical line shells facilitate the graphical user interface, while the command
line shells facilitate the command line interface. Thus, both of these shells implement
operations. However, the graphical user interface shells work slower as compared to
the command-line interface shells.
There are a few types of these shells which are categorized as follows:
o Korn shell
o Bourne shell
o C shell
o POSIX shell
o Hardware drivers: Most of the users of Linux face an issue while using Linux.
Various companies of hardware prefer to build drivers for Mac or Windows
due to they contain several users than Linux. Linux has small drivers for
peripheral hardware than windows.
o Software alternative: Let's take the Photoshop example which is a famous
tool for graphic editing. Photoshop exists for Windows; however, it is not
available in Linux. Also, there are some other tools for photo editing but the
Photoshop tool is more powerful as compare to others. Another example is
MS office which is not present for Linux users.
o Learning curve: Linux isn't a very user-friendly operating system. Hence, it
might be confusing for many beginners. Getting begun with Windows is
efficient and easy for many beginners; however, understanding Linux working
is complex.
We have to understand the command line interface and finding for newer
software is a little bit complex as well. When we face any issue in the OS, the
searching solution is very problematic. Also, there are various experts for Mac
and Windows as compare to Linux.
o Games: Several games are developed for Windows but unfortunately not for
Linux. Because the platform of Windows is used widely. So, the developers of
the games are more interested in windows.
File system of Linux is as follows:
General files: General files or ordinary files are files that contain images, text, or a
program. These files are in ASCII text or Binary format. The General files are the most
common in any Linux system.
Directory files: Directory files are the depository for other files. A directory can have
a subdirectory file within it. For Windows, we can understand them as folders.
Device files: Windows represents the external devices (Pendrives, hard drives, and
CD-ROM) as letters such as E: F: But, Linux represents devices as files, such as the
hard drive's partitions are represented as dev/sda1, dev/sda2, and more (depends
upon the number of partitions). All the device files take place within the directory
/dev.
Linux files are case sensitive; therefore, we can have two files having the same name;
one in upper case and other in lower case. Comparatively, the Windows files are not
case sensitive; we cannot have two files with the same name.
o Users
o Regular
o Administrative(root)
o Service
Linux File Ownership
Every Linux system have three types of owner:
1. User: A user is the one who created the file. By default, whosoever, creates the file becomes
the owner of the file. A user can create, delete, or modify the file.
2. Group: A group can contain multiple users. All the users belonging to a group have same
access permission for a file.
3. Other: Any one who has access to the file other than user and group comes in the category
of other. Other has neither created the file nor is a group member.
Syntax:
1. ls -lh
Look at the above snapshot, all the listed files and directories have the same user and group
that is sssit.First sssit column denotes the user and second column denotes the group.
Listing User Accounts
To know the local users account, following command can be used. It list out all the local
users from the system.
Syntax:
Syntax:
Example:
Look at the above snapshot, earlier 'file' group was sssit. But after passing the
command "chgrp php file" , we have changed the group to php (we have highlighted php
just to show you).
Note: Only root user have the permissison to change the owner or group of the files in the system.
Syntax:
Example:
Look at the above snapshot, we have changed the owner of the file list from sssit to jtp.
Command chown can also be used to change both user owner and group.
Syntax:
Example:
Look at the above snapshot; both user owner and group are changed
to jtp and php respectively.
AD
List of Special Files
When we type ls -l command, ten characters are displayed before user owner and group. First
character tells us about the type of the file.
- Normal file
d Directory
l Symbolic link
p Named pipe
b Blocked device
c Character device
s Socket
Look at the above snapshot, first letter c denotes the character device and b denotes the blocked
device
Look at the above snapshot, first letter (-) denotes the normal file and d denotes the directory.
File Permissions
All the three owners (user owner, group, others) in the Linux system have three types
of permissions defined. Nine characters denote the three types of permissions.
1. Read (r) : The read permission allows you to open and read the content of a file. But
you can't do any editing or modification in the file.
2. Write (w) : The write permission allows you to edit, remove or rename a file. For
instance, if a file is present in a directory, and write permission is set on the file but
not on the directory, then you can edit the content of the file but can't remove, or
rename it.
3. Execute (x): In linux type system, you can't run or execute a program unless execute
permission is set.But in Windows, there is no such permission available.
Look at the above snapshot, there are ten characters (-rw-rw-r--) before the user
owner. We'll describe these ten characters here.
When you are the User owner, then the user owner permission applies to you. Other
permissions are not relevant to you.
When you are the Group then the group permission applies to you. Other
permissions are not relevant to you.
When you are the other, then the other permission applies to you. User and group
permissions are not relevant to you.
Permission Example
Now we'll show some examples how permissions can be seen for a file or directory.
Look at the above snapshot, different directories and files have different permissions.
Now, from remaining nine letters, first triplet represents the permission for user
owner. Second triplet represents the permission for group owner. Third triplet represents
the permission for other .
Syntax:
Example:
Look at the above snapshot, permission to execute is added to the user owner group.
Syntax:
Example:
AD
Syntax:
Example:
Look at the above snapshot, we have given permission to write for all the groups.
Note: Similarly, you can also remove the permission for all the groups.
Syntax:
Example:
1. chmod +w file
Look at the above snapshot, this example is same as the earlier one only difference is
that we haven't typed a in this.
Syntax:
Example:
Look at the above snapshot, we have set explicit permission to read and write for
others.
Syntax:
Example:
For example, to set r octal will be 4, to set w octal will be 2, to set x octal will be 1.
Octal Table:
000 0 ---
001 1 --x
010 2 -w-
011 3 -wx
100 4 r--
101 5 r-x
110 6 rw-
111 7 rwx
1. 777 = rwxrwxrwx
2. 765 = rwxrw-r-x
3. 654 = rw-r-xr--
and so on.
Look at the above snapshot, we have shown some random octal examples with the
numbers 777, 274 and 111.
umask
While creating a file or directory, by default a set of permissions are applied. These
default permissions are viewed by umask command.
For safety reasons all Unix systems doesn't provide execution permission to newly
created files.
mkdir -m
Syntax:
Example:
Look at the above snapshot, we have created two files new1 and new2 with
mode 777 and 000 respectively.
cp -p
The 'cp -p' command preserves the permissions and time stamps from source files.
Syntax:
1. cp -p <sourceFile> <destinationFile>
Example:
1. cp -p list dupli.txt
Look at the above snapshot, earlier permissions for files list and dupli.txt were
different. But after passing the command "cp -p list dupli.txt", both the files have
same permissions.
Linux Directories
What are Commands
o Built-in shell commands: They are part of a shell. Each shell has some built in
commands.
o External/Linux commands: Each external command is a separate executable
program written in C or other programming languages.
Directory Description
Command
pwd The pwd command stands for (print working directory). It displays the current
working location or directory of the user. It displays the whole working path
starting with /. It is a built-in command.
ls The ls command is used to show the list of a folder. It will list out all the files
in the directed folder.
mkdir With mkdir command you can create your own directory.
rmdir The rmdir command is used to remove a directory from your system.
The Linux home directory is a directory for a particular user of the system and consists of
individual files. It is also referred to as the login directory. This is the first place that occurs
after logging into a Linux system. It is automatically created as "/home" for each user in the
directory'. It is a standard subdirectory of the root directory. The root directory contains all
other directories, subdirectories, and files on the system. It is denoted by a forward slash (/).
The home directory can be said as a personal working space for all the users except root.
There is a separate directory for every user. For example, two users 'jtp1' and 'jtp2' will have
directories like "/home/jtp1" and "/home/jtp2". These users will have all the rights under their
directory files.
The root (administrative) user is the only user who has its home directory in a different
location by default. The path of the root user is '/root/', where it has control under all the
directories and files.
Shell Programming in Linux
What is a shell?
It is an interface between the user and operating system service. A shell provides
users with an interface that not only accepts human-readable commands but also
executes them.
An operating system consists of many parts, but the 2 core parts are the kernel and
shell, we know that the kernel is like the atom of the operating system, the sole
heart of the operating system. The kernel works a mediator between the software
and hardware.
The shell on the other hand is more like the right hand of the kernel, it takes input
from you in the form of commands, processes it, and then gives an output. The
shell is retrieved via the terminal that runs it. Shell also provides an interface
through which a user works on programs and scripts.
Types of shell
When you execute the commands, the result is then displayed on the terminal to
the user. The terminal is powerful and allows users to store commands in a file and
execute them together. Linux is not the only operating system with a command
line, even windows had the command prompt and so doe mac OS
The command line is a boon for developers and administrators, but it may not be
everyone‟s cup of coffee as memorizing so many command sand and an even
longer list of options is not an easy task. That is why there are 2 options for the
shell so that users can use what they prefer most.
2. Graphical shell
The graphical shell is nothing but the GUI – Graphical interface design. The
graphical shell provides an interface for manipulating programs based on the
graphical user interface. The GUI allows users to do bath tasks that the terminal
can and can not do.
For example, you can do tasks like allowing for operations such as opening,
closing, moving, and resizing windows, as well as switching focus between
windows and more which the terminal can‟t. The main advantage of the GUI is
that users need not type commands for every small task.
There are various shells that are available for Linux based operating systems, let us
look at some of them:
The prompt for the bash shell is a dollar sign ($). The bash shell is a derivative of
the Bourne shell, other derivatives of the Bourne shell include POSIX and Korn.
3. CSH (C shell)
The C shell was written, designed, and developed at the University of California by
Bill Joy. J8ust like bash has the prompt “$”, chs has the prompt “%”. The C shell
was developed to be very similar to the C programming language, it also includes
useful programming features like in-built support for arithmetic operations
The C shell feature „aliases‟ and command history which was missing in different
types of shells in Linux like the Bourne shell. The complete pathname for the C
shell is /bin/sch.
ZSH (Z shell)
Fish Shell
POSIX
TENEX
What is shell scripting?
We saw that shells are interactive, they accept commands as input from users and
execute them. But if we want to execute a bunch of commands again and again
(like a loop), we have to type in all commands each time in the terminal.
To avoid this, the shell can also take in the commands as the input from a file,
where we can compile all of these commands and execute them. These files are
called Shell programs or shell scripts and have the extension of “.sh”.
Shell programs, just like any other programming language in the world, have a
specific syntax and a set of rules. A shell script contains the following elements:
Easy to use
Portable
Quick start
Interactive debugging
Same syntax as that of command line
Avoids repetitive work and automation
Shell scripting is much quicker than writing commands in the terminal
Simple up learn
Disadvantages of shell programming
They say that every good thing has its negatives, which also hold true for shell
programming, here are some of its disadvantages:
Output
mmukul@192 Docs-Linux % read
This is to stdin
In the above command, we are providing an input to the stream and the read tool is
getting the input from stdin.
Now, an example of stdout is shown below −
Command
ls -ltr
Output
immukul@192 Downloads % ls -ltr
total 1085456
drwxr-xr-x@ 13 immukul staff 416 Dec 7 2019 source-code-pro-release
-rw-r--r--@ 1 immukul staff 350337 Dec 22 2019 messi.jpg
-rw-r--r--@ 1 immukul staff 5953321 Dec 22 2019 927225.png
-rw-r--r--@ 1 immukul staff 601852 Dec 22 2019 238870.jpg
.
We know that we make use of the ls command with the -ltr flag to list all the files in
a certain sorted fashion, where the lastly updated file is shown at the bottom. The
list is sent to the stdout data stream and the terminal then simply prints it out.
Now, an example of stderr is shown below −
Command
ls -ltr printit
The above command is invalid as I don’t have any directory named printit and it will
generate an error message that will be sent to stderr and then the terminal will print
it.
Output
immukul@192 Downloads % ls -ltr printit
ls: printit: No such file or directory
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner