Linux File Management and Shell Scripting: A Comprehensive Guide, Study notes of Computer Science

study notes of linux for second year student

Typology: Study notes

2021/2022

Available from 01/29/2022

babythangarasu
babythangarasu 🇮🇳

4.7

(3)

9 documents

1 / 80

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction to
Linux & Shell Script
Contents
Chapter 1: Introduction to Operating System
What isOperatingSystem? 1
MostPopularOS 2
Functions of anOperatingSystem 3
Introduction to LinuxOperatingSystem 4
Chapter 2: Files and Directories Management
FileSystem 12
Commands tohandleDirectories 15
File operationrelatedCommands 20
Chapter 3: File Creation and Managing Documents
TextEditors 26
vi EditorinLinux 27
File Locations 30
StandardFileTypes 31
OperationofRedirection 32
VariousFilters 33
Pipes 37
Chapter 4: Files Security
Accessing PermissionforFiles 40
File ViewAccessPermission 40
ChangingAccess Permission 41
Changing user andgroup ownership 46
Chapter 5: Shell Scripts for Automated Tasks
Scriptingin Shell 48
Variablesin Script 50
Types ofShellVariables 53
CommandSubstitution 57
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50

Partial preview of the text

Download Linux File Management and Shell Scripting: A Comprehensive Guide and more Study notes Computer Science in PDF only on Docsity!

Introduction to

Linux & Shell Script

Contents

Chapter 1: Introduction to Operating System What isOperatingSystem? 1 MostPopularOS 2 Functions of anOperatingSystem 3 Introduction to LinuxOperatingSystem 4 Chapter 2: Files and Directories Management FileSystem 12 Commands tohandleDirectories 15 File operationrelatedCommands 20 Chapter 3: File Creation and Managing Documents TextEditors 26 vi EditorinLinux 27 File Locations 30 StandardFileTypes 31 OperationofRedirection 32 VariousFilters 33 Pipes 37 Chapter 4: Files Security Accessing PermissionforFiles 40 File ViewAccessPermission 40 ChangingAccess Permission 41 Changing user andgroup ownership 46 Chapter 5: Shell Scripts for Automated Tasks Scriptingin Shell 48 Variablesin Script 50 Types ofShellVariables 53 CommandSubstitution 57

Chapter 6: Conditional and Iteration Statements Execution usingconditionsstatement 61 case …esacconstruct 65 Iteration statements inShell Script 66 The break andcontinuecommands 70 Simple Shell Script Programs Index

Introduction to Operating System 2 punched cards and submit to computer operator. This system has drawbacks such as lack of communication between user and system, CPU time may be idle, etc.

2. Time Sharing OperatingSystem In time sharing environment multitasking can be achieved because of ability of the Processor to handle multiple jobs at a time. Multiple jobs are carried out by the CPU by switching between the jobs, which make user to receive an immediate response. 3. Distributed OperatingSystem In distributed systems, it uses multiple processors for handling numerous real- time applications and many users at a time. Data processing jobs are shared among the processors accordingly. 4. Network OperatingSystem Network OS is used in the client / server environment in which request and response happening over the remote systems. Few advantages of Network OS are a Centralized server which makes storing and accessing data in convenient manner, security in server is more than normal system, upgradation to new technologies and hardware can be easily included into the system and etc. 5. Real Time OperatingSystem A Real Time Operating System is software that quickly switches between jobs, giving the feeling that multiple programs are being performed at the same time on a single processing core.

Most PopularOS

Most popularly used OS are Windows, Linux, Mac OS and etc. Windows OS is owned by Microsoft. Various types of Windows are available for various platforms. Linux is open source software with variant versions. Linux is mostly used in server, Internet, IoT and etc. It is a OS which is much convenient for networking environment. Difference between Linux and Windows are:  Windows OS is licensed commercial version whereas Linux is open source operatingsystem.  Windows cannot be modified based on users need whereas Linux has access to source code and code can be altered as per userneed.  Windows are slow on older hardware whereas Linux will run faster than windows.  Windows gather the user information’s which has privacy concern. In Linux distributions no user data iscollected.  Regarding killing(closing) application if they hung is much hard in Windows when compared Linux.  Wide variety free software’s are supported by Linux when compared with Windows.  Security is much more in Linux when compared withWindows.

Introduction to Linux & Shell Script 3

Functions of an OperatingSystem

