Linux Unit - 2

 Unit-2 

Linux file system  

A Linux file system is a structured collection of files on a disk drive or a partition. A partition is a  segment of memory and contains some specific data. In our machine, there can be various partitions  of the memory. Generally, every partition contains a file system. 

The general-purpose computer system needs to store data systematically so that we can easily  access the files in less time. It stores the data on hard disks (HDD) or some equivalent storage type.  There may be below reasons for maintaining the file system: 

o Primarily the computer saves data to the RAM storage; it may lose the data if it gets turned  off. However, there is non-volatile RAM (Flash RAM and SSD) that is available to maintain  the data after the power interruption. 

o Data storage is preferred on hard drives as compared to standard RAM as RAM costs more  than disk space. The hard disks costs are dropping gradually comparatively the RAM. 

The Linux file system contains the following sections: 

o The root directory (/) 

o A specific data storage format (EXT3, EXT4, BTRFS, XFS and so on) 

o A partition or logical volume having a particular file system. 

Linux Shell 

The shell can be defined as a command interpreter within an operating system like Linux/GNU or  Unix. It is a program that runs other programs. The shell facilitates every user of the computer as an  interface to the Unix/GNU Linux system. Hence, the user can execute different tools/utilities or  commands with a few input data. 

The shell sends the result to the user over the screen when it has completed running a program  which is the common output device. That's why it is known as "command interpreter".

The shell is not just a command interpreter. Also, the shell is a programming language with complete  constructs of a programming language such as functions, variables, loops, conditional execution,  and many others. 

For this reason, GNU/Unix Linux Shell is stronger than the Windows shell. 

Broadly, the shell is categorized into two main categories which are explained below:

Graphical Shells 

These shells specifies the manipulation of programs that are based on the graphical user interface  (GUI) by permitting for operations like moving, closing, resizing, and opening windows and  switching focus among windows as well. Ubuntu OS or Windows OS could be examined as a good  example that offers a graphical user interface to the user to interact with the program. Various users  don't need for typing in any command for all the actions. 

Command-line Shell 

Various shells could be accessed with the help of a command-line interface by users. A unique  program known as Command prompt in Windows or Terminal in macOS/Linux is offered for typing  in the human-understandable commands like "ls", "cat", etc and after that, it is being run. The result  is further shown to the user on the terminal. 

Working on a command-line shell is a complicated for many beginners due to it is hard to remember  several commands. Command-line shell is very dominant and it permits users for storing commands  in a file and run them together. In this way, a repetitive action could be automated easily. Usually,  these files are known as Shell scripts in macOS/Linux systems and batch files in Windows. 

There are various types of shells which are discussed as follows: 

Bash Shell

In the bash shell, bash means Bourne Again Shell. It is a default shell over several distributions of  Linux today. It is a sh-compatible shell. It could be installed over Windows OS. It facilitates practical  improvements on sh for interactive and programming use which contains: 

o Job Control 

o Command-line editing 

o Shell Aliases and Functions 

o Unlimited size command history 

o Integer arithmetic in a base from 2-64 

Csh/Tcsh Shell 

Tcsh is an upgraded C shell. This shell can be used as a shell script command processor and  interactive login shell. 

Tcsh shell includes the following characteristics: 

o C like syntax 

o Filename completion and programmable word 

o Command-line editor 

o Job control 

o Spelling correction 

Ksh Shell 

Ksh means for Korn shell. It was developed and designed by David G. Korn. Ksh shell is a high-level,  powerful, and complete programming language and it is a reciprocal command language as well just  like various other GNU/Unix Linux shells. The usage and syntax of the C shell are very same as the C  programming language. 

Zsh Shell 

Zsh shell is developed to be reciprocal and it combines various aspects of other GNU/Unix Linux  shells like ksh, tcsh, and bash. Also, the POSIX shell standard specifications were based on the Korn  shell. 

Also, it is a strong scripting language like other available shells. Some of its unique features are listed as follows: 

o Startup files 

o Filename generation 

o Login/Logout watching 

o Concept index 

o Closing comments

o Variable index 

o Key index 

o Function index and various others that we could find out within the man pages. All these shells do a similar job but take different commands and facilitate distinct built-in functions. Fish 

Fish stands for "friendly interactive shell". It was produced in 2005. Fish shell was developed to be  fully user-friendly and interactive just like other shells. It contains some good features which are  mentioned below: 

o Web-based configuration 

o Man page completions 

o Auto-suggestions 

o Support for term256 terminal automation 

o Completely scripted with clean scripts 

Linux Text Editors 

