You are on page 1of 10

Linux

Monday, May 30, 2022 6:08 PM

1- Linux concepts :
Some Linux basics we have to know :
1- Open your terminal
2- If you want to know the place you are in : pwd
3- If you want to go inside a file : cd ~/Desktop/ (this ~ refers to the whole URL for example : user/NadaHabba/Desktop ---> so cd
~/Desktop/ --> ~ means whole the URL before desktop
4- To create a new folder --> mkdir directoryName
5- To know about the filles inside directory : ls
6- If I want to print something inside a file : echo "Hello world" > test.txt
7- If I want to print what inside a file : cat FileName
8- To copy a file into another file : cp test.txt test2.txt
9- If I want to know detail about the files inside the directory : ls --ltr
10- To remove a file and check firstly if its exist or not : rm -rf test.txt
11- To move a file from directory to another : mv test.txt temp\test.txt
12- Each (..) Means that up by one folder

Resources:

=====================================================

Components of Linux System


Linux Operating System has primarily three components
• Kernel − Kernel is the core part of Linux. It is responsible for all major activities of this operating system. It consists of
various modules and it interacts directly with the underlying hardware. Kernel provides the required abstraction to hide
low level hardware details to system or application programs.
• System Library − System libraries are special functions or programs using which application programs or system
utilities accesses Kernel's features. These libraries implement most of the functionalities of the operating system and do
not requires kernel module's code access rights.
• System Utility − System Utility programs are responsible to do specialized, individual level tasks

Kernel Mode vs User Mode


Kernel component code executes in a special privileged mode called kernel mode with full access to all resources
of the computer. This code represents a single process, executes in single address space and do not require any
context switch and hence is very efficient and fast. Kernel runs each processes and provides system services to
processes, provides protected access to hardware to processes.
Support code which is not required to run in kernel mode is in System Library. User programs and other system
programs works in User Mode which has no access to system hardware and kernel code. User programs/ utilities
use System libraries to access Kernel functions to get system's low level tasks.

Operating systems types:


Batch operating system
The users of a batch operating system do not interact with the computer directly. Each user prepares his job on an
off-line device like punch cards and submits it to the computer operator. To speed up processing, jobs with similar
needs are batched together and run as a group. The programmers leave their programs with the operator and the
operator then sorts the programs with similar requirements into batches.
The problems with Batch Systems are as follows −
Lack of interaction between the user and the job.

1 CS Fundmentals Page 1
• Lack of interaction between the user and the job.
• CPU is often idle, because the speed of the mechanical I/O devices is slower than the CPU.
• Difficult to provide the desired priority.
Time-sharing operating systems
Time-sharing is a technique which enables many people, located at various terminals, to use a particular computer
system at the same time. Time-sharing or multitasking is a logical extension of multiprogramming. Processor's time
which is shared among multiple users simultaneously is termed as time-sharing.
The main difference between Multiprogrammed Batch Systems and Time-Sharing Systems is that in case of
Multiprogrammed batch systems, the objective is to maximize processor use, whereas in Time-Sharing Systems, the
objective is to minimize response time.
Multiple jobs are executed by the CPU by switching between them, but the switches occur so frequently. Thus, the
user can receive an immediate response. For example, in a transaction processing, the processor executes each
user program in a short burst or quantum of computation. That is, if n users are present, then each user can get a
time quantum. When the user submits the command, the response time is in few seconds at most.
The operating system uses CPU scheduling and multiprogramming to provide each user with a small portion of a
time. Computer systems that were designed primarily as batch systems have been modified to time-sharing
systems.
Advantages of Timesharing operating systems are as follows −
• Provides the advantage of quick response.
• Avoids duplication of software.
• Reduces CPU idle time.
Disadvantages of Time-sharing operating systems are as follows −
• Problem of reliability.
• Question of security and integrity of user programs and data.
• Problem of data communication.

Distributed operating System


Distributed systems use multiple central processors to serve multiple real-time applications and multiple users. Data
processing jobs are distributed among the processors accordingly.
The processors communicate with one another through various communication lines (such as high-speed buses or
telephone lines). These are referred as loosely coupled systems or distributed systems. Processors in a
distributed system may vary in size and function. These processors are referred as sites, nodes, computers, and so
on.
The advantages of distributed systems are as follows −
• With resource sharing facility, a user at one site may be able to use the resources available at another.
• Speedup the exchange of data with one another via electronic mail.
• If one site fails in a distributed system, the remaining sites can potentially continue operating.
• Better service to the customers.
• Reduction of the load on the host computer.
• Reduction of delays in data processing.
Network operating System
A Network Operating System runs on a server and provides the server the capability to manage data, users, groups,
security, applications, and other networking functions. The primary purpose of the network operating system is to
allow shared file and printer access among multiple computers in a network, typically a local area network (LAN), a
private network or to other networks.
Examples of network operating systems include Microsoft Windows Server 2003, Microsoft Windows Server 2008,
UNIX, Linux, Mac OS X, Novell NetWare, and BSD.
The advantages of network operating systems are as follows −
• Centralized servers are highly stable.
• Security is server managed.
• Upgrades to new technologies and hardware can be easily integrated into the system.
• Remote access to servers is possible from different locations and types of systems.
The disadvantages of network operating systems are as follows −

1 CS Fundmentals Page 2
The disadvantages of network operating systems are as follows −
• High cost of buying and running a server.
• Dependency on a central location for most operations.
• Regular maintenance and updates are required.
Real Time operating System
A real-time system is defined as a data processing system in which the time interval required to process and
respond to inputs is so small that it controls the environment. The time taken by the system to respond to an input
and display of required updated information is termed as the response time. So in this method, the response time is
very less as compared to online processing.
Real-time systems are used when there are rigid time requirements on the operation of a processor or the flow of
data and real-time systems can be used as a control device in a dedicated application. A real-time operating system
must have well-defined, fixed time constraints, otherwise the system will fail. For example, Scientific experiments,
medical imaging systems, industrial control systems, weapon systems, robots, air traffic control systems, etc.
There are two types of real-time operating systems.
Hard real-time systems
Hard real-time systems guarantee that critical tasks complete on time. In hard real-time systems, secondary storage
is limited or missing and the data is stored in ROM. In these systems, virtual memory is almost never found.
Soft real-time systems
Soft real-time systems are less restrictive. A critical real-time task gets priority over other tasks and retains the
priority until it completes. Soft real-time systems have limited utility than hard real-time systems. For example,
multimedia, virtual reality, Advanced Scientific Projects like undersea exploration and planetary rovers, etc.

2- Shell scripting

A shell script is a computer program designed to be run by the Unix/Linux shell which could be one of the following:
• The Bourne Shell
• The C Shell
• The Korn Shell
• The GNU Bourne-Again Shell
A shell is a command-line interpreter and typical operations performed by shell scripts include file manipulation,
program execution, and printing text.

The shell is, after all, a real programming language, complete with variables, control structures, and so forth. No matter how complicated
a script gets, it is still just a list of commands executed sequentially.

The following script uses the read command which takes the input from the keyboard and assigns it as the value of
the variable PERSON and finally prints it on STDOUT.
#!/bin/sh
# Author : Zara Ali
# Copyright (c) Tutorialspoint.com
# Script follows here:
echo "What is your name?"
read PERSON
echo "Hello, $PERSON"

The prompt, $, which is called the command prompt, is issued by the shell. While the prompt is displayed, you can
type a command.

1 CS Fundmentals Page 3
type a command.
Shell reads your input after you press Enter. It determines the command you want executed by looking at the first
word of your input. A word is an unbroken set of characters. Spaces and tabs separate words.
Following is a simple example of the date command, which displays the current date and time −
$date
Thu Jun 25 08:30:19 MST 2009
You can customize your command prompt using the environment variable

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.

The basic concept of a shell script is a list of commands, which are listed in the order of execution. A good shell
script will have comments, preceded by # sign, describing the steps.
There are conditional tests, such as value A is greater than value B, loops allowing us to go through massive
amounts of data, files to read and store data, and variables to read and store data, and the script may include
functions.

Before you add anything else to your script, you need to alert the system that a shell script is being started. This is
done using the shebang construct. For example −
#!/bin/sh
This tells the system that the commands that follow are to be executed by the Bourne shell. It's called a shebang
because the # symbol is called a hash, and the ! symbol is called a bang.
To create a script containing these commands, you put the shebang line first and then add the commands −
#!/bin/bash
pwd
ls
Shell Comments
You can put your comments in your script as follows −
#!/bin/bash
# Author : Zara Ali
# Copyright (c) Tutorialspoint.com
# Script follows here:
pwd
ls

Save the above content and make the script executable −


$chmod +x test.sh
The shell script is now ready to be executed −
$./test.sh

#!/bin/sh
NAME="Zara Ali"
echo $NAME

Shell provides a way to mark variables as read-only by using the read-only command. After a variable is marked
read-only, its value cannot be changed.
For example, the following script generates an error while trying to change the value of NAME −
Live Demo
#!/bin/sh
NAME="Zara Ali"

1 CS Fundmentals Page 4
NAME="Zara Ali"
readonly NAME
NAME="Qadiri"

Unsetting or deleting a variable directs the shell to remove the variable from the list of variables that it tracks. Once
you unset a variable, you cannot access the stored value in the variable.
Following is the syntax to unset a defined variable using the unset command −
unset variable_name
The above command unsets the value of a defined variable. Here is a simple example that demonstrates how the
command works −
#!/bin/sh
NAME="Zara Ali"
unset NAME
echo $NAME
The above example does not print anything. You cannot use the unset command to unset variables that are
marked readonly.

Variable Types
When a shell is running, three main types of variables are present −
• Local Variables − A local variable is a variable that is present within the current instance of the shell. It is not available
to programs that are started by the shell. They are set at the command prompt.
• Environment Variables − An environment variable is available to any child process of the shell. Some programs need
environment variables in order to function correctly. Usually, a shell script defines only those environment variables that
are needed by the programs that it runs.
• Shell Variables − A shell variable is a special variable that is set by the shell and is required by the shell in order to
function correctly. Some of these variables are environment variables whereas others are local variables.

==========================

How can I move up a directory?


The parent of a directory is the directory above it. For example, /home is the parent of /home/repl, and /home/repl is
the parent of /home/repl/seasonal. You can always give the absolute path of your parent directory to commands
like cd and ls. More often, though, you will take advantage of the fact that the special path .. (two dots with no
spaces) means "the directory above the one I'm currently in". If you are in /home/repl/seasonal, then cd .. moves you
up to /home/repl. If you use cd .. once again, it puts you in /home. One more cd .. puts you in the root directory /,
which is the very top of the filesystem. (Remember to put a space between cd and .. - it is a command and a path,
not a single four-letter command.)

A single dot on its own, ., always means "the current directory", so ls on its own and ls . do the same thing, while cd . has no effect
(because it moves you into the directory you're currently in).

One final special path is ~ (the tilde character), which means "your home directory", such as /home/repl. No matter where you are, ls
~ will always list the contents of your home directory, and cd ~ will always take you home.

How can I copy files?


You will often want to copy files, move them into other directories to organize them, or rename them. One command
to do this is cp, which is short for "copy". If original.txt is an existing file, then:
cp original.txt duplicate.txt
creates a copy of original.txt called duplicate.txt. If there already was a file called duplicate.txt, it is overwritten. If the
last parameter to cp is an existing directory, then a command like:
cp seasonal/autumn.csv seasonal/winter.csv backup
copies the 2 files (Autumn and winter ) into that directory backup.

1 CS Fundmentals Page 5
copies the 2 files (Autumn and winter ) into that directory backup.

Copy spring.csv and summer.csv from the seasonal directory into the backup directory without changing your current working
directory (/home/repl).
Solution

cp seasonal/spring.csv seasonal/summer.csv backup

How can I move a file?


While cp copies a file, mv moves it from one directory to another, just as if you had dragged it in a graphical file
browser. It handles its parameters the same way as cp, so the command:
mv autumn.csv winter.csv ..

mv can also be used to rename files. If you run:


mv course.txt old-course.txt
then the file course.txt in the current working directory is "moved" to the file old-course.txt. This is different from the
way file browsers work, but is often handy.
One warning: just like cp, mv will overwrite existing files. If, for example, you already have a file called old-course.txt,
then the command shown above will replace it with whatever is in course.txt.

How can I delete files?


We can copy files and move them around; to delete them, we use rm, which stands for "remove". As with cp and mv,
you can give rm the names of as many files as you'd like, so:
rm thesis.txt backup/thesis-2017-08.txt
removes both thesis.txt and backup/thesis-2017-08.txt

How can I create and delete directories?


mv treats directories the same way it treats files: if you are in your home directory and run mv seasonal by-season,
for example, mv changes the name of the seasonal directory to by-season. However, rm works differently.
If you try to rm a directory, the shell prints an error message telling you it can't do that, primarily to stop you from
accidentally deleting an entire directory full of work. Instead, you can use a separate command called rmdir. For
added safety, it only works when the directory is empty, so you must delete the files in a directory before you delete
the directory. (Experienced users can use the -r option to rm to get the same effect; we will discuss command
options in the next chapter.)

Since a directory is not a file, you must use the command mkdir directory_name to create a new (empty) directory.

How can I view a file's contents?


Before you rename or delete files, you may want to have a look at their contents. The simplest way to do this is
with cat, which just prints the contents of files onto the screen.
cat agarwal.txt

How can I view a file's contents piece by piece?


You can use cat to print large files and then scroll through the output, but it is usually more
convenient to page the output. The original command for doing this was called more, but it has
been superseded by a more powerful command called less. (This kind of naming is what passes for
humor in the Unix world.) When you less a file, one page is displayed at a time; you can press
spacebar to page down or type q to quit.
1 CS Fundmentals Page 6
spacebar to page down or type q to quit.
If you give less the names of several files, you can type :n (colon and a lower-case 'n') to move to
the next file, :p to go back to the previous one, or :q to quit.

Use less seasonal/spring.csv seasonal/summer.csv to view those two files in that order.
Press spacebar to page down, :n to go to the second file, and :q to quit.

How can I look at the start of a file?


The first thing most data scientists do when given a new dataset to analyze is figure out what fields it contains and
what values those fields have. If the dataset has been exported from a database or spreadsheet, it will often be
stored as comma-separated values (CSV). A quick way to figure out what it contains is to look at the first few rows.
We can do this in the shell using a command called head. As its name suggests, it prints the first few lines of a file
(where "a few" means 10), so the command:
head seasonal/summer.csv

You won't always want to look at the first 10 lines of a file, so the shell lets you change head's behavior by giving it
a command-line flag (or just "flag" for short). If you run the command:
head -n 3 seasonal/summer.csv
head will only display the first three lines of the file

How can I list everything below a directory?


In order to see everything underneath a directory, no matter how deeply nested it is, you can
give ls the flag -R (which means "recursive"). If you use ls -R in your home directory, you will see
something like this:
backup course.txt people seasonal
./backup:
./people:
agarwal.txt
./seasonal:
autumn.csv spring.csv summer.csv winter.csv
This shows every file and directory in the current level, then everything in each sub-directory, and
so on.

From <https://campus.datacamp.com/courses/introduction-to-shell/manipulating-data?ex=6>

To help you know what is what, ls has another flag -F that prints a / after the name of every directory and
a * after the name of every runnable program. Run ls with the two flags, -R and -F, and the absolute path to
your home directory to see everything it contains. (The order of the flags doesn't matter, but the directory
name must come last.)

How can I get help for a command?


To find out what commands do, people used to use the man command (short for "manual"). For example, the
command man head brings up this information:

The one-line description under NAME tells you briefly what the command does, and the summary
under SYNOPSIS lists all the flags it understands. Anything that is optional is shown in square brackets [...], either/or

1 CS Fundmentals Page 7
under SYNOPSIS lists all the flags it understands. Anything that is optional is shown in square brackets [...], either/or
alternatives are separated by |, and things that can be repeated are shown by ..., so head's manual page is telling
you that you can either give a line count with -n or a byte count with -c, and that you can give it any number of
filenames.
The problem with the Unix manual is that you have to know what you're looking for. If you don't, you can
search Stack Overflow, ask a question on DataCamp's Slack channels, or look at the SEE ALSO sections of the
commands you already know.

• Use tail with the flag -n +7 to display all but the first six lines of seasonal/spring.csv.

How can I select columns from a file?


head and tail let you select rows from a text file. If you want to select columns, you can use the command cut. It has
several options (use man cut to explore them), but the most common is something like:
cut -f 2-5,8 -d , values.csv
which means "select columns 2 through 5 and columns 8, using comma as the separator". cut uses -f (meaning
"fields") to specify columns and -d (meaning "delimiter") to specify the separator. You need to specify the latter
because some files may use spaces, tabs, or colons to separate columns.

From <https://campus.datacamp.com/courses/introduction-to-shell/manipulating-data?ex=8>

What can't cut do?


cut is a simple-minded command. In particular, it doesn't understand quoted strings. If, for example,
your file is:
Name,Age
"Johel,Ranjit",28
"Sharma,Rupinder",26
then:
cut -f 2 -d , everyone.csv
will produce:
Age
Ranjit"
Rupinder"
rather than everyone's age, because it will think the comma between last and first names is a
column separator.

Even better, history will print a list of commands you have run recently.
Re-run head again using ! followed by a command number.

How can I select lines containing specific values?


head and tail select rows, cut selects columns, and grep selects lines according to what they
contain. In its simplest form, grep takes a piece of text followed by one or more filenames and prints
all of the lines in those files that contain that text. For example, grep bicuspid
seasonal/winter.csv prints lines from winter.csv that contain "bicuspid".

grep can search for patterns as well; we will explore those in the next course. What's more
important right now is some of grep's more common flags:
• -c: print a count of matching lines rather than the lines themselves
• -h: do not print the names of files when searching multiple files
• -i: ignore case (e.g., treat "Regression" and "regression" as matches)
1 CS Fundmentals Page 8
• -i: ignore case (e.g., treat "Regression" and "regression" as matches)
• -l: print the names of files that contain matches, not the matches
• -n: print line numbers for matching lines
• -v: invert the match, i.e., only show lines that don't match

How can I store a command's output in a file?


All of the tools you have seen so far let you name input files. Most don't have an option for naming an output file
because they don't need one. Instead, you can use redirection to save any command's output anywhere you want.
If you run this command:
head -n 5 seasonal/summer.csv
it prints the first 5 lines of the summer data on the screen. If you run this command instead:
head -n 5 seasonal/summer.csv > top.csv
nothing appears on the screen. Instead, head's output is put in a new file called top.csv.

nothing appears on the screen. Instead, head's output is put in a new file called top.csv. You can take a look at that
file's contents using cat:
cat top.csv
The greater-than sign > tells the shell to redirect head's output to a file. It isn't part of the head command; instead, it
works with every shell command that produces output.

How can I use a command's output as an input?


Suppose you want to get lines from the middle of a file. More specifically, suppose you want to get lines 3 -5 from
one of our data files. You can start by using head to get the first 5 lines and redirect that to a file, and then use tail to
select the last 3:
head -n 5 seasonal/winter.csv > top.csv
tail -n 3 top.csv
A quick check confirms that this is lines 3-5 of our original file, because it is the last 3 lines of the first 5.

Use cut to select all of the tooth names from column 2 of the comma delimited file seasonal/summer.csv, then pipe
the result to grep, with an inverted match, to exclude the header line containing the word "Tooth". cut and grep were
covered in detail in Chapter 2, exercises 8 and 11 respectively.
Solution

cut -d , -f 2 seasonal/summer.csv | grep -v Tooth

How can I count the records in a file?


The command wc (short for "word count") prints the number of characters, words, and lines in a file. You can make it
print only one of these using -c, -w, or -l respectively.
Count how many records in seasonal/spring.csv have dates in July 2017 (2017-07).
• To do this, use grep with a partial date to select the lines and pipe this result into wc with an appropriate flag to
count the lines.
Solution

grep 2017-07 seasonal/spring.csv | wc -l

1 CS Fundmentals Page 9
1 CS Fundmentals Page 10

You might also like