Command Interpretation CPU can do the tasks such as copy a file, delete a file, rename a file, etc., which are given by the user. The commands given by users have to be interpreted to binary code which is made up of zeros and ones. CPU can use the code and make the operations. Translation of user’s commands into binary code is done by OS which helps CPU to interpret thecommand. Process Management A process is defined as a program in execution. Random access memory (RAM) is main memory where program resides for execution. When developer executes a program, the OS gets the program to the memory, CPU starts the execution of instruction one by one at a time. In a multiprogramming atmosphere, a lot of processes run on the computer simultaneously. To enable more than a few processes to be executed at the same time, the OS splits the CPU time. The OS assigns time slots to the different processes in the memory which makes process to be executed concurrently. Memory Management The OS assigns the available memory to the processes. Because main memory is limited, it is not possible to get all the processes loaded into the memory at a given time. Memory executes the current running process, where as remaining process are stored in HDD (Hard Disk Drive) that are not executed at that time. HDD is used as secondary storage device. Processes are moved to main memory when it is needed to be executed. OS performs the operation of swapping processes between HDD and Main memory Input / Output (I/O) Operations and peripheral Management When applications are executed that are available in main memory, may need to accept input and generate output of the operation. On its own applications cannot do the I/O operations. Here comes the role of OS which performs the operation of handling the I/O devices. Operating System has the controlling mechanism over the peripherals devices that are connected to System. Communication between the peripherals and CPU is managed by OS. File Management File management is another main operation of an OS. The OS allows user to do file operations, such as creating, naming, opening, reading, and closing a file. Types of Systems

1. Single UserSystems Single user system is environment in which system can be operated by single user at a time. An example of a single user system is a Personal Computer (PC). It is a general purpose computer that can run programs to carry out a sample tasks.

Introduction to Linux & Shell Script 5 There are quite a few distributors of Linux. All the distributors use the Linux kernel. Some of the distributors are: Distributors Name Web Site Red Hat http://www.redhat.com Caldera http://www.caldera.com Mandrake http://www.linux- mandrake.com Debian http://www.debian.com SuSE http://www.suse.com Slackware http://www.slackware.com History of Linux OS Linux is an operating system is available for personal computers from 1991. At beginning, Linux can be operated only in Intel 8086 processor. Over the years many versions of Linux were distributed with support of running all the processors. At present, Linux is one of OS that running on a wide range of processors such as Intel, AMD, Motorola, SPARC and IBM. Linux is alike to UNIX which has many concepts from UNIX and equipped with UNIX API. Linux is without a doubt the mostly growing operating system. It is used in various areas right form embedded systems to mainframe. One of the exciting and most significant specifications about Linux is that it is open-sourced. The Linux kernel is licensed under the GNU General Public License (GPL); the kernel source code is freely available and can be modified to suit the needs of user machine. Users can make suggestion for alterations to the kernel code. When a new version of Linux is introduced, user can work on the new version to fix bugs, if any. To keep constancy, Linus Torvalds ensures quality control and then combines all the new code into the kernel. This is a main cause for the accomplishment of Linux. Linux has an official mascot – the Linux penguin – Tux. Linux has free software, such as text editors, browsers, and programming tools. Architecture of Linux The Linux Penguin – Tux The GNU/Linux OS made-up of a kernel, a shell, utilities, and application programs. The Linux architecture is shown below:

Introduction to Operating 6 System Hardware Kernel Shell Utilities Application Programs Kernel The OS program kernel is core element of Linux system. The kernel manages the resource of a system, allocating them to various users and tasks. It communicates directly with hardware device, which makes way for easy program creation and moving them across different hardware platform. Users do not interact with a kernel directly. Actually, the logon process starts a new interactive program called the shell for each user. Shell Linux has a simple interface called the shell. The shell provides services for a user. A shell can be used by user to interact with the system. The users don’t want to have an idea about how interaction happens with hardware. Some of the common shells in Linux are bash, sh, tcsh, csh, and ksh. Utilities and Application programs of Linux Various utilities and commands collections are available in Linux which can be used service processing. Utilities and commands can be executed with shell. Software from independent venders such as Application programs, Database Management, Word Processors, etc can be installed and used in Linux. Difference between UNIX and Linux With UNIX as a reference model new OS Linux was developed. Fundamental structure and concepts of Linux and UNIX is identical to each other. Linux is can be treated as a new version of UNIX. The main difference between Linux and UNIX is that Linux is open source which provides Linux as free of cost. Linux has some many development tools, such as C, C++, and scripting languages such as Perl, Python and etc. Features Linux UNIX Shells Bash, pdksh, tcsh, zsh, ash Bourne, Korn, C Vendors Red Hat, Caldera, Debian, LinuxPPC, SUSE AT & T, MULTITCS, BSD, SCO, HF, Ux, IRIX, Ultrix, XENIX, Sun Solaris Licensing Free of Cost Licensed Comparison of Linux and UNIX

