Auto copy files on folder creation

Nextcloud v33

OS Ubuntu 24

I have teamfolder with name US_Team, now when any user creates a subfolder inside it for example subfolder1, then files should copies from Documents/* to that Subfolder (i.e. US_Team/subfolder1/). I have achieved this by system script but problem is that occ scan command takes some times to complete.

Is there any app or or something so that I can achieve this goal via GUI? So we can skip that occ scan command?

@bb77 do you have any idea?

Not sure, to be honest. Maybe you could put something together with Flow, but I don’t have any experience with it.

With your current workflow, you could also try restricting the occ files:scan command to a specific path, which should make the scan faster: Using the occ command — Nextcloud latest Administration Manual latest documentation

example:

occ files:scan --all --home-only restricting local files path and ignoring external files

I just found out that there’s a specific command called groupfolders:scan that might be better suited for the OP’s use case.

@dolphinscorp See here: GitHub - nextcloud/groupfolders: 📁👩‍👩‍👧‍👦 Admin-configured folders shared by everyone in a group or team. https://github.com/nextcloud-releases/groupfolders · GitHub

occ groupfolders:scan <folder_id> → trigger a filescan for a Team folder

1 Like

Actually i am already using this

$OCC groupfolders:scan $FOLDER_ID >/dev/null 2>&1

But delay is around 18 seconds in showing files and folder in Nextcloud GuI

This is my full script

#!/bin/bash

while true; do
# Your actual script logic below
/usr/local/bin/nextcloud-folder-auto-setup.sh >> /var/log/nextcloud-folder-setup.log 2>&1

sleep 30

done
root@ubuntu-server:/usr/local/bin# cat nextcloud-folder-auto-setup2.sh
#!/bin/bash

while true; do
# Your actual script logic below
/usr/local/bin/nextcloud-folder-auto-setup-archive.sh >> /var/log/nextcloud-folder-setup-archive.log 2>&1

sleep 30

done
root@ubuntu-server:/usr/local/bin# cat nextcloud-folder-auto-setup.sh
#!/bin/bash

NEXTCLOUD_PATH=“/var/www/html/nextcloud”
ORDERS_PATH=“/var/www/html/data/__groupfolders/1”
TEMPLATE=“/var/www/html/data/admin/files/TemplatesCustom/TemplatesMain/.”
TEMPLATEUS=“/var/www/html/data/admin/files/TemplatesCustom/TemplatesForTeamUS/.”
TEMPLATECN=“/var/www/html/data/admin/files/TemplatesCustom/TemplatesForTeamCN/.”
OCC=“sudo -u www-data php $NEXTCLOUD_PATH/occ”
FOLDER_ID=1

cd “$NEXTCLOUD_PATH” || exit 1

for order in “$ORDERS_PATH”/*; do
[ -d “$order” ] || continue
base=$(basename “$order”)

if [ -d "$order/Team US" ] && [ -d "$order/Team CN" ]; then
    continue
fi

echo "Setting up structure in: $base"

# Create folders
mkdir -p "$order/Team US" "$order/Team CN"

# Copy templates
cp -a "$TEMPLATE" "$order/"
cp -a "$TEMPLATEUS" "$order/Team US/"
cp -a "$TEMPLATECN" "$order/Team CN/"

# Ownership
chown -R www-data:www-data "$order"

# Scan groupfolder so Nextcloud indexes the new content
echo "Scanning groupfolder..."
$OCC groupfolders:scan $FOLDER_ID >/dev/null 2>&1

# Wait until Team US and Team CN are visible to Nextcloud
echo "Waiting for Nextcloud to recognize new paths..."
ready=0
for i in {1..30}; do
    output=$($OCC groupfolders:permissions $FOLDER_ID --group "admin" "/$base/Team US" -- +read 2>&1)
    if [[ "$output" != *"Path not found in folder"* ]]; then
        # revert test permission properly after path exists
        $OCC groupfolders:permissions $FOLDER_ID --group "admin" "/$base/Team US" -- +read +write +create +delete +share >/dev/null 2>&1
        ready=1
        echo "Paths are now recognized."
        break
    fi
    sleep 1
done

if [ "$ready" -ne 1 ]; then
    echo "ERROR: Nextcloud still does not recognize /$base/Team US after waiting. Skipping permissions for $base"
    continue
fi

# Apply permissions
# US Team
$OCC groupfolders:permissions $FOLDER_ID --group "US Team" "/$base" -- +read -delete +write +create +share
$OCC groupfolders:permissions $FOLDER_ID --group "US Team" "/$base/Team US" -- +read +delete +write +create +share
$OCC groupfolders:permissions $FOLDER_ID --group "US Team" "/$base/Team CN" -- -read

# CN Team
$OCC groupfolders:permissions $FOLDER_ID --group "CN Team" "/$base" -- +read +write +create +share -delete
$OCC groupfolders:permissions $FOLDER_ID --group "CN Team" "/$base/Team CN" -- +read +delete +write +create +share
$OCC groupfolders:permissions $FOLDER_ID --group "CN Team" "/$base/Team US" -- -read

# Admin
$OCC groupfolders:permissions $FOLDER_ID --group "admin" "/$base" -- +read +write +create +delete +share
$OCC groupfolders:permissions $FOLDER_ID --group "admin" "/$base/Team US" -- +read +write +create +delete +share
$OCC groupfolders:permissions $FOLDER_ID --group "admin" "/$base/Team CN" -- +read +write +create +delete +share

echo "Completed: $base"

done

I have been asked to zero the delay, current delay is 18 seconds around to display files in nextcloud gui