System Scripting Lab 1

Exercise 1: Linux Commands & File Management

1. Create a directory

mkdir SystemsScripting

Explanation: Creates a new folder called "SystemsScripting".

2. Change into the directory

cd SystemsScripting

3. Create another folder and enter it

mkdir lab01
cd lab01

4. Create two files

touch file1.txt file2.txt

Explanation: touch creates empty files.

5. Create two folders

mkdir folder1 folder2

6. List directory contents

ls

7. Create an alias to copy folders

alias copydir='cp -r'

Explanation: -r allows recursive copying of directories.

8. List all aliases

alias

9. Use alias to copy a folder

copydir folder1 folder1_copy

10. Verify copy

ls

11. Remove alias

unalias copydir

12. Verify alias removal

alias

13. Rename a file

mv file1.txt renamed_file.txt

14. Move second file into a folder

mv file2.txt folder1/

15. Install and use gedit

sudo apt-get install gedit
gedit filename.sh

Exercise 2: First Bash Script

Script: welcome.sh

#!/bin/bash

# Display welcome message with username
echo "Welcome $USER to System Scripting Lab 1"

# List contents of home directory
echo "Listing Home Directory:"
ls ~

Make script executable

chmod +x welcome.sh

Run script

./welcome.sh

Explanation:


Exercise 3: File & Folder Automation Script

Script: manage_files.sh

#!/bin/bash

# Create two folders
mkdir folderA folderB

# Create five files in folderA
touch folderA/file1.txt folderA/file2.txt folderA/file3.txt folderA/file4.txt folderA/file5.txt

# Move three files to folderB
mv folderA/file1.txt folderA/file2.txt folderA/file3.txt folderB/

# Display contents of both folders
echo "Contents of folderA:"
ls folderA

echo "Contents of folderB:"
ls folderB

Exercise 4: Globbing & Permissions

Script: globbing_script.sh

#!/bin/bash

# Create folders
mkdir folder1 folder2

# Create five files
touch folder1/file{1..5}.txt

# Move all files using globbing (* matches all files)
mv folder1/* folder2/

# Rename folder2
mv folder2 newFolder

# Change into renamed folder
cd newFolder

# Grant read and execute permission to group
chmod g+rx *

# List detailed file info
ls -l

Key Concepts:


Exercise 5: Interactive Script (Overwrite Confirmation)

Script: move_file.sh

#!/bin/bash

# Ask user for file name
read -p "Enter file name: " filename

# Ask user for destination
read -p "Enter destination folder path: " destination

# Check if file exists in destination
if [ -f "$destination/$filename" ]; then
    echo "File already exists in destination."

    # Ask for confirmation
    read -p "Overwrite? (y/n): " choice

    if [ "$choice" == "y" ]; then
        mv -f "$filename" "$destination/"
        echo "File overwritten successfully."
    else
        echo "Operation cancelled."
    fi
else
    mv "$filename" "$destination/"
    echo "File moved successfully."
fi

Explanation:


Key Exam Tips