Introduction to Operating 8 System Shells available in Linux

1. BourneShell The Bourne shell was developed at AT & T by Stephen Bourne. This shell is circulated with all UNIX systems and is resides in the /bin directory. The executable file name for Bourne shell is sh. 2. C Shell The C shell was developed by William Joy at the University of California at Berkeley. It is similar to the C programming language. The C shell derives its name from its programming language, which resembles the C programming language in syntax. The executable file name for the C shell is csh. 3. KornShell In the AT & T Laboratory Korn shell was developed by David Korn. The Korn shell combines the features of both the Bourne and C shells. The executable file name for the Korn shell is ksh. 4. RestrictedShell Restricted shell can be used for limited access on the OS to an usr. The restricted shell typically used for guest users who only need limited rights and permissions. 5. BashShell The Bash shell is an enhancement on the Bourne shell, hence the name Bash, which is an acronym for Bourne Again Shell. Bash is product of Free Software Foundation’s GNU project. The Bash shell is the default shell for most Linux systems and is stored in the /bin directory. The Bash shell stores all of the commands that user use in a session. In addition, it stores the commands that user used in previous session. In Red Hat Linux, the sh command is a symbolic link to bash. 6. TcshShell Tcsh stands for Tom’s C Shell and is an enhancement of the C shell. The Tcsh shell is also known as TC shell. In Linux, the csh command is a symbolic link to the Tcsh shell. User can execute the Tcsh shell by typing either csh or tcsh at the command prompt. 7. AShell The A shell was developed by Kenneth Almquist of the University of Berkeley. It emulates the Bourne shell. The A shell is suitable for computers that have limited memory. The executable file name for the A shell is ash. 8. ZShell The Z shell offers the features of Tcsh and Korn shells. In addition, it provides a large number of utilities and extensive documentation. The executable file name for Z shell is zsh.

Introduction to Linux & Shell 9 Script Beginning a Linux: Logging on Telnet program can be used by User to connect to a system which is in Windows 95/98/NT OS’s to the system running the Linux OS. User can start telnet program by typing the following command at prompt. telnet For example, if user is working on a system running in Windows 95 OS and the TCP/IP address to the Linux server is 192.198.0.1, user would need to give the following command to connect to the Linux Server. telnet 192.168.0. When a connection is established between the Linux and Windows, a message similar to the one shown below would appear on the screen: Red Hat Linux release 7.1 (Seawolf) Kernel 2.4.2.2 on an 1686 Login: Each user has an identification called the user name or the logon name, which the user needs to enter when the login: prompt appears on the screen The steps to log on to a Linux server are: At the login: prompt, enter your logon name and password. Information about users, such as user names and passwords, is stored in the shadow and passwd files in the / etc directory. Users enter their logon name and password at command prompt. Given information’s are verified with shadow and passwd files. If the login : information provided by the user does not match the information in the shadow and passwd files, an error message: "Login incorrect" is displayed. Only authorized users can make logon process which makes system more secure. When a valid user name is entered, the shell prompt is displayed on the screen. The shell prompt is givenbelow: [user name@localhostcurrent_directory_name]$ At the shell prompt, user name is the logon name of the user, and current_directory_name is the current working directory of theuser. When user comes, a new logon account has to be created. Administrator creates a new login and assigns a home directory to the user. The home directory is the default directory for a user when the user logs on. In Linux, logon names are usually the names of users, and their home directory usually has the same name. For instance, if user name is tom and home directory name is also tom, then after logging on. The entire logon process appears just as the one shown below: login: tom Password: Last login: Sat Sep 18 12:18:02 [tom 172.17.55.167 tom]$ _ Security for Users:Passwords In Linux, the logon process makes sure that only approved users can access the system that runs on Linux. As an added advantage a security measure in Linux allows user to specify a password associated with user logon name. To log on, user need to enter