Linux text editors can be used for editing text files, writing codes, updating user instruction  files, and more. A Linux system supports multiple text editors. There are two types of text editors in  Linux, which are given below: 

o Command-line text editors such as Vi, nano, pico, and more. 

o GUI text editors such as gedit (for Gnome), Kwrite, and more. 

A text editor plays an important role while coding. So, it is important to select the best text editor. A  text editor should not only be simple but also functional and should be good to work with. 

A text editor with IDE features is considered as a good text editor. 

In this section, we are going to discuss the top 20 text editors for Linux. Further, we will talk about  the latest text editors and will compare them with the traditional text editors such as Vi and nano.  This will help you with selecting the editor of your choice. 

1. Vi/VIM editor 

2. Nano editor 

3. Gedit editor 

4. Sublime text editor

5. VSCode 

6. GNU emacs 

7. Atom editor 

8. Brackets editor 

9. Pico editor 

10. Bluefish 

Virtual consoles allow the storage of multiple text buffers, enabling different console programs to  run simultaneously but interact with the user in different contexts. From the user's point of view,  this creates the illusion of several independent consoles. 

Each virtual console can have its own character set and keyboard layout. Linux 2.6 introduced the  ability to load a different font for each virtual console (kernel versions predating 2.6 change the font  only on demand). 

The text mode implementation is used on PC-based systems with a legacy CGA/EGA/MDA/VGA  video card that implements text-based video modes. In text mode, the kernel sends a 2D array of  characters to the video card, and the video card converts the characters to pixels for display. 

The text buffer is a part of VGA memory which describes the content of a text screen in terms  of code points and character attributes. Code points in the text buffer and font are generally not the  same as encoding used in text terminal semantics to put characters on the screen. The set  of glyphs on the screen is determined by the current font. The text screen is handled  by console.c and consolemap.c drivers. There is a utility for altering fonts and terminal encodings  called consolechars

Working with Linux console-text based and virtual based 

Virtual Console 

To understand the Linux virtual console concept, we have to look back in the history of computers.  In earlier days, computers used to be very expensive. Instead of personal computers, companies  used to prefer mainframe computers. A mainframe computer allows multiple users to connect with  it separately. In mainframe computing, every user accesses the mainframe computer as a separate  computer.

A device known as terminal which contains only few parts and a monitor with integrated keyboard is  used to access the mainframe computer. The earliest terminals were also known as teletypes  (abbreviated TTY). 

Terminal connects with mainframe computer on serial console port. Once connected, it uses all  resources such as CPU, RAM and Hard disk from mainframe computer. 

Since a terminal directly communicates with system at low level on dedicated serial console port, it  does not need any special service, software or application to run. When you booted up the terminal,  you would see a login prompt called Linux console on monitor.

In terminal, Linux console is the only place where you can enter commands for system. Linux  operating system installed in mainframe computer emulates each console as a separate system with  a separate login session. 

Over the time, technology improved and the price of personal computers came down which made  companies to switch on personal computers from mainframe computers. Personal computers not  only have all necessary resources for operating system but also easier to setup and manage. 

Since PCs have all necessary resource for operating system, instead of connecting those with  mainframe computer companies preferred to install an individual operating system in each PC.  Gradually terminals became outdated and replaced by PCs. 

Even though terminals are history now, terminal concept still exists in Linux operating system. A  Linux operating system not only have necessary software configuration that allows physical  terminals to connect with it but also offers a way to access that software configuration virtually. That  way is known as virtual console. 

Physical console: - A physical terminal device connected with Linux system on serial port via serial  cable physically. 

Virtual console: - An application that simulates a physical terminal device in software and connects it  with Linux system on serial port through software configuration virtually. 

Physical terminal device: - A physical device that consisted of nothing more than a monitor and  keyboard attached to it. It uses all resources such as CPU, RAM and Hard disk from server system. 

Virtual terminal application: - An application that provides a text based environment to access the  shell. Since physical terminals are no longer used, it became common practice to use the  word terminal to refer the virtual terminal application. 

Number of virtual consoles 

Different Linux flavors offer different number of virtual consoles. For instance, RHEL provides six  virtual consoles while Ubuntu provides seven virtual consoles. Virtual consoles are always  mentioned along with one physical console (also known as default console). So, the actual number of  virtual consoles remains one less than the total number of consoles. For example, in RHEL and  Ubuntu the number of actual virtual consoles are 5 (six - one) and 6 (seven - one) respectively.

Text modes 

The text mode implementation is used on PC-based systems with a legacy CGA/EGA/MDA/VGA  video card that implements text-based video modes. In text mode, the kernel sends a 2D array of  characters to the video card, and the video card converts the characters to pixels for display. 

