0% found this document useful (0 votes)
2 views12 pages

UNIX Lab Programs

The document outlines various shell commands and scripts used in Unix/Linux systems, categorized into different functionalities such as file management, process management, and user information. It includes example scripts for tasks like checking file permissions, counting lines without vowels, and merging files. Additionally, it provides step-by-step instructions for creating a working environment for Unix programming.

Uploaded by

spam123zzy
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)
2 views12 pages

UNIX Lab Programs

The document outlines various shell commands and scripts used in Unix/Linux systems, categorized into different functionalities such as file management, process management, and user information. It includes example scripts for tasks like checking file permissions, counting lines without vowels, and merging files. Additionally, it provides step-by-step instructions for creating a working environment for Unix programming.

Uploaded by

spam123zzy
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

Prog1

a.

• “date”: Shows current date.


• “cal”: Shows current month’s calendar.
• “whoami”: Shows the name of user.
• “uptime”: Shows how long system has been running.
• “finger”: Shows M before converted character.
• “history”: Shows all commands used previously.
b.
• “cat”: Used for Concatenation.
• “more”: To view content of the file one screen at a time.
• “head”: To view first 10 lines of text file.
• “tail”: To view last 10 lines of text file.
• “cmp”: To compare contents of two text files.
• “vi”: Interface to type shell script.
• “grep”: Searches for particular patterns.
• “wc”: Prints the length of the longest line in a file.

c.

• “cp”: Copies contents of the file to new file.


• “ls”: Lists down all files in the directory.
• “rm”: Removes/Destroys file.
• “chmod”: Changes permission information on the file.
• “find”: Finds file in directory.
• “mv”: moves file to different filename.
d.

• “ps”: Displays information about current running programs.


• “kill”: Manually terminate a process.
• “exit”: End the session.

e.

• “mkdir”: Creates a new directory in the current directory.


• “rmdir”: Removes a directory.
• “pwd”: Shows the path of the file.
• “cd”: Changes the file directory.

Prog2
#!/bin/bash

#Loop through each file in the current directory

for file in *; do

#Check if the file has read, write and execute permissions for the current user

if [[ -r $file && -w $file && -x $file ]]; then

#Print the name of the file

echo $file

fi
done

Prog3
clear

echo " Enter the number of files::"

read n

echo "Enter the "n" files ::"

read fn

set $fn

for i in `cat $1`

do

echo -e " word = $i"

echo -e "------------"

grep -c "$i" $*

echo -e "------------"

done
Prog4

echo -n "Enter Filename:"

read fileName

if [ ! -f $fileName ]

then

echo "Filename $fileName doesn't exist"

exit 1

fi

command

tr '[A-Z]' '[a-z]' < $fileName


Prog6

echo -n "Enter file 1:"

read file1

echo -n "Enter file 2:"

read file2

if cmp -s $file1 $file2

then

echo Same

rm $file2

else

echo Different

fi
Prog7a

#!/bin/bash

# Check if file exists

if [ ! -f "$1" ]; then

echo "File does not exist"

exit 1

fi

# Loop through each line in the file and count lines without vowels

count=0

while IFS= read -r line; do

if [[ ! $line =~ [aeiouAEIOU] ]]; then

((count++))

fi

done < "$1"

echo "Number of lines without vowels: $count"

Prog7b

#!/bin/bash

# Count the number of characters, words, and lines

num_chars=$(wc -c < "$1")

num_words=$(wc -w < "$1")

num_lines=$(wc -l < "$1")

# Print the results

echo "Number of characters: $num_chars"

echo "Number of words: $num_words"

echo "Number of lines: $num_lines"


Prog 8

# !/bin/bash

echo "enter directory name"

read dir

if [ -d "$dir" ];

then

echo "list of files in the directory"

directory=$(ls -l "$dir" | grep -v /) #'^d'

echo "Files in : $dir"

echo "$directory"

else

echo "enter proper directory name"

fi
Prog 9

#!/bin/bash

# List the users currently logged in

logged_in_users=$(w | awk '{print $1}')

# Print the list of logged-in users

echo "Users currently logged in:"

echo "$logged_in_users"

Prog 10

#!/bin/bash

gged_in_users=$(w | awk '{print $1}')

# Print the list of logged-in users

echo "Users currently logged in:"

echo "$logged_in_users"

# Merge three text files into a single file

cat 1.txt 2.txt 3.txt > "Merged_file.txt"

# Get the file descriptor for the new file

file_descriptor=$(ls -l merged_file.txt | awk '{print $5}')

# Print the file descriptor

echo "File descriptor for merged_file.txt: $file_descriptor"


Steps

1. Create Folder in Desktop. File Name: “UnixLab”.

2. Open Mobaxterm. Change directory to Desktop using “cd”.

3. Change directory to “UnixLab”.

4. Enter shell using “vi”.

5. Enter the program using “i”.

6.

You might also like