Unit 1:
A script is a sequence of commands that are written in a file and executed
by the shell or another interpreter. In the context of shell scripting (e.g.,
Bash), a script automates tasks by executing a series of commands and
logic in sequence. Shell scripts can perform operations like system
administration, text processing, file manipulation, and more.
1. Cal
Command Description Example Output/Explanation
Displays the Shows the current month’s
cal calendar for the Cal calendar, with today’s date
current month. highlighted if supported.
Displays the
calendar for a
cal MM cal 11 Displays the calendar for
specific month
YYYY 2024 November 2024.
(MM) and year
(YYYY).
Displays the entire
calendar for a Shows all months in the year
cal YYYY cal 2024
specified year 2024.
(YYYY).
Displays the
Shows the calendar for each
cal -y calendar for the cal -y
month in the current year.
entire current year.
2. Date
The date command in Unix/Linux systems displays the current date and
time .
Command Description Example Output/Explanation
Displays the
date +"Month: date +"Month: Month: 12 (December
current
%m" %m" is month 12).
month.
Displays the
date +"Date: current date date +"Date: Date: 13 (Today’s
%d" (day of the %d" date is 13).
month).
Displays the
date +"Year: date +"Year: Year: 2024 (Current
current
%Y" %Y" year is 2024).
year.
date +"Hour: Displays the date +"Hour: Hour: 15 (3 PM in 24-
%H" current %H" hour clock).
hour in 24-
Command Description Example Output/Explanation
hour
format.
Displays the
date +"Minute: date +"Minute: Minute: 24 (24th
current
%M" %M" minute of the hour).
minute.
Displays the
date +"Second: date +"Second: Second: 35 (35th
current
%S" %S" second of the minute).
second.
Displays the Full Date: 2024-12-13
date +"Full Date: date +"Full Date:
full current (Year-Month-Day
%Y-%m-%d" %Y-%m-%d"
date. format).
Displays the date +"Full Full Time: 15:24:35
date +"Full Time:
full current Time: %H:%M: (Hour:Minute:Second
%H:%M:%S"
time. %S" format).
Display
time in
Outputs 10:00:00 if
date +"%H:%M: HH:MM:SS date +"%H:%M:
the current time is
%S" format (24- %S"
10:00 AM.
hour
format).
Display date
date +"%A, %B in a more date +"%A, %B Outputs Monday,
%d, %Y" readable %d, %Y" November 11, 2024.
format.
Display
time in
HH:MM
date +"%I:%M date +"%I:%M Outputs 10:00 AM if
AM/PM
%p" %p" the time is 10:00 AM.
format (12-
hour
format).
Common Formatting Codes
Format Code Description Example Output
%Y Year (4 digits) 2024
%y Year (last 2 digits) 24
%m Month (01–12) 11
%B Full month name November
%b Abbreviated month name Nov
%d Day of the month (01–31) 11
Format Code Description Example Output
%A Full weekday name Monday
%a Abbreviated weekday name Mon
%H Hour (00–23, 24-hour format) 10
%I Hour (01–12, 12-hour format) 10
%M Minute (00–59) 00
%S Second (00–59) 00
%p AM or PM AM
3. Echo
The echo command in Unix/Linux is used to display a line of text or
variables in the terminal. It’s widely used in scripts to output messages,
format text, or display the values of variables.
Common Options and Usage
Command Description Example Output/Explanation
Displays text
echo "text" in the echo "Hello" Outputs Hello.
terminal.
a=10
Displays the press enter then
echo Outputs the current
value of a write in next
$VARIABLE username.
variable. shell
echo $a
Enables
interpretation
Outputs Hello on
of escape echo -e "Hello\
echo -e "text" one line and World
sequences (like nWorld"
on the next.
newline \n, tab
\t).
Outputs text with a
Adds a tab
echo -e "\ echo -e "\ tab space at the
space before
tIndented Text" tIndented Text" beginning (Indented
the text.
Text).
Redirects
output to a Writes Hello to
echo "text" > file, echo "Hello" > [Link], overwriting
[Link] overwriting [Link] any previous
the file’s content.
contents.
“echo "text" >> Appends echo "Goodbye" Appends Goodbye to
[Link] output to a file >> [Link] the end of [Link].
Command Description Example Output/Explanation
without
overwriting.
Displays all
Lists files and
files and
directories in the
echo * directories in echo *
current folder, like a
the current
basic ls command.
directory.
Performs
arithmetic
echo "$
operations and echo "$((3 + 2))" Outputs 5.
((expression))"
outputs the
result.
Executes a
command and Outputs the current
echo $(command) echo $(date)
outputs its date and time.
result.
Escape Sequences with -e Option
Escape
Description Example Output
Sequence
echo -e "Hello\ Hello
\n Newline
nWorld" World
echo -e "Hello\
\t Horizontal tab Hello World
tWorld"
Hello
echo -e "Hello\
\v Vertical tab World (in
vWorld"
vertical)
echo -e "Hello\
\b Backspace HellWorld
bWorld"
Carriage echo -e "Hello\
\r World
return rWorld"
4. bc
The bc command in Unix/Linux is a command-line calculator that allows
you to perform mathematical operations with arbitrary precision. It's
commonly used for basic arithmetic, advanced mathematical functions,
and even working with variables and user-defined functions in scripts.
Example
5. Who
Here’s the who command explained in tabular form with common
options and examples:
Option Description Example Output/Explanation
Shows information Displays usernames, terminal
Who about all users who info, login time, and remote
currently logged in. source (if applicable).
Shows information
Displays current user, terminal,
who am about the current
who am i and login time for the active
i user logged into the
terminal session.
terminal.
Displays all available
information Lists all details, including boot
-a who -a
(equivalent to -b -d -l time, run level, and more.
-p -r -t -u).
Shows the time when
Outputs the system's last boot
-b the system was last who -b
time.
booted.
Shows information Displays any sessions that have
-d about dead processes who -d logged out but still show up due
or sessions. to lingering processes.
Lists only the current Outputs current login sessions,
-l login sessions who -l such as console or remote
(console or remote). access points.
Shows information
Displays active processes
-p about active who -p
related to logged-in users.
processes.
Displays the current
Shows the current run level of
-r run level of the who -r
the system.
system.
Displays the last time
Shows when the system clock
-t the system clock was who -t
was last updated.
updated.
Displays idle times Shows how long each user has
-u who -u
for users logged in. been idle, if applicable.
6. uname
The uname command in Unix/Linux is used to display system information,
such as the kernel name, version, machine architecture, and more. It is a
useful tool for obtaining details about the system you're working on.
Basic Syntax:
uname [OPTION]
Commonly Used Options:
Option Description Example Output/Explanation
uname Displays the kernel name. uname Prints the kernel name (e.g., Linux).
Displays all available system Outputs complete system information,
-a information (kernel, machine, uname -a including kernel version, machine type, OS,
OS, etc.). etc.
-s Displays the kernel name. uname -s Outputs the kernel name (e.g., Linux).
Displays the network node
-n uname -n Outputs the hostname of the machine.
(hostname) of the system.
Displays the kernel release Outputs the kernel version (e.g., 5.10.0-8-
-r uname -r
(version). amd64).
Displays the kernel version Outputs detailed kernel version info (e.g., #1
-v uname -v
(release info). SMP Debian 5.10.0-8).
Displays the machine hardware Outputs the machine architecture (e.g.,
-m uname -m
architecture. x86_64, armv7l).
-p Displays the processor type. uname -p Outputs the processor type (e.g., x86_64).
Outputs the hardware platform (e.g.,
-i Displays the hardware [Link] -i
x86_64, arm).
Outputs the operating system (e.g.,
-o Displays the operating system. uname -o
GNU/Linux).
7. tty
The tty command in Unix/Linux is used to display the file name of the
terminal connected to the standard input. It provides the name of the
terminal session (such as /dev/tty1, /dev/pts/0) that is currently being used
to interact with the system.
Syntax:
tty
Example Output:
Option Description Example Output/Explanation
Tty Displays the file name of the tty Example output: /dev/tty1 (for local
Option Description Example Output/Explanation
terminal connected to standard
terminals)
input.
8. man
The man command opens a manual page for the specified command.
The manual page is divided into sections that provide detailed information
about the command's syntax, options, arguments, and examples.
Syntax:
man [OPTION] <command>
Example
man ls
9. Wc
Option Description Example Output/Explanation
Print the line, word, and
Prints the number of lines, words,
Wc byte (character) count wc [Link]
and characters in [Link].
for a file.
wc -l Prints the number of lines in
-l Print only the line count.
[Link] [Link].
Print only the word wc -w Prints the number of words in
-w
count. [Link] [Link].
Print only the character
wc -c Prints the number of characters
-c count (including spaces,
[Link] (bytes) in [Link].
punctuation, etc.).
Print the length of the wc -L Prints the number of characters in
-L
longest line in the file. [Link] the longest line of [Link].
wc prints the line count, word count, and character count for each file
provided.
Linux Files and Directories
Current Working Directory-pwd, Listing Files and Directories-ls,
Matching Filenames with Patterns (wildcard characters), Simple ways to
create a file- touch, cat, echo, Showing the contents of a file-cat, more, less,
head, tail.
1. Current Working Directory - pwd
The pwd command stands for Print Working Directory. It displays the full
absolute path of the current directory.
$ pwd
/home/user/Documents
2. Listing Files and Directories – ls
The ls command lists the files and directories in the current directory. Common
options include:
ls: List files and directories.
ls -l: List in long format (shows file permissions, size, owner, etc. ).
ls -a: List all files, including hidden files (those starting with a
dot.).
ls -lh: Long format with human-readable sizes.
$ ls
[Link] [Link] folder1
$ ls -l
total 8
-rw-r--r-- 1 user user 1024 Nov 18 14:00 [Link]
-rw-r--r-- 1 user user 2048 Nov 18 14:05 [Link]
drwxr-xr-x 2 user user 4096 Nov 18 14:10 folder1
3. Matching Filenames with Patterns (Wildcard Characters)
Wildcards help in matching files and directories based on patterns. Common
wildcards include:
*: Matches zero or more characters.
$ ls *.txt # Lists all files ending with .txt
?: Matches exactly one character.
$ ls file?.txt # Matches [Link], [Link] but not [Link]
[ ]: Matches any one character inside the brackets.
$ ls file[1-3].txt # Matches [Link], [Link], [Link]
4. Creating a File - touch, cat, echo
There are multiple ways to create a file:
Using touch
The touch command creates an empty file if it does not already exist or updates
the file's timestamp if it does.
Example: $ touch [Link]
Using cat
The cat command can also create files when used with redirection (>):
Example: $ cat > [Link]
This is a new file. # Type text and press Ctrl+Z to save
Using echo
The echo command outputs text to the terminal or a file:
Example:$ echo "Hello World" > [Link]
5. Showing the Contents of a File - cat, more, less, head, tail
cat: Concatenate and display the content of files.
Example: $ cat [Link]
This is the content of file1.
more: View content page by page (useful for large files).
Example: $ more [Link]
Space: Move to the next page.
Enter: Move one line at a time.
q: Quit.
less: Similar to more, but with more navigation options.
Example:$ less [Link]
head: Display the first few lines of a file (default is 10 lines).
Example:$ head [Link]
To display the first n lines:
Example:$ head -n 5 [Link]
tail: Display the last few lines of a file (default is 10 lines).
Example:$ tail [Link]
To display the last n lines:
Example:$ tail -n 5 [Link]
Command Description Example
pwd Prints the current working directory $ pwd → /home/user
ls Lists files and directories in the current $ ls
directory
ls -l Lists files in long format (permissions, size, $ ls -l
date, etc.)
ls -a Lists all files, including hidden files $ ls -a
ls -lh Long format with human-readable file sizes $ ls -lh
ls *.txt Lists all .txt files using wildcard pattern $ ls *.txt
touch Creates an empty file $ touch [Link]
cat > Creates a file and inputs text $ cat > [Link]
echo Creates a file with specified content $ echo "Hello" >
[Link]
cat Displays the content of a file $ cat [Link]
more Displays content of a file page by page $ more [Link]
less Displays content of a file with scroll navigation$ less [Link]
head Shows the first 10 lines of a file (default) $ head [Link]
head -n Shows the first n lines of a file $ head -n 5 [Link]
tail Shows the last 10 lines of a file (default) $ tail [Link]
tail -n Shows the last n lines of a file $ tail -n 5 [Link]
tail -f Follows a file in real-time (useful for logs) $ tail -f [Link]
ls ? Matches files with exactly one character (e.g., $ ls a?.txt
a, b)
ls [ ] Matches files with any character inside $ ls file[1-3].txt
brackets
Unit 2:
Additional File Management Commands
Command Explanation Example
mkdir my_folder creates a directory named
mkdir Creates a new directory.
my_folder.
Removes an empty rmdir my_folder removes the directory my_folder
rmdir
directory. if it is empty.
C
Copies files or directories. cp [Link] [Link] copies [Link] to [Link].
p
cp -r dir1 dir2 copies the directory dir1 and its
contents to dir2.
Removes files or
rm directories (even if non- rm [Link] deletes [Link].
empty).
rm -r dir1 deletes dir1 and its contents.
Moves or renames files or
mv mv old_name.txt new_name.txt renames a file.
directories.
Compares two files byte cmp [Link] [Link] compares two files for
cmp
by byte. differences.
Command Explanation Example
Finds common lines comm [Link] [Link] shows common and unique
comm
between two sorted files. lines.
Displays line-by-line
diff [Link] [Link] highlights differences
diff differences between two
between files.
files.
Locates files or
find . -name "*.txt" finds all .txt files in the
find directories based on
current directory.
name or pattern.
Changes file/directory chmod 755 [Link] gives read, write, and execute
chmod
permissions. permissions to owner.
read name waits for the user to input and stores it
read Accepts user input.
in name.
Chmod
u for user (owner)
g for group
o for others
Permission Type Symbolic Representation Numeric Value
Read R 4
Write W 2
Execute X 1
Command/Permission Effect Explanation Example
Allows viewing
Grants read file contents or chmod u+r
r (Read)
access listing directory [Link]
contents.
Allows
modifying file
Grants write chmod g+w
w (Write) contents or
access [Link]
directory
structure.
x (Execute) Grants execute Allows running a chmod o+x
access file (if [Link]
Command/Permission Effect Explanation Example
executable) or
accessing a
directory.
7 means read,
write, and
Full access for chmod 777
chmod 777 [Link] execute (rwx) for
everyone [Link]
owner, group, and
others.
7 (rwx) for
Owner full;
owner, 5 (r-x) for chmod 755
chmod 755 [Link] group/others
group, 5 (r-x) for my_program
read+exec
others.
Owner
6 (rw-) for owner,
read/write; chmod 644
chmod 644 [Link] 4 (r--) for group,
others read- [Link]
4 (r--) for others.
only
Owner
6 (rw-) for owner,
read/write; no chmod 600
chmod 600 [Link] 0 (---) for group,
access for private_key.pem
0 (---) for others.
others
Makes a script
Adds execute chmod u+x
chmod u+x [Link] executable for the
for the owner [Link]
owner.
Explanation of Numeric Values (755, 644, etc.):
Each permission is represented by a number:
o 4 = read (r)
o 2 = write (w)
o 1 = execute (x)
o 0 = no permission (-)
The number is the sum of these values:
o 7 = read (4) + write (2) + execute (1) (rwx)
o 6 = read (4) + write (2) (rw-)
o 5 = read (4) + execute (1) (r-x)
o 4 = read (4) (r--)
Examples:
chmod 755 [Link]:
o 7 (Owner): read, write, execute (rwx)
o 5 (Group): read, execute (r-x)
o 5 (Others): read, execute (r-x)
chmod 644 [Link]:
o 6 (Owner): read, write (rw-)
o 4 (Group): read (r--)
o 4 (Others): read (r--)
Editing Files
Editor Explanation Example
Text editor with command and
Vi vi [Link] opens [Link] for editing.
insert modes.
Grep Command
The grep command in Linux/Unix is used to search for a specific string or
pattern in a file or input provided. It stands for Global Regular Expression
Print.
Syntax
grep [OPTIONS] PATTERN [FILE...]
Option Syntax Description Example
grep "hello" [Link]
Basic grep PATTERN Searches for a pattern in
(Searches for "hello" in
Usage FILE the specified file.
[Link].)
grep -i "hello" [Link]
grep -i Performs a case- (Matches "hello",
-i
PATTERN FILE insensitive search. "HELLO", "Hello",
etc.)
Inverts the match, grep -v "hello" [Link]
grep -v
-v displaying lines that do (Displays lines without
PATTERN FILE
not match the pattern. "hello".)
grep -n "hello" [Link]
grep -n Displays line numbers
-n (Outputs matched lines
PATTERN FILE for matched lines.
with line numbers.)
grep -c "hello" [Link]
grep -c Displays the count of
-c (Counts how many lines
PATTERN FILE matching lines.
contain "hello".)
grep -l grep -l "hello" *.txt
Lists the filenames
-l PATTERN (Lists files with
containing matches.
FILE1 FILE2 ... matches in all .txt files.)
Read command
The read command in shell scripting is used to read input from the user or
from a file.
Syntax
read [options] variable_name
Example
echo "Enter your name:"
read name
echo "Hello, $name!"
Reading Multiple Variables:
echo "Enter your first name and last name:"
read first_name last_name
echo "Hello, $first_name $last_name!"
Basics of Shell Scripting Programming:
Operator:
1. Arithmetic Operators
Used for mathematical calculations:
+ : Addition
- : Subtraction
* : Multiplication
/ : Division
% : Modulus (remainder)
++ : Increment
-- : Decrement
Example:
a=5
b=2
echo "addition of a and b " $((a + b))
echo $((a-b))
echo $((a*b))
echo $((a/b))
echo $((a%b))
x=2
((x++))
echo $x
((x--))
echo $x
2. Relational Operators
Used to compare two values:
-eq : Equal to
-ne : Not equal to
-lt : Less than
-le : Less than or equal to
-gt : Greater than
-ge : Greater than or equal to
Example:
a=5
b=10
if [ $a -lt $b ]
then
echo "$a is less than $b"
fi
3. Logical Operators
Used to combine multiple conditions:
&& : Logical -a
|| : Logical -o
! : Logical NOT
Example :
a=5
b=2
c=3
if [ $a -gt $b -a $a -gt $c ]
then
echo "a is greater then a and b"
elif [ $b -gt $a -a $b -gt $c ]
then
echo "b is greater then a and c"
else
echo " c is larger"
fi
4. String Operators
Used for string operations:
= : Equal to
!= : Not equal to
-z : String is null (empty)
-n : String is not null (not empty)
> : Greater than (lexicographically)
< : Less than (lexicographically)
Example:
str="Hello"
if [ -z "$str" ]; then
echo "String is empty"
else
echo "String is not empty"
fi
Example 2:
a='Somlalit'
c='BCA College'
if [[ $a > $c ]]
then
echo "a is greater"
else
echo "c is greater"
fi
5. File Test Operators
Used for checking file attributes:
-e : File exists
-f : Regular file
-d : Directory
-r : Readable
-w : Writable
-x : Executable
-s : Non-zero size (file not empty)
Example:
if [ -s [Link] ]
then
echo "yes "
else
echo "no "
fi
6. Redirection Operators
Used for redirecting input/output:
> : Redirect output to a file (overwrites)
>> : Redirect output to a file (appends)
< : Redirect input from a file
<< : providing input inline
Example:
echo "Hello World" > [Link]
cat [Link] >> [Link]
Decision Statements
1. if...then...fi
Syntax
if [ condition ]
then
# commands to execute if condition is true
Fi
Example
a=10
b=20
if [ $a -lt $b ]
then
echo "a is less than b"
fi
2. Using else
Syntax
if [ condition ]
then
# commands to execute if condition is true
Else
# commands to execute if condition is false
Fi
Example
a=30
b=20
if [ $a -lt $b ]
then
echo "a is less than b"
else
echo "a is not less than b"
fi
3. if...then...elif...fi
Syntax:
if [ condition1 ]
then
# commands to execute if condition1 is true
elif [ condition2 ]
then
# commands to execute if condition2 is true
else
# commands to execute if none of the above conditions are true
fi
Example
echo "Enter a number:"
read number
if [ $number -lt 10 ]
then
echo "The number is less than 10."
elif [ $number -eq 10 ]
then
echo "The number is equal to 10."
else
echo "The number is greater than 10."
fi
Example 2:
file="[Link]"
if [ -d "$file" ]
then
echo "$file is a directory."
elif [ -f "$file" ]
then
echo "$file is a regular file."
else
echo "$file does not exist or is of an unknown type."
fi
4. case….esac
The case statement in shell scripting is a way to perform multi-way
branching based on pattern matching. It is often used as an alternative to
if...elif...fi when dealing with multiple conditions.
Case $variable in
Choice 1;;
Choice 2;;
*);;
esac
Syntax
echo "Select an option:"
echo "1. Start"
echo "2. Stop"
echo "3. Restart"
read choice
case $choice in
1)
echo "You chose Start;;
2)
echo "You chose Stop."
;;
3)
echo "You chose Restart."
;;
*)
echo "Invalid option."
;;
esac
Example:
echo "enter choice"
read ch
case $ch in
1)
echo $(date)
;;
2)
echo $(cal)
;;
*)
echo "wrong choice"
;;
esac
Arithmetic Operations
Redirection and Piping
Comma
Explanation Example
nd
echo "Hello" > [Link] writes
> Redirects output to a file.
"Hello" to [Link].
Sends output of one command
` ` (pipe)
to another.
Outputs to a file and terminal
tee `echo "Hello"
simultaneously.
This format provides a quick reference and is easy to follow for
understanding, practicing, and answering related questions. Let me
know if you need further enhancements!
Command Explanation Example
cat Displays the entire content of a file. cat [Link]
Allows you to view large files page by
less less [Link]
page and scroll using the keyboard.
Similar to less but with fewer
more navigation features; displays content more [Link]
page-wise.
Displays the first 10 lines of a file by head [Link] or head
head
default (can specify number of lines). -n 20 [Link]
Displays the last 10 lines of a file by tail [Link] or tail -n
tail
default (can specify number of lines). 15 [Link]
Displays the file content with line
nl nl [Link]
numbers.
Opens the file in the vi editor for
vi vi [Link]
viewing or editing.
Opens the file in the nano editor for
nano nano [Link]
viewing or editing.
Displays file content in octal,
od od -c [Link]
hexadecimal, or ASCII format.
Extracts and displays readable text
strings strings binary_file
from binary or non-text files.
Example Usage:
1. To quickly view a file's content:
cat [Link]
2. To view only the first 5 lines of a file:
head -n 5 [Link]
3. To scroll through a long file:
less [Link]
4. To see the last few lines as the file updates in real-time (useful for
logs):
tail -f [Link]
Each command is suited for different purposes, depending on the file
size and required functionality.
Looping Statements
Loops in shell scripting are used to execute a series of commands multiple
times. Shell scripts support different types of loops, including for, while, and
until loops, as well as loop control statements like break and continue.
1. For Loop
The for loop iterates over a list of items.
Syntax:
for variable in list
do
commands
done
Example:
for i in 1 2 3 4 5
do
echo "Number: $i"
done
or
for i in {1..5}
do
echo $i
done
or
for (( i=1; i<=5; i++ ))
do
echo "Number: $i"
done
2. While Loop
Syntax
while [ condition ]
do
commands
done
Example
count=1
while [ $count -le 5 ]
do
echo $count
((count++))
done
3. until Loop
Syntax
until [ condition ]
do
# Commands to execute
Done
Example
count=1
until [ $count -gt 5 ]
do
echo "Count: $count"
count=$((count + 1))
done
break:
Example
for i in {1..5}
do
if [ $i -eq 3 ]
then
break
fi
echo $i
done
continue:
Example
for i in {1..5}
do
if [ $i -eq 3 ]
then
continue
fi
echo $i
done
Unit 3:
1. cut Command
The cut command is used to extract specific sections of a line from a file or
standard input. It works with fields (delimited text) or character positions.
Syntax
cut [options] [filename]
Common Options:
-f: Specifies the field number(s) to extract (works with a delimiter).
-d: Specifies the delimiter (default is a tab).
Example:
Suppose you have a file [Link] with the following content:
John,25,Engineer
Alice,30,Doctor
Bob,22,Designer
Extract the first column (names):
cut -d',' -f1 [Link]
Output
John
Alice
Bob
2. paste Command
The paste command is used to merge lines of files horizontally (side by side)
by default.
Syntax
paste [options] [file1] [file2] ...
Example
Suppose you have two files:
[Link]:
John
Alice
Bob
[Link]:
25
30
22
Merge the files line by line
paste [Link] [Link]
Output:
John 25
Alice 30
Bob 22
Merge the files with a custom delimiter (,):
paste -d',' [Link] [Link]
Output:
John,25
Alice,30
Bob,22
Combining cut and paste
You can combine cut and paste to extract and manipulate text more effectively.
Example:
Extract the names and professions from [Link], then merge them into a new
file:
bash
cut -d',' -f1 [Link] > [Link]
cut -d',' -f3 [Link] > [Link]
paste -d':' [Link] [Link]
Output:
makefile
John:Engineer
Alice:Doctor
Bob:Designer
Splitting a file vertically–cut
Syntax
cut [OPTION]... [FILE]...
Example
Sort
Option Description Syntax Example
Sorts lines alphabetically
Default sort [file] sort [Link]
(lexicographically).
-r Sorts lines in reverse order. sort -r [file] sort -r [Link]
-k Sorts based on a specified column (key). sort -k [column] [file] sort -k 2 [Link]
-u Removes duplicate lines after sorting. sort -u [file] sort -u [Link]
Sorts by month names (e.g., Jan, Feb,
-M sort -M [file] sort -M [Link]
etc.).
sort [file] -o sort [Link] -o
-o Outputs the result to a file.
[output_file] [Link]
Tr
The tr command in a shell script is used for translating or deleting characters
from standard input or a file.
Syntax
tr [options] SET1 [SET2]
SET1: Specifies the characters to be replaced, deleted, or matched.
SET2: Specifies the characters to replace characters in SET1 (if applicable).
Example
echo "hello" | tr 'a-z' 'A-Z'
Output
HELLO
Delete Characters Use the -d option to delete all occurrences of characters in
SET1.
Example
echo "hello world" | tr -d 'aeiou'
Squeeze Repeated Characters Use the -s option to replace repeated
occurrences of characters in SET1 with a single instance.
Example
echo "aaabbbccc" | tr -s 'abc'
Output
abc
Unit 4:
Here’s the information formatted in a tabular form for clarity:
Option Meaning
-c Create a new archive.
-v Verbose mode: Lists files as they are added to the archive.
-z Compress the archive using gzip.
Specifies the file name of the archive (must be followed by the archive
name).
-f
Command Syntax Example Output
Compress a gzip [options] Creates
Gzip gzip [Link]
file filename [Link]
gunzip
Decompress a gunzip
Gunzip [options] Restores [Link]
.gz file [Link]
[Link]
Tar Archive tar -cvf tar -cvf Creates
multiple files [Link] file1 [Link]
Option Meaning
[Link]
file2 [Link]
[Link]
tar -cvzf Creates
tar -cvzf
Archive and [Link] [Link]
Tar + Gzip [Link]
compress [Link] (compressed
file1 file2
[Link] archive)
Extract Extract tar tar -xvf tar -xvf Extracts files
Tar archive [Link] [Link] from [Link]
Extract Extracts files
Extract tar -xvzf tar -xvzf
compressed from
Tar+Gz [Link] [Link]
tar archive [Link]
Compress zip [options] zip [Link]
Creates
Zip files into .zip [Link] [Link]
[Link]
archive file1 file2 [Link]
Extracts files
Extract .zip unzip unzip
Unzip from
archive [Link] [Link]
[Link]