The text buffer is a part of VGA memory which describes the content of a text screen in terms  of code points and character attributes. Code points in the text buffer and font are generally not the  same as encoding used in text terminal semantics to put characters on the screen. The set  of glyphs on the screen is determined by the current font. The text screen is handled  by console.c and consolemap.c drivers. There is a utility for altering fonts and terminal encodings  called consolechars

The Linux kernel (keyboard.c driver) has almost complete support for keyboard input (keyboard  layouts), but it remains a bit inconsistent because it interacts badly with different character sets.  Layouts are loaded by the loadkeys utility. 

These two utilities and corresponding data files are packed in Linux Console  Tools http://lct.sourceforge.net/ shipped with many Linux distributions

Efforts on the internationalization of Linux at the kernel level started as early as in 1994 by Markus  Kuhn and Andries Brouwer

The Linux console is capable of supporting any VGA-style text mode, but the kernel itself has very  limited means to set these modes up. SVGATextMode helps to enable more complex text modes  than the standard EGA and VGA modes. It is fully compatible with Console Tools, but has some  conflicts with dosemu, SVGAlib and display servers

Currently, there is no support for different modes on different virtual consoles. 

File name expansion 

After word splitting, unless the -f option has been set (see section 4.3 The Set Builtin), Bash scans  each word for the characters '*', '?', and '['. If one of these characters appears, then the word is  regarded as a pattern, and replaced with an alphabetically sorted list of file names matching the  pattern. If no matching file names are found, and the shell option nullglob is disabled, the word is  left unchanged. If the nullglob option is set, and no matches are found, the word is removed. If the  shell option nocaseglob is enabled, the match is performed without regard to the case of alphabetic  characters. 

When a pattern is used for filename generation, the character '.' at the start of a filename or  immediately following a slash must be matched explicitly, unless the shell option dotglob is set.  When matching a file name, the slash character must always be matched explicitly. In other cases,  the '.' character is not treated specially. 

See the description of shopt in section 4.2 Bash Builtin Commands, for a description of  the nocaseglob, nullglob, and dotglob options.

The GLOBIGNORE shell variable may be used to restrict the set of filenames matching a pattern.  If GLOBIGNORE is set, each matching filename that also matches one of the patterns  in GLOBIGNORE is removed from the list of matches. The filenames '.' and '..' are always ignored,  even when GLOBIGNORE is set. However, setting GLOBIGNORE has the effect of enabling  the dotglob shell option, so all other filenames beginning with a '.' will match. To get the old  behavior of ignoring filenames beginning with a '.', make '.*' one of the patterns in GLOBIGNORE.  The dotglob option is disabled when GLOBIGNORE is unset. 

Concept of Pipe  

The Pipe is a command in Linux that lets you use two or more commands such that output of one  command serves as input to the next. In short, the output of each process directly as input to the  next one like a pipeline. The symbol ‘|’ denotes a pipe. 

Pipes help you mash-up two or more commands at the same time and run them consecutively. You  can use powerful commands which can perform complex tasks in a jiffy. 

Let us understand this with an example. 

When you use ‘cat’ command to view a file which spans multiple pages, the prompt quickly jumps to  the last page of the file, and you do not see the content in the middle. 

To avoid this, you can pipe the output of the ‘cat’ command to ‘less’ which will show you only one  scroll length of content at a time. 

cat filename | less 

An illustration would make it clear.

Job:-Background 

A job is a process that the shell manages. Each job is assigned a sequential job ID. Because a job is a  process, each job has an associated PID. There are three types of job statuses: 1. Foreground: When you enter a command in a terminal window, the command occupies that  terminal window until it completes. This is a foreground job. 

2. Background: When you enter an ampersand (&) symbol at the end of a command line, the  command runs without occupying the terminal window. The shell prompt is displayed immediately  after you press Return. This is an example of a background job. 

3. Stopped: If you press Control + Z for a foreground job, or enter the stop command for a  background job, the job stops. This job is called a stopped job. 

Note: Except the Bourne shell, the other shells support job control. 

Job Control Commands 

Job control commands enable you to place jobs in the foreground or background, and to start or  stop jobs. The table describes the job control commands. 


Option 

jobs 

bg %n 

fg %n 

Control-Z 



Description 

Lists all jobs 

Places the current or specified job in the background, where n is the job ID Brings the current or specified job into the foreground, where n is the job ID Stops the foreground job and places it in the background as a stopped job 

Note: The job control commands enable you to run and manage multiple jobs within a shell.  However, you can use the job control commands only in the shell where the job was initiated. 

Running a Job in the Background 

