Skip to main content
Automatic Folder Mirroring In Linux

Automatic Folder Mirroring In Linux

·2 mins·
Author
Gabriel Baltazart
Undergraduate Student at the University of Alberta
Table of Contents

Introduction
#

Mirroring the contents of two folders in Linux has many different use cases. In my case, I am looking for a way to automatically upload screenshots captured by GNOME screenshot to my cloud storage for easy access on other devices.

Steps
#

The first step is to download the inotify-tools

sudo pacman -S inotify-tools

As an example I will the GNOME screenshots folder ~/Pictures/screenshots will be mirrored to ~/Pictures/Mirror. We will write a script that uses inotify-tools to move new files automatically. In my case I placed the script named screenshot-watcher.sh at ~/.local/bin.

#!/usr/bin/env bash

SRC_DIR="$HOME/Pictures/Screenshots"
DEST_DIR="$HOME/pictures/Mirror"

inotifywait -m -e close_write --format "%f" "$SRC_DIR" | while read -r FILE; do
	cp "$SRC_DIR/$FILE" "$DEST_DIR/"
	notify-send "Screenshot copied" "$FILE has been mirrored!" # optional line to send a notification when script runs
done
Note

This script copies files from the original folder to the mirroed folder, you could replace cp with mv to insead move files

Ensure the script is executable with

sudo chmod +x ~/.local/bin/screenshot-watcher.sh

Then to have the script run automatically we will create a service. First create a directory at ~/.config/systemd/user

mkdir -p ~/.config/systemd/user

then in that directory create the service screenshot-watcher.service

[Unit]
Description=Automatically copy new screenshots to another folder

[Service]
ExecStart=%h/.local/bin/screenshot-watcher.sh
Restart=always
RestartSec=5

[Install]
WantedBy=default.target

We can then enable our service with the following set of commands

sudo systemctl --usr deamon-reload
sudo systemctl --user enable --now screenshot-watcher.service

Then you can check if it is running with

sudo systemctl --usr status screenshot-watcher.service

You should see the output

● screenshot-watcher.service - Automatically copy new screenshots to another folder
     Loaded: loaded (/home/gabriel/.config/systemd/user/screenshot-watcher.service; enabled; preset: enabled
)
     Active: active (running) since Fri 2025-11-14 02:08:04 MST; 11h ago
 Invocation: 3c2b94c22aa84927b20bfc6daec1363c
   Main PID: 2855 (bash)
      Tasks: 3 (limit: 37794)
     Memory: 1.8M (peak: 3.9M)
        CPU: 54ms
     CGroup: /user.slice/user-1000.slice/user@1000.service/app.slice/screenshot-watcher.service
             ├─2855 bash /home/gabriel/.local/bin/screenshot-watcher.sh
             ├─2899 inotifywait -m -e close_write --format %f /home/gabriel/Pictures/Screenshots
             └─2900 bash /home/gabriel/.local/bin/screenshot-watcher.sh

Nov 14 02:08:04 Polaris systemd[2846]: Started Automatically copy new screenshots to another folder.
Nov 14 02:08:04 Polaris screenshot-watcher.sh[2899]: Setting up watches.
Nov 14 02:08:04 Polaris screenshot-watcher.sh[2899]: Watches established.

Then just test the script by adding a new file to the original folder. If everything is setup correctly you should see a notification and the file with be copied over to the mirror folder.