Introduction to Linux & Shell 11 Script Lima Documentation Project (LDP). The syntax to access help using man pages is as follows: Syntax man For example, the command to obtain information about the various options of thepasswd commandis: $ man passwd; Ending a Linux Session: Logging Off After you log on to Linux, the work session continues until you instruct the shell to terminate the session. Typing exit or logout at the command prompt end's the current Linux session. User can also press +d to end the Linux session. In order to keep the files secure, user must end thier Linux session. Exercises Fill in the Blanks

  1. Linux was developedby
  2. UNIX is licensed, Linuxis
  3. The executable file name of Cshell
  4. User password can bechanged with command
  5. is program used to connect Linux from Windowsenvironment.
  6. is command which provides information related to Linuxcommands.
  7. Shortcut key to close a Linux sessionis Answer in details
  8. Write detail on introduction to OperatingSystems.
  9. Explain the evolution ofLinux.
  10. Discuss the functions of OperatingSystems.
  11. Describe the developments and features of Linux OS.
  12. Explain the process of logon and logout in Linux with example.
  13. What is role of root user?Explain.
  14. How you will change password in Linux? GiveExample.

Files and Directories 12 Management

FileSystem

The Linux File System

Chapter 2

Files and Directories Management

In a secondary storage data are stored in the structure of files. OS has to provide a method to handle the thousand of files in organized manner. A file system can defined as a method of storing and managing files and directories in the storage device. File system of Linux is little different from other Operating Systems. It follows the hierarchical file structure which makes the users to store and access the files in the directories. Directories are much alike a drawers of a cabinet. Drawer used to contain files, documents. As we do in drawer files and directories can be created on a disk to store files. In a cabinet, a user specifies names for the drawer labels and contents. Similarly, in a Linux file system, a user can give a directory name where collection of files to be placed in the directory. Files are placed in the relevant directories based on its data. Linux OS admin is responsible for creating home directory for users. For example, Arun is home directFoirley for the user named Arun. In this directory he can store all his files or can create directories in his home directory. The directory Arun haveng two files, namely prog1 and prog2, two directories named as Desktop and Templates. In the Desktop directory it has two files, Default and start. In Linux, root (/) directory is main directory of entire file system. It is divided into bin, boot, home, usr, etc. Each of these directories is organized to store specific types of files.  The /bin directory is used to store Linux utilities. These utilities are the commands in the Linux. It is named bin because, utilities of Linux are stored in the binaryformat.  Device related files stored in /dev directory.  The /etc directory stores the data related to the operating system, including the OS programs and configuration files. Commands such as passed file are placed in this directory which can be used byusers.  Data 1ibraries for the compilers are placed in /lib directory.  The /home directory contains the home directories of all theusers.  The /usr directory stores the operating system files that are not required during the startupprocess.  The /var directory contains information specific to different utilities available in Linux. In a Hierarchy file system, files are retrieved using its path which has the details such as file name, preceded by directory name which contains the file. The file name and different directory names in the path are separated by ‘/' symbol.

Files and Directories 14 Management Ordinary Directory Special Files Character DeviceBlock Device Hard Link Symbolic link  Character devicefiles  Block devicefiles  Hardlinks  Symboliclinks Character Device Files To a read or write a character at time through a device, a character device files is used. One of the examples for character device file is modem. It is also known as sequentially accessed devices. Block Device Files A block of data can be accessed as one block using Block device files. A block data is made up of either 512 or 1,024 bytes. Kernel reads or writes data in blocks. Minimum one block is handled at a time. Data are collected and stored in the memory for user needed. This process is carried by the kernel. It allows the random access method, which makes I/O in faster and efficient manner. HDD is device is primary example for Block device operations. I/O devices may be either character device or block device depending on commands used by the device. Hard Links Hard links are special files that allow a single file to have multiple names. User can specify a hard link only for a file and not for a directory. User can specify hard links for files only when they are on the same file system. Symbolic Links Symbolic links are also known as soft links. Symbolic links are similar to hard links, but user can specify symbolic links for files across different file systems. Types of Files in Linux Types of Users in Linux SystemAdministrator A System Administrator (SA) is administrator who handles the various operations of OS environment such as creating user, granting and revoking, restricting rights, handling the files, directories and etc. SA is authorized person for handling the entireOSwhichstartswithsystemconsoleonwhichOSisinstalled.Admincreates

Introduction to Linux & Shell 15 Script lib / Arun User2 User bin dev home etc user d a t a 1 prog d a t a 1 users, group, group policies and etc., The SA makes backups to prevent data loss if the operating system crashes. In Linux, the SA is also known as the root user. The SA has all the rights for the Linux operating system. File Owner File owner of the user created are concern user. They can do any operation on that file, such as copying, deleting, orediting. Group Owner In a project development a team of users has to develop the modules which are confidential. In this case, the developed programs have to be stored in home directory of team leader. In this case, programmers are the file owners of their own program files. A programmer might need to link a program with another program to test the program. Therefore, each program also belongs to other programmers. SA needs a group to handle the programs in collectively manner. A group can be created with one of the member as group owner to handle these programs all together. In Linux, you can define the users who belong to a group. In addition, similar to a user name, you can specify a name for the group ofusers. Other Users All the users of the Linux operating system who are not members of the group are known as other users for the files of that group. Other users do not belong to the particular group.

