0% found this document useful (0 votes)
15 views

Linux Practical2

Uploaded by

apiit.sachin12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Linux Practical2

Uploaded by

apiit.sachin12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Practical -1

Familiarize with Unix/Linux Log In/Log Out and various other commands &vi editor

Linux Log In/Log Out

1. Local Login:

login:

• The login command initiates a new login session on the local system.
• It prompts for a username and password.
• Once logged in, it provides access to the system's resources.

Command for Remote Login


$ login

2. Remote Login:

ssh (Secure Shell):

• The ssh command is used to log in to a remote system securely.


• It requires the username and hostname (or IP address) of the remote system.
• SSH encrypts the data during transmission, providing security.

Command for Remote Login


$ ssh username@hostname

3. Logout:

exit:

•The exit command is used to terminate a shell session.


•It closes the current shell or logs out of a remote session initiated via SSH.
Command for Logout

$ logout

Various other basic Commands:

Command Meaning Example


ls List directory contents ls
cd Change directory cd /path/to/directory
pwd Print working directory pwd
mkdir Make directory mkdir new_directory
rm Remove files or directories rm file.txt or rm -r directory
cp Copy files or directories cp file.txt new_file.txt
mv Move (rename) files or directories mv file.txt new_location/
cat Concatenate and display file content cat file.txt
head Display the beginning of a file head file.txt
tail Display the end of a file tail file.txt
Grep Search for patterns in files grep pattern file.txt
Chmod Change file permissions chmod 755 file.txt
Chown Change file owner and group chown user:group file.txt
Ps Display information about processes ps aux
Kill Terminate processes kill PID or killall process_name
Ssh Secure Shell - Remote login ssh username@hostname
vi or vim Text editor vi filename or vim filename
Login Start a new local session login
Exit Terminate the current shell session exit
Logout Log out of the current shell session logout
Ctrl + D Send EOF (End-of-File) signal <kbd>Ctrl</kbd> + <kbd>D</kbd>

Vi Editor Basic Commands:


1. Opening and Saving Files:

• Opening a File: To open a file in Vi, type vi followed by the filename:


vi filename

• Saving Changes and Quitting:


• Press Esc to ensure you're in command mode.
• Type :w to save changes without quitting.
• Type :wq to save changes and quit.
• Type :q! to quit without saving changes.

2. Navigating within a File:

• Moving the Cursor:


• Use arrow keys or h, j, k, l for left, down, up, and right respectively.
• Moving to Specific Locations:
• 0 (zero): Move to the beginning of the current line.
• $: Move to the end of the current line.
• gg: Move to the first line of the file.
• G: Move to the last line of the file.
• <line_number>G: Move to a specific line number.

3. Editing Text:

• Inserting Text:
• Press i to enter insert mode before the cursor.
• Press a to enter insert mode after the cursor.
• Press o to open a new line below the current line and enter insert mode.
• Deleting Text:
• Press x to delete the character under the cursor.
• Press dd to delete the entire line.
• Press d$ to delete from the cursor to the end of the line.
• Press dw to delete a word.

4. Exiting Insert Mode:

• Press Esc to exit insert mode and return to command mode.

5. Searching:

• Searching for Text:


• Press / followed by the text you want to search for, then press Enter.
• Use n to find the next occurrence and N to find the previous occurrence.

6. Other Useful Commands:

• Undoing Changes:
• Press u to undo the last change.
• Redoing Changes:
• Press Ctrl + r to redo changes that were undone.
• Copying and Pasting:
• Position the cursor where you want to start copying.
• Press v to enter visual mode.
• Move the cursor to select the text.
• Press y to copy (yank) the selected text.
• Move the cursor to where you want to paste.
• Press p to paste the copied text after the cursor.
Output of Some basic commands on terminal:

Vi Editor -
Practical -2

Develop simple shell programs using Bash or any other shell in Linux.

• Simple testing program in vi editor using Bash


vi abc.sh

chmod +x abc.sh

~$ ls -l
total 4
-rw-r--r-- 1 user user 0 Apr 21 15:39 2024-04-21-terminal-1.term
-rw-r--r-- 1 user user 0 Apr 21 15:39 2024-04-21-terminal-2.term
-rw-r--r-- 1 user user 20 Apr 21 14:31 abc
-rwxr-xr-x 1 user user 57 Apr 21 15:44 abc.sh
-rw-r--r ~$

~$ ./abc.sh
Linux Shell Testing
Welcome in linux Shell

• Sum of two numbers using bash

#!/bin/bash
# Prompt the user to enter the first number
echo "Enter the first number:"
read num1
# Prompt the user to enter the second number
echo "Enter the second number:"
read num2
# Perform the addition
sum=$((num1 + num2))
# Display the result
echo "The sum of $num1 and $num2 is: $sum"
Output:

~$ vi sum.sh

~$ ls -l

total 5

-rw-r--r-- 1 user user 0 Apr 21 15:39 2024-04-21-terminal-1.term

-rw-r--r-- 1 user user 0 Apr 21 15:39 2024-04-21-terminal-2.term

-rw-r--r-- 1 user user 20 Apr 21 14:31 abc

-rwxr-xr-x 1 user user 57 Apr 21 15:44 abc.sh

-rw-r--r-- 1 user user 57 Apr 21 15:44 apiit.txt

-rw-r--r-- 1 user user 294 Apr 21 15:54 sum.sh

~$ chmod +x sum.sh

~$ ./sum.sh

Enter the first number:

20

Enter the second number:

30

The sum of 20 and 30 is: 50

Program-3

Develop advanced shell programs using grep, fgrep & egrep

#!/bin/bash

echo "Enter the pattern to search for:"

read pattern
echo "Enter the filename:"

read filename

echo "Results for pattern '$pattern' in file '$filename':"

grep "$pattern" "$filename"

Output :

vi grep1.sh

~$ chmod +x grep1.sh

~$ ./grep1.sh

Enter the pattern to search for:

first

Enter the filename:

sum.sh

Results for pattern 'first' in file 'sum.sh':

# Prompt the user to enter the first number

echo "Enter the first number:"


4.Using fgrep for fixed string matching:

#!/bin/bash

# Define the pattern to search for


pattern="APIIT"

# Iterate over all files in the current directory


for file in *; do
# Check if the current item is a file
if [ -f "$file" ]; then
# Use fgrep to search for the pattern in the current file
if fgrep -q "$pattern" "$file"; then
echo "Pattern '$pattern' found in file: $file"
else
echo "Pattern '$pattern' not found in file: $file"
fi
fi
done

Output:

$ cat>file

my name is vishav. i am doing BTECCH FROM APIIT.

APIIT ENG AND MANAGEMENT COLLEGE.

Ctrl+d

5. Count the occurrences of a pattern in a file:


#!/bin/bash
echo "Enter the pattern to count:"
read pattern
echo "Enter the filename:"
read filename
count=$(grep -o "$pattern" "$filename" | wc -l)
echo "Number of occurrences of '$pattern' in '$filename': $count"
6. Program using awk command with database.txt
File database.txt
Name Age Gender Occupation
John 30 Male Engineer
Alice 25 Female Doctor
Bob 40 Male Teacher

Program:

#!/usr/bin/awk -f

# Print specific columns (Name and Gender)

print $1, $3

# Filter based on condition (Gender is "Male")

$3 == "Male"

# Calculate statistics (Average age)

NR > 1 {

total += $2

# Customized output (Greeting the first person)

NR == 1 {

print "Welcome " $1 ", you are a " $4 ". How can I assist you?"

# End of script

END {
if (NR > 1) {

print "Average age:", total/(NR-1)

Output:

You might also like