There are several reasons why one might choose to use linux:
The emergence of Linux, one of the world’s most widely used open-source operating systems, can be traced to several important events and the work of a few people. Below is a summary of the major events that led to the emergence of Linux.
The Linux ecosystem is a constantly evolving and expanding platform, so there is a lot of development going on. Notable recent developments include:
Absolute Pathname
To write an abosulte path-name:
$cat abc.sql
Note: we don't have to write '$' its already written in terminal.
$cat /home/database/abc.sql
In the above example, if the first character of a pathname is /, the file’s location must be
determined with respect to root.
Relative path
Using . and .. in Relative Path-names
$pwd
/home/kt/abc
$cd .. *** moves one level up ***
$pwd
/home/kt
Note: Now when / is used with .. has a different meaning, instead of moving down a
level, it moves one level up.
$pwd
/home/kt/abc
$cd ../.. *** moves two level up ***
$pwd
/home
Suppose you are currently located in home/kt and you want to change your directory to home/kt/abc. Let's see both the absolute and relative path concepts to do this:
1. Changing directory with relative path concept:
$pwd
/home/kit
$cd abc
$pwd
/home/kit/abc
2. Changing directory with absolute path concept:
$pwd
/home/kit
$cd /home/kt/abc
$pwd
/home/kit/abc
pwd
Example ↓
$pwd
/home/database/sql/dataone/
tree
cd
$cd /etc/ssh
By using the absolute path, it doesn't matter where in the filesystem you currently are.
. .. ~
Take a shortcut. These make navigation easier. Three of them are:
$sudo cp /etc/ssh/sshd_config .
$cd ..
$cp /etc/ssh/sshd_config ~
Home directory
$cd ~
$cd ~username
$cd -
ls
$ls dirname
mkdir
$mkdir mydir
Creates the directory mydir in the currect directory.
$mkdir /tmp/test-dir
This command creates the directory test-dir in the /tmp directory. The mkdir command
produces no output if it successfully creates the requested directory.
$mkdir docs pub
Creates the directories docs and pub under the current directory.
Creating Parent Directories
$mkdir /tmp/amrood/test
mkdir: Failed to make directory "/tmp/amrood/test";
No such file or directory
$mkdir -p /tmp/amrood/test
rmdir
$rmdir dirname
Note − To remove a directory, make sure it is empty which means there should not be
any file or sub-directory inside this directory.
$rmdir dirname1 dirname2 dirname3
The above command removes the directories dirname1, dirname2, and dirname3, if they are
empty. The rmdir command produces no output if it is successful.
mv
$mv olddir newdir
$mv mydir yourdir
ls -la
pravin@Pravins-MacBook-Air BCA % ls -la
total 64
drwxr-xr-x@ 10 pravin staff 320 Apr 18 19:01 .
drwxrwxrwx@ 8 pravin staff 256 Feb 15 09:46 ..
-rw-r--r--@ 1 pravin staff 8196 Jun 4 10:58 .DS_Store
drwxr-xr-x 15 pravin staff 480 Jun 5 12:29 .git
-rw-r--r-- 1 pravin staff 8 Feb 18 12:13 .gitignore
drwxr-xr-x 3 pravin staff 96 Apr 18 19:01 .vscode
-rw-r--r--@ 1 pravin staff 8661 May 28 09:15 index.html
drwxr-xr-x 9 pravin staff 288 May 21 10:34 public
-rw-r--r-- 1 pravin staff 674 Apr 17 06:09 readme.md
drwxr-xr-x 6 pravin staff 192 Apr 27 16:18 resources
Creating files
$vi filename
This is unix file... and we are writing on this file.
I'm going to save this content in the file.
Editing files
$vi filename
Display content of a file
$cat filename
This is unix file... and we are writing on this file.
I'm going to save this content in the file.
$
Copying files
$cp source_file destination_file
$cp filename copyfile
Renaming files
$mv myold_file newfile
Deleting files
$rm filename
$rm filename1 filename2 filename3
Under normal circumstances, every Unix program has three streams (files) opened for it when it starts up
We will discuss in detail about file permission and access modes in Unix. File ownership is an important component of Unix that provides a secure method for storing files. Every file in Unix has the following attributes −
$ls -l /home/amrood
-rwxr-xr-- 1 amrood users 1024 Nov 2 00:10 myfile
drwxr-xr--- 1 amrood users 1024 Nov 2 00:10 mydir
The permissions of a file are the first line of defense in the security of a Unix system. The basic building blocks of Unix permissions are the read, write, and execute permissions, which have been described below :
Directory access modes are listed and organized in the same manner as any other file. There are a few differences that need to be mentioned:
To change the file or the directory permissions, you use the chmod (change mode) command. There are two ways to use chmod — the symbolic mode and the absolute mode.
Using chmod in Symbolic Mode
The easiest way for a beginner to modify file or directory permissions is to use the symbolic mode. With symbolic permissions you can add, delete, or specify the permission set you want by using the operators in the following table.
Sr. No Chmod operator & Description
1 + Adds the designated permission(s) to file or directory
2 - Removes the designated permission(s) to file or directory
3 = Sets the designated permission(s).
Here's an example using testfile. Running ls -l on the testfile shows that the file's permissions are as follows
$ls -l testfile
-rwxrwxr-- 1 amrood users 1024 Nov 2 00:10 testfile
Then each example chmod command from the preceding table is run on the testfile, followed by ls –l, so you can see the permission changes
$chmod o+wx testfile
$ls -l testfile
-rwxrwxrwx 1 amrood users 1024 Nov 2 00:10 testfile
$chmod u-x testfile
$ls -l testfile
-rw-rwxrwx 1 amrood users 1024 Nov 2 00:10 testfile
$chmod g = rx testfile
$ls -l testfile
-rw-r-xrwx 1 amrood users 1024 Nov 2 00:10 testfile
Here's how you can combine these commands on a single line
$chmod o+wx,u-x,g = rx testfile
$ls -l testfile
-rw-r-xrwx 1 amrood users 1024 Nov 2 00:10 testfile
Using chmod with Absolute Permissions
The second way to modify permissions with the chmod command is to use a number to specify each set of permissions for the file.
Each permission is assigned a value, as the following table shows, and the total of each set of permissions provides a number for that set.
Number Octal Permission Representation Ref
0 No permission ---
1 Execute permission --x
2 Write permission -w-
3 Execute and write permission: 1 (execute) + 2 (write) = 3 -wx
4 Read permission r--
5 Read and execute permission: 4 (read) + 1 (execute) = 5 r-x
6 Read and write permission: 4 (read) + 2 (write) = 6 rw-
7 All permissions: 4 (read) + 2 (write) + 1 (execute) = 7 rwx
Here's an example using the testfile. Running ls -1 on the testfile shows that the file's permissions are as follows:
$ls -l testfile
-rwxrwxr-- 1 amrood users 1024 Nov 2 00:10 testfile
Then each example chmod command from the preceding table is run on the testfile, followed by ls –l, so you can see the permission changes
$ chmod 755 testfile
$ls -l testfile
-rwxr-xr-x 1 amrood users 1024 Nov 2 00:10 testfile $chmod 743 testfile
$ls -l testfile
-rwxr---wx 1 amrood users 1024 Nov 2 00:10 testfile $chmod 043 testfile
$ls -l testfile
----r---wx 1 amrood users 1024 Nov 2 00:10 testfile
The chown command changes the ownership of a file. The basic syntax is as follows:
$chown user filelist
The value of the user can be either the name of a user on the system or the user id (uid) of a user
on the system.
The following example will help you understand the concept
$chown amrood testfile
$
Changes the owner of the given file to the user amrood.
NOTE − The super user, root, has the unrestricted capability to change the ownership of any
file but normal users can change the ownership of only those files that they own.
The chgrp command changes the group ownership of a file. The basic syntax is as follows
$ chgrp group filelist
The value of group can be the name of a group on the system or the group ID (GID) of a group on the
system.
Following example helps you understand the concept:
$ chgrp special testfile
$
Changes the group of the given file to special group.
Often when a command is executed, it will have to be executed with special privileges in order to
accomplish its task.
As an example, when you change your password with the passwd command, your new password is
stored in the file /etc/shadow.
As a regular user, you do not have read or write access to this file for security
reasons, but when you change your password, you need to have the write permission to this file. This
means that the passwd program has to give you additional permissions so that you can write to
the file /etc/shadow.
Additional permissions are given to programs via a mechanism known as the Set User ID (SUID)
and Set Group ID (SGID) bits.
When you execute a program that has the SUID bit enabled, you inherit the permissions of that
program's owner. Programs that do not have the SUID bit set are run with the permissions of the user
who started the program.
This is the case with SGID as well. Normally, programs execute with your group permissions,
but instead your group will be changed just for this program to the group owner of the program.
The SUID and SGID bits will appear as the letter "s" if the permission is available. The
SUID "s" bit will be located in the permission bits where the owners’ execute permission normally
resides.
For example, the command
$ ls -l /usr/bin/passwd
-r-sr-xr-x 1 root bin 19031 Feb 7 13:47 /usr/bin/passwd* $
Shows that the SUID bit is set and that the command is owned by the root. A capital letter S
in the execute position instead of a lowercase s indicates that the execute bit is not set.
If the sticky bit is enabled on the directory, files can only be removed if you are one of the
following users:
$ chmod ug+s dirname
$ ls -l
drwsr-sr-x 2 root root 4096 Jun 19 06:45 dirname
$
$date
Shell Types
# this is a comment
#!/bin/sh
#!/bin/bash
pwd
ls
#!/bin/bash
# Author: Zara Ali
# Copyright(c) Tutorialspoint.com
# Script follows here:
pwd
ls
#!/bin/sh
# Author : Zara Ali
# Copyright (c) Tutorialspoint.com # Script follows here:
echo "What is your name?"
read PERSON
echo "Hello, $PERSON"
$./test.sh
What is your name? Zara Ali
Hello, Zara Ali
$
Example 1:
In this example, we will concatenate two strings using += operator. The input strings
will be stored in two variables, a and b.
Code ↓
#Read inputs a and b and store string variables in them.
read a b
#append b to the string a
a+=$b
#Output the resulting string
echo $a
Example 2:
In this example, we have two variables a and b, and we stored a string in each variable. We
want to concatenate them one after the other. The result of concatenation will be stored in the
variable c.
Code ↓
#Read inputs a and b and store string variables in them.
read a b
#concatenate the strings by writing one after the other. Store the result in c
c=$a$b
#output c
echo $c
Problem solution:
Here, we will create a shell script program to create two variables and then print values of
variables on the console screen.
Program/Source code:
The source code to create a Linux shell script program to create and print the value of
variables is given below. The given program is compiled and executed successfully on
Ubuntu 20.04.
#!/bin/bash
# Program name: "printVar.sh"
# Linux shell script program to create and print the value of variables.
country="India"
year=2021
echo "Country name: $country"
echo "Year: $year"
Output ↓
$ sh printVar.sh
Country name: India
Year: 2021
Explanation:
Here, we created two variables country, year. which are initialized
with "India", 2021 respectively. After that, we printed the value of variables on the console
screen using echo command.
Problem solution:
Here, we will create a shell script program to add two integer numbers and then print the sum of
numbers on the console screen.
Program/Source code:
The source code to create a Linux shell script program to add two numbers is given below.
#!/bin/bash
# Program name: "add_two_num.sh"
# Shell script program to add two numbers.
num1=10
num2=20
num3=`expr $num1 + $num2`
echo "Sum is: $num3"
Now, we will save the shell script program with "add_two_num.sh" name.
Output ↓
$ sh add_two_num.sh
Sum is: 30
Explanation:
In the above program, we created two variables num1, num2 that are initialized with 10 and 20
respectively. Here, we used the expr command to add two values of variables and then print the sum
of two numbers on the console screen.
Problem solution:
Here, we will create a shell script program to swap to numbers and then print both variables
after swapping on the console screen.
Program/Source code:
The source code to create a Linux shell script program to swap two numbers is given below.
#!/bin/bash
# Program name: "swap.sh"
# shell script program to swap two numbers.
num1=10
num2=20
echo "Before Swapping"
echo "Num1: $num1"
echo "Num2: $num2"
num3=$num1
num1=$num2
num2=$num3
echo "After Swapping"
echo "Num1: $num1"
echo "Num2: $num2"
Output ↓
$ sh swap.sh
Before Swapping
Num1: 10
Num2: 20
After Swapping
Num1: num2
Num2: num3
Sol:
Logical Operators
These operators are typically used to combine multiple conditions or expressions and evaluate the
result based on their logical relationship.
Example ↓
#!/bin/bash
# This is a script to compare two numbers using logical operator.
# Assign values to variables
x=5
y=10
# Check if both conditions are true using logical AND operator (&&)
if ((x > 0)) && ((y < 10)); then
echo "Both conditions are true."
fi
# Check if either of the conditions is true using logical OR operator (||)
if ((x > 0)) || ((y < 10)); then
echo "At least one condition is true."
fi
# Check if a condition is false using logical NOT operator (!)
if ! ((x == y)); then
echo "The numbers are not equal."
fi
Relational Operators
These operators are primarily used for comparing values or variables and returning a true or false
result based on the comparison.
Example ↓
#!/bin/bash
# This is a script to compare two numbers relational operator.
# Assign values to variables
x=5
y=10
# Compare the values using relational operators
if ((x == y)); then
echo "The numbers are equal."
elif ((x > y)); then
echo "x is greater than y."
else
echo "x is less than y."
fi
Reference ↓