Commands to handleDirectories

User can type the commands at the Linux prompt. In Linux, user needs to type all commands in lowercase characters. For each of the commands the file hierarchy will beused. A Sample Linux File Hierarchy

Introduction to Linux & Shell 17 Script /usr/bin [Suresh@localhost bin]$ cd [Suresh@loealhost Suresh] $ pwd /home/Suresh [Suresh@loealhost Suresh] $ _ The tilde symbol (~) is used to denote the complete path of home in Linux. Let us assume there are two directories baseball and basketball in Suresh s homedirectory. Example [Suresh@localhost etc] $ pwd /etc [Suresh@localhost etc]$ cd ~/baseball [Suresh@localhost baseball] $ pwd /home/Suresh/baseball [Suresh@localhost baseball] $ cd ~ [Suresh@localhost Suresh] $ pwd /home/Suresh [Suresh@localhost Suresh] $ ___ To switch from the directory datal to directory data2, Suresh needs to type the following cd command: Example [Suresh@localhost datal] $ pwd /home/Suresh/datal [Suresh@localhost datal] $ cd ../data [Suresh@localhost data2] $ pwd /home/Suresh/data [Suresh@localhost data2] $ __ Creating a Directory_ User can use of mkdir (make directory) command to create directories. For example [Suresh@localhost Suresh] $ mkdir pro-file [Suresh@localhost Suresh] $_ When user types wants to create directory or sub-directory mkdir command can be used. Above example shows a subdirectory pro-file is created in the current directory. User can also specify the complete path with the mkdir command. Example [Suresh@localhost Suresh]$ mkdir /temp/pro-file [Suresh@localhost Suresh]$_ In the above example, the directory pro-file is created in the / temp directory. Removing a Directory User can use the rmdir (remove directory) command to remove a specified directory. Example [Suresh@localhost Suresh]$ rmdir pro-file [Suresh@localhost Suresh]$_

Files and Directories 18 Management Here, the pro-file directory is deleted. User can also specify the complete path with the rmdir command. Example [Suresh@localhost Suresh]$ rmdir /home/Suresh/tennis [Suresh@localhost Suresh]$ _ The above command removes the tennis directory, which is located in Suresh's home directory. Listing the Contents of a Directory To list the names of files and subdirectories that stored in a directory can be done with ls command. Example [Suresh@localhost Suresh]$ ls /home/Suresh DEADJOE X baseball comm tennis Desktop a.out basketball program.cc [Suresh@localhost Suresh]$ In the above example, all the files and directories in the Suresh directory are listed. To display the files and directories in the current directory, user need not specify the directory name with the ls command. In the above output, only the file names and not the file types are displayed. User should use the -1 option with the ls command to display a detailed list of files and directories. Example [Suresh@localhost Suresh]$ ls - total 22 drwxr-xr-x 5 Suresh Suresh 1024 Sep 20 13:00Desktop -rw-rw-r- 1 Suresh Suresh 5 Sep 21 11:47XYZ -rwxrwxr- 1 Suresh Suresh 1290 Sep 20 16:13prg1.out drwxrwxr-x 2 Suresh Suresh 1024 Sep 21 00:11Bball drwxrwxr-x 2 Suresh Suresh 1024 Sep 21 00:12Basket drwxrwxr-x 2 Suresh Suresh 1024 Sep 21 00:12Com.cc -rw-rw-r- 1 Suresh Suresh 10Sep2114:45program.c drwxrwxr-x 2 Suresh Suresh 1024 Sep 21 12:26Cricket 1 2 3 4 5 6 7 8 9 The symbols r, w, and x in the first column specify the read, write, and execute permissions of a file or directory. The outcome of the 1s -1 is enlighten in the below table: Column # Description 1 File type and File Access Permissions (FAPs) 2 Number of links 3 File owner 4 Group owner (group name) 5 File size (in bytes) 6, 7 and 8 Day and time of the last modification to the file 9 Name of the file A group is a collection of users or groups who share common attributes. In Linux, there are three kinds of users: file owners, group owners, and other users. The file and group owners are included in the detailed list for a file. The first character of column number 1