To run a job in the background, you need to enter the command that you want to run, followed by  an ampersand (&) symbol at the end of the command line. For example, run the sleep command in  the background. 

$ sleep 100 &  

[1] 1302 

The shell returns the job ID, in brackets, that it assigns to the command and the associated PID. With  the job ID, you can use the job control commands to manage the job whereas the kernel uses PIDs to  manage jobs. 

When a background job is complete and you press Return, the shell displays a message indicating  the job is done.

[1] + Done sleep 100 & 

Managing the background jobs 

You can use the jobs command to list the jobs that are currently running or suspended in the  background. 

$ jobs 

[1]+ Running sleep 100 & 

You can use the fg command to bring a background job to the foreground. 

$ fg % 1 

sleep 100 

Note: The foreground job occupies the shell until the job is completed, suspended, or stopped and  placed into the background. 

You can use the ‘Control+Z keys and bg command to return a job to the background. The Control+Z  keys suspend the job, and place it in the background as a stopped job. The bg command runs the job  in the background. For example: 

1. Using CTRL+Z 

$ sleep 100 

^Z 

[1]+ Stopped sleep 100 

$ jobs 

[1]+ Stopped sleep 100 

2. Using bg 

$ bg % 1 

[1]+ sleep 100 & 

$ jobs 

[1]+ Running sleep 100 &

Kill & Interruption 

A user can stop or interrupt any command such as cp, mv and others by pressing CTRLc (press and  hold ctrl key followed by c) during the execution of a command or bash/perl/python script. Another  option is to use the kill command to kill process by PID: 

Get process id for firefox by running the following command (open another terminal and type the  command): 

$ ps aux | grep -i firefox 

USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND 

veryv 31241 2.8 9.0 5806852 752084 ?? S 11:08AM 31:35.18  

/Applications/Firefox.app/Contents/MacOS/firefox -foreground 

veryv 55647 0.0 0.0 2442020 1788 s002 S+ 3:50AM 0:00.00 grep -i firefox 

To kill PID # 31241 (from second column), run: 

$ kill -15 pid 

$ kill -TERM 31241 

$ kill -15 31241 

If everything is failed try passing the kill signal: 

$ kill -9 31241 

Verify that firefox has gone with the ps command along with the grep command: $ ps aux | grep -i firefox 

You can replace the firefox command with cp command or any other command of your choice. More about CTRL-C 

When you press CTRL-C the current running command or process get Interrupt/kill (SIGINT) signal.  This signal means just terminate the process. Most commands/process will honor the SIGINT signal  but some may ignore it. You can press Ctrl-D to close the bash shell or open files when using cat  command. You can press Ctrl-Z to suspend the current foreground process running in bash shell. 

What happens if cp command is interrupted? 

If you interrupt the cp the already transferred files are copied from the source directory to the  destination directory and rest of the operation is aborted.

What happens if mv command is interrupted? 

From the thread

If you move a directory on the same file system you only move the directory entry from one location  in the file system to another one. E.g. mv /source/dir /target/dir will delete the directory entry of dir  from /source and create a new one in /target. That’s done by one atomic system call (i.e.  uninterruptable). The inode containing the directory entries of dir as well as the actual content of  the directory itself is not affected. 

If you move the directory from one file system to another the files are transferred one-by-one (as  Ignacio mentions in his answer), i.e. if you interrupt the mv the already transferred files are removed  from the source directory. 

Ending Process-PS & KILL in linux 

Linux and Unix-like operating system come with the kill command to terminate stalled or unwanted  processes without having to log out or restart the server. 

Hence, it is important to the stability of Linux systems. The kill command sends the designated signal  such as KILL process to the specified process or process groups. If no signal is specified, the TERM  signal is sent. Please note that kill command can be internal as part of modern shells built-in function  or externally located at /bin/kill. Usage and syntax remain similar regardless internal or external kill  command 

Before killing or terminating a process, you need to consider permissions

A root user can kill all processes. You can either add sudo before a command to run it as root, or  obtain a root shell with su. Then execute the command. 

Killing a process sends a termination message to the given process. There are multiple types of  termination messages including: 

SIGKILL – SIGKILL is the ultimate way of killing a process. It will always kill a process and will  kill the process abruptly, generating a fatal error. SIGKILL should always work. If it does not  work, the operating system has failed. 

SIGTERM – SIGTERM attempts to kill a process, but unlike SIGKILL it may be blocked or  otherwise handled. It can be considered a gentler way of attempting to terminate a process. 

For most purposes, SIGKILL will be the fastest and most effective method to terminate the process.

X window system 

The X Window System (X11) is an open source, cross platform, client-server computer software  system that provides a GUI in a distributed network environment. 

