531.2 Shell scripting in web dev (creating files, searching logs)

Learn how simple shell scripts can automate common back-end tasks like managing files and processing logs.

Overview

In this topic, we explore how shell scripting is used in web development to automate server-side tasks. Students learn to write basic scripts that create files, manage directories, and search server logs for specific information. These skills are particularly useful for deploying web applications, managing user uploads, and monitoring server health.

Targets

In this topic, students learn to:

  • Write simple shell scripts using common commands

  • Create, move, and delete files and directories

  • Search server logs using tools like grep

  • Understand the role of shell scripts in web deployment and monitoring

  • Recognise how scripting supports automation and DevOps

Syllabus references

Programming for the web

Designing web applications

  • Develop a web application using an appropriate scripting language with shell scripts to make files and directories, and searching for text in a text file

What is shell scripting?

A shell script is a plain text file that contains a sequence of commands to be executed by the shell (such as bash or zsh). These scripts are widely used for:

  • Automating server setup

  • Managing deployments

  • Handling backups and logs

  • Running scheduled tasks (via cron jobs)

Shell scripts are written using basic Unix commands and run on most web servers.

Creating files and directories

Basic commands used in shell scripts:

mkdir logs         # Create a directory called logs
touch report.txt   # Create an empty file
mv file.txt /logs  # Move file to another folder
rm oldfile.txt     # Delete a file

Scripts can include these commands in sequence to automate repetitive tasks.

Example: Creating a backup folder

#!/bin/bash
mkdir -p backups/$(date +%F)
echo "Backup folder created on $(date)"

This script creates a dated backup folder.

Searching logs

Web applications often produce log files that track user activity, errors, or requests. Shell commands can be used to search and extract useful information.

Example: Finding all failed login attempts

grep "Failed login" auth.log

Example: Counting requests from an IP address

grep "192.168.0.1" access.log | wc -l

Shell scripts automate back-end tasks like file creation, log processing, and cleanup, helping maintain and monitor web applications efficiently.

When is shell scripting used in web dev?

  • During deployment – to set up file structure, environment variables, and permissions

  • For backups – to copy and store database or user files

  • For log rotation – to archive old logs and clean up storage

  • For scheduled tasks – like sending emails or generating reports at set times

Summary

Shell scripting allows developers to automate back-end workflows that would otherwise be manual and time-consuming. Creating files, managing logs, and maintaining server directories are common tasks made easier by scripting, especially when deploying or maintaining web applications.

Last updated

Was this helpful?