TP 2
Intelligent File Organization – Linux Scripting Project
Université du Québec à Rimouski
Course: INF14107 – Architecture des Systèmes Informatiques
Assignment: Travail 2 – Linux Scripting
Topic: Organisation Intelligente des Fichiers
Solution - GitHub
🎯 Objective
Folders like Documents or Downloads often contain many files of different types: images, documents, audio, video, code, etc.
Organizing files by type improves visibility and makes searching much easier — but doing this manually is time-consuming.
Goal: Create a shell script that automates this file organization task.
📋 Part I: Basic File Organization Script (13 pts)
Write a Shell script that can be launched from the terminal to organize files by type into different folders. Also generate an information file containing a description of the organized folder.
✅ Required Tasks
-
Create a
files/directory containing sample files to organize:- Images:
.png,.jpg - Documents:
.odt,.txt - Code:
.py,.php,.cpp
(See Figure 1.1 for reference)
Place this folder in the same directory as your final script.
- Images:
-
Create a
.shscript file with:- A proper Shebang line (
#!/bin/bash) - Executable permissions (
chmod +x script.sh)
- A proper Shebang line (
-
Read the contents of the
files/directory and store filenames in an array using aforloop. -
Extract file extensions from each filename (hint: use
${file##*.}) -
Copy files from
files/into a new directory namedFiles_$USER(where$USERis the current username), created by the script. -
Filter and move files by extension into categorized subfolders:
Images: .png, .jpg → images/ Docs: .odt, .txt → docs/ Code: .py, .php, .cpp → code/ -
Generate an
info.txtfile containing:- Number of files in
images/ - Number of files in
docs/ - Number of files in
code/ - Total number of files
- Current date
- Username (
$USER)
- Number of files in
🖼️ Expected Output
| Figure 1.1 – Initial State | Figure 1.2 – Final State |
|---|---|
Mixed files in files/ | Organized subfolders + info.txt |
Assemble all steps into a single, functional shell script that achieves the stated objective.
🚀 Part II: Advanced Configurable Script (2 pts)
Create a second, more flexible script that:
- Accepts a custom directory path as input (the folder to organize)
- Uses a configuration file named
extension.txtto define file-type mappings
📄 Example extension.txt format:
Image: png, bmp, gif
Code: cs, php, cpp
Document: odt, txt, pdf
Audio: mp3, wav, flacThis allows the script to dynamically categorize files based on user-defined rules — making it reusable for any folder structure.
🛠️ Technical Requirements
- Language: Bash/Shell scripting
- Permissions: Script must be executable (
chmod +x) - Variables: Use
$USER, parameter expansion${file##*.}, arrays, loops - File operations:
mkdir,cp/mv,echo, redirection> - Portability: Should run on standard Linux terminals