Used primarily on Unix variants, X versions are also available for other operating systems. Features  of the X window system include network transparency, the ability to link to different networks, and  customizable graphical capabilities. The X window system was first developed in 1984, as part of  project Athena, a collaboration between Stanford University and MIT. X.Org Foundation, an open  group, manages the development and standardization of the X window system. 

The X Window System is also known simply as X, X11 or X Windows. 

Techopedia Explains X Window System 

The client/server model in X system works in reverse to typical client/server model, where the client  runs on the local machine and asks for services from the server. In X system, the server runs on the  local machine and provides its display and services to the client programs. The client programs may  be local or remotely exist over different networks, but appear transparently. 

X is used in networks of interconnected mainframes, minicomputers, workstations, and X Terminals.  X window system consists of a number of interacting components, including: 

X server: Manages the display and input hardware. It captures command-based and graphics based inputs from input hardware and passes it to the client application that requested it. It  also receives inputs from the client applications and displays the output under guidance from  windows manager. The only component that interacts with hardware is X server. This makes  it easier to recode it as per the requirements of different hardware architectures. 

Windows manager: Is the client application that manages client windows. It controls the  general operations of the window system like geometry, appearance, coordinates, and  graphical properties of X display. Window manager can change the size and position of  windows on the display and reshuffle windows in a window stack. 

X client: Is an application program that communicates with X server using X protocol. Xterm,  Xclock, and Xcalc are examples of X clients. X manages its windows in a hierarchal structure.  The shaded area that fills the entire screen is the root window. X client application windows  are displayed on top of the root window and are often called the children of the root.

Configuration of X window 

During installation, the system's monitor, video card, and display settings are configured. To  change any of these settings after installation, use the X Configuration Tool. To start the X Configuration Tool, go to System (on the panel) => Administration => Display,  or type the command system-config-display at a shell prompt (for example, in an XTerm or  GNOME terminal). If the X Window System is not running, a small version of X is started to  run the program. 

After changing any of the settings, log out of the graphical desktop and log back in to enable  the changes. 

31.1. Display Settings 

The Settings tab allows users to change the resolution and color depth. The display of a  monitor consists of tiny dots called pixels. The number of pixels displayed at one time is  called the resolution. For example, the resolution 1024x768 means that 1024 horizontal  pixels and 768 vertical pixels are used. The higher the resolution values, the more images the  monitor can display at one time. 

The color depth of the display determines how many possible colors are displayed. A higher  color depth means more contrast between colors.

File system Ext2,Ext3 

In a computer, a file system is a way in which files are named and placed logically to store, retrieve,  and update the data and also used to manage space on the available devices. 

The file system is divided into two segments called User Data and Metadata. In this article, I am  trying to explore how to create and convert various Linux file systems and high-level differences  amongst Ext2, Ext3, and Ext4 file systems. 

Ext2 

Ext2 stands for second extended file system. 

It was introduced in 1993. Developed by Rémy Card. 

This was developed to overcome the limitation of the original ext file system. Ext2 does not have journaling feature. 

On flash drives, usb drives, ext2 is recommended, as it doesn’t need to do the over head of  journaling. 

Maximum individual file size can be from 16 GB to 2 TB 

Overall ext2 file system size can be from 2 TB to 32 TB 

Ext3 

Ext3 stands for third extended file system. 

It was introduced in 2001. Developed by Stephen Tweedie. 

Starting from Linux Kernel 2.4.15 ext3 was available. 

The main benefit of ext3 is that it allows journaling. 

Journaling has a dedicated area in the file system, where all the changes are tracked. When the  system crashes, the possibility of file system corruption is less because of journaling. Maximum individual file size can be from 16 GB to 2 TB 

Overall ext3 file system size can be from 2 TB to 32 TB 

There are three types of journaling available in ext3 file system. 

Journal – Metadata and content are saved in the journal. 

Ordered – Only metadata is saved in the journal. Metadata are journaled only after writing  the content to disk. This is the default. 

Writeback – Only metadata is saved in the journal. Metadata might be journaled either  before or after the content is written to the disk. 

You can convert a ext2 file system to ext3 file system directly (without backup/restore). Use the method we discussed earlier to identify whether you have ext2 or ext3 or ext4 file system. 

Creating an ext2, or ext3 

Once you’ve partitioned your hard disk using fdisk command, use mke2fs to create either ext2, ext3 Create an ext2 file system: 

mke2fs /dev/sda1



Create an ext3 file system:

mkfs.ext3 /dev/sda1 

(or) 

mke2fs –j /dev/sda1



No comments:

Post a Comment