0% found this document useful (0 votes)
13 views3 pages

Linux/Unix File Search Script

This document is an assignment for a course on Linux/Unix systems at the University of Dodoma, authored by a student named Bright Peter Ng'ondyah. It includes a Bash script that searches for specific file extensions in a given directory and prints the found files, along with a second part that searches for a keyword in those files. The assignment is supervised by Dr. Justin Woiso and is part of the BSc-CNISE program.

Uploaded by

Gabriel Mturi
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)
13 views3 pages

Linux/Unix File Search Script

This document is an assignment for a course on Linux/Unix systems at the University of Dodoma, authored by a student named Bright Peter Ng'ondyah. It includes a Bash script that searches for specific file extensions in a given directory and prints the found files, along with a second part that searches for a keyword in those files. The assignment is supervised by Dr. Justin Woiso and is part of the BSc-CNISE program.

Uploaded by

Gabriel Mturi
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

THE UNIVERSITY OF DODOMA

COLLEGE OF INFORMATICS AND VIRTUAL EDUCATION

DEGREE PROGRAM: BSc-CNISE 2.


COURSE NAME: INTRODUCTION TO LINUX /UNIX SYSTEM

COURSE CODE: CP 211

COURSE INSTRUCTOR: DR. JUSTIN WOISO

NATURE OF WORK: ASSIGNMENT 3


STUDENT NAME: BRIGHT PETER NG’ONDYAH

REGISTRATION NUMBER: T21-03-03649

1
ANSWER 1

#!/bin/bash

# Define the directory to search in

dir=$1

# Define the file extensions to search for

extensions=(".tiff" ".ogg" ".r")

# Index for iterating through the extensions array

index=0

# Iterate through the extensions array using a while loop

while [ $index -lt ${#extensions[@]} ]; do

extension=${extensions[index]}

# Search for files with the specified extension in the directory

found_files=($(find $dir -name "*$extension"))

# Check if any files were found

if [ ${#found_files[@]} -gt 0 ]; then

# Print the found files

echo "Files with extension $extension found:"

for file in "${found_files[@]}"; do

echo $file

done

else

# Print a message if no files were found

echo "No files with extension $extension found."

2
fi

# Increment the index for the next iteration

index=$((index + 1))

done

ANSWER 2

# Search for a keyword in all found files

for file in "${found_files[@]}";

do grep -i "keyword" $file

done

You might also like