0% found this document useful (0 votes)
32 views5 pages

Os Assignment

This Bash script contains functions to recursively list directories and files, move files with a specific extension to another directory, and delete files with a specific extension from a directory. The main function uses a case statement in a while loop to call the appropriate function based on the user's menu selection and obtain any required arguments, like the directory or file extension.

Uploaded by

Javaria Tabassum
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)
32 views5 pages

Os Assignment

This Bash script contains functions to recursively list directories and files, move files with a specific extension to another directory, and delete files with a specific extension from a directory. The main function uses a case statement in a while loop to call the appropriate function based on the user's menu selection and obtain any required arguments, like the directory or file extension.

Uploaded by

Javaria Tabassum
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/ 5

UNIVERSITY OF ENGINEERING AND TECHNOLOGY,

TAXILA

SUBMITTED TO:
MAAM SABA AWAN

SUBMITTED BY:
JAVARIA TABASSUM

REGISTRATION NO:
22-SE-33

ASSIGNMENT

OPERATING SYSTEMS (LAB)


SCRIPT
#!/bin/bash

# Function to list directories recursively


list_directories() {
local directory="$1"

echo "Directories in $directory:"


echo "$directory" # Print the current directory

for dir in "$directory"/*; do


if [ -d "$dir" ]; then
list_directories "$dir"
fi
done
}

# Function to list files in a directory recursively


print_files_recursively() {
local directory="$1"
echo "Files in $directory:"
for file in "$directory"/*; do
if [ -f "$file" ]; then
echo "${file##*/}"
elif [ -d "$file" ]; then
print_files_recursively "$file"
fi
done
[ "$(ls -A "$directory")" ] || echo "No files found in $directory"
}

# Function to move files with specific extensions


move_files() {
mv "$source"/*."$extension" "$destination"
echo "Files with .$extension extension moved from $source to $destintaion"
}
# Function to delete files with specific extensions
delete_files() {
find "$directory" -type f -name "*.$extension" -delete
echo “Finding the files with .$extension in $directory.”
echo "Files with .$extension extension deleted."
}

# Main function
main() {
while true; do
echo "Select an operation:"
echo "1. List directories recursively"
echo "2. Print files in a directory recursively"
echo "3. Move files with specific extension"
echo "4. Delete files with specific extension"
echo "5. Exit"
read -p "Enter your choice (1, 2, 3, 4, or 5): " choice

case $choice in
1)
read -p "Enter the directory to list recursively: " directory
list_directories "$directory"
;;

2)
read -p "Enter the directory: " directory
print_files_recursively "$directory"
;;
3)
read -p "Enter the source directory: " source
read -p "Enter the destination directory : " destination
read -p "Enter the file extension to move (e.g., txt, jpg): " extension
move_files "$source" "$destination" "$extension"
;;
4)
read -p "Enter the directory: " directory
read -p "Enter the file extension to delete: " extension
delete_files "$directory" "$extension"
;;
5)
echo "Exiting the program."
exit
;;
*)
echo "Invalid choice. Please enter a number between 1 and 5."
;;
esac
done
}

# Run the main function


Main

OUTPUT

LIST DIRECTORIES RECURSIVELY:


LIST FILES RECURSIVELY:

MOVE FILES WITH SPECIFIC EXTENSION:

DELETE FILES WITH SPECIFIC EXTENSION:

You might also like