# Flatten folder? / Syncthing-Fork run custom script after folder sync completed

**URL:** https://forum.syncthing.net/t/flatten-folder-syncthing-fork-run-custom-script-after-folder-sync-completed/24225
**Category:** Android
**Created:** [April 22, 2025, 12:16pm UTC](https://forum.syncthing.net/t/flatten-folder-syncthing-fork-run-custom-script-after-folder-sync-completed/24225 "2025-04-22T12:16:36Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![testing123](https://forum.syncthing.net/letter_avatar_proxy/v4/letter/t/e19b73/32.png) [@testing123](https://forum.syncthing.net/u/testing123)
#### Post date: [April 22, 2025, 12:16pm UTC](https://forum.syncthing.net/t/flatten-folder-syncthing-fork-run-custom-script-after-folder-sync-completed/24225/1 "2025-04-22T12:16:36Z")

</div>

I have an iPhone and it syncs to both a server and an Android phone. The Android is set to receive-only, everything works flawlessly. I would like to set up some kind of automation so that the date folders from iOS get flattened only on Android, after being synced, not sure how to do that.

Do the files appear in the synced folder only after they are fully downloaded or does it also appear in a partial way (e.g. like torrents)?

If anyone has ideas on how to set up automatic flattening on a non-rooted Android phone, I’m all ears. If not, perhaps there is a thing that would be possible with Tasker or some other tool which can run a linux command like:

```plaintext
find "$TARGET" -mindepth 2 -type f -exec mv -n "{}" "$TARGET/" \;
find "$TARGET" -mindepth 1 -type d -empty -delete

```

---

<div class="post-metadata">

### Author: ![testing123](https://forum.syncthing.net/letter_avatar_proxy/v4/letter/t/e19b73/32.png) [@testing123](https://forum.syncthing.net/u/testing123)
#### Post date: [April 22, 2025, 3:08pm UTC](https://forum.syncthing.net/t/flatten-folder-syncthing-fork-run-custom-script-after-folder-sync-completed/24225/3 "2025-04-22T15:08:14Z")

</div>

Thank you, thinking of that, just did not want to go through the hassle since I’ve never used it, but it would be able to watch folders I guess. Another option is Macrodroid, which I suppose can run scripts.

But are the files in the synced folder available fully once they are present or do I need to wait for a while? I want to avoid moving partial files, not syre how Syncthing works behind the hood.

---

<div class="post-metadata">

### Author: ![gadget](https://forum.syncthing.net/letter_avatar_proxy/v4/letter/g/dec6dc/32.png) [@gadget](https://forum.syncthing.net/u/gadget)
#### Post date: [April 22, 2025, 5:33pm UTC](https://forum.syncthing.net/t/flatten-folder-syncthing-fork-run-custom-script-after-folder-sync-completed/24225/4 "2025-04-22T17:33:19Z")

</div>

> [@testing123](#):
>
> Thank you, thinking of that, just did not want to go through the hassle since I’ve never used it, but it would be able to watch folders I guess. Another option is Macrodroid, which I suppose can run scripts.

There are two potential downsides to flattening the directory tree:

- The Android phone will be shown as perpetually out-of-sync due to the local changes.
- Depending on the Android version, syncing to a single directory with a large number of files will slowly drag down sync speeds.

> [@](#):
>
> But are the files in the synced folder available fully once they are present or do I need to wait for a while? I want to avoid moving partial files, not syre how Syncthing works behind the hood.

Syncthing’s “[Understanding Synchronization](https://docs.syncthing.net/users/syncing.html)” page has all the details about temporary files during transfer and renaming in place.

---

<div class="post-metadata">

### Author: ![testing123](https://forum.syncthing.net/letter_avatar_proxy/v4/letter/t/e19b73/32.png) [@testing123](https://forum.syncthing.net/u/testing123)
#### Post date: [April 22, 2025, 10:56pm UTC](https://forum.syncthing.net/t/flatten-folder-syncthing-fork-run-custom-script-after-folder-sync-completed/24225/6 "2025-04-22T22:56:49Z")

</div>

```plaintext
The Android phone will be shown as perpetually out-of-sync due to the local changes.
Depending on the Android version, syncing to a single directory with a large number of files will slowly drag down sync speeds.

```

That is by design, the Android device will regularly be emptied. I kept a large folder in the previous implementations but it was pretty performant as-is.

```plaintext
Files are renamed when complete.

```

Are they renamed in the same folder? The documentation hints that this is the case, with \*.tmp files. The hidden attribute would be a quick selection if that is the case.

Sorry if I misused the forum formatting and quoting, getting used to it. Also autocorrect does not work for some reason.

---

<div class="post-metadata">

### Author: ![chaos](https://forum.syncthing.net/letter_avatar_proxy/v4/letter/c/9d8465/32.png) [@chaos](https://forum.syncthing.net/u/chaos)
#### Post date: [April 23, 2025, 1:40am UTC](https://forum.syncthing.net/t/flatten-folder-syncthing-fork-run-custom-script-after-folder-sync-completed/24225/7 "2025-04-23T01:40:41Z")

</div>

Files that are being sent by Syncthing get a .tmp extension, in the same folder, during that process. Once the file is fully synced and the blocks all match, it is renamed to the original filename.

---

<div class="post-metadata">

### Author: ![testing123](https://forum.syncthing.net/letter_avatar_proxy/v4/letter/t/e19b73/32.png) [@testing123](https://forum.syncthing.net/u/testing123)
#### Post date: [April 30, 2025, 7:36am UTC](https://forum.syncthing.net/t/flatten-folder-syncthing-fork-run-custom-script-after-folder-sync-completed/24225/8 "2025-04-30T07:36:45Z")

</div>

I’ve tried setting up a script using termux and inotify but the problem is that the termux script does not have the permissions to read/write the files. I assume this happens because SAF was used for the folder to be synced but termux has its own symlinks. Without root, no idea how to proceed with this. Is there any post-sync hook that I can use to run a script?

---

<div class="post-metadata">

### Author: ![testing123](https://forum.syncthing.net/letter_avatar_proxy/v4/letter/t/e19b73/32.png) [@testing123](https://forum.syncthing.net/u/testing123)
#### Post date: [April 30, 2025, 10:53am UTC](https://forum.syncthing.net/t/flatten-folder-syncthing-fork-run-custom-script-after-folder-sync-completed/24225/10 "2025-04-30T10:53:06Z")

</div>

But there is no way to chmod o+w on the files and folders, right? This would at least allow me to run the scripts. I will research some more, worst case I can perhaps use a termux folder for syncing.

---

<div class="post-metadata">

### Author: ![testing123](https://forum.syncthing.net/letter_avatar_proxy/v4/letter/t/e19b73/32.png) [@testing123](https://forum.syncthing.net/u/testing123)
#### Post date: [May 14, 2025, 9:36am UTC](https://forum.syncthing.net/t/flatten-folder-syncthing-fork-run-custom-script-after-folder-sync-completed/24225/13 "2025-05-14T09:36:27Z")

</div>

Wow, that was fast! Will try it and report back, thank you.

---

<div class="post-metadata">

### Author: ![testing123](https://forum.syncthing.net/letter_avatar_proxy/v4/letter/t/e19b73/32.png) [@testing123](https://forum.syncthing.net/u/testing123)
#### Post date: [May 14, 2025, 12:07pm UTC](https://forum.syncthing.net/t/flatten-folder-syncthing-fork-run-custom-script-after-folder-sync-completed/24225/14 "2025-05-14T12:07:30Z")

</div>

It seems to work, now I need to figure out why my script is not working 🙂

```plaintext
05-14 15:04:25.820 11872 11872 D RestApi : setRemoteCompletionInfo: Completed folder=[iOS]
05-14 15:04:25.822 11872 11872 D Util : runShellCommandGetOutput: cd "/storage/emulated/0/iOS/.stfolder/..";sh "/storage/emulated/0/iOS/.stfolder/flatten-folder-after-sync.sh" "sync_complete"
05-14 15:04:25.849 11872 11872 V Util : runScriptSet: Exec result []
05-14 15:04:25.852 11872 11872 D Util : runShellCommandGetOutput: cd "/storage/emulated/0/iOS/.stfolder/..";sh "/storage/emulated/0/iOS/.stfolder/demo1.sh" "sync_complete"
05-14 15:04:25.866 11872 11872 V Util : runScriptSet: Exec result [[INFO] Demo 1: param1=[sync_complete], pwd=[/storage/emulated/0/iOS]

```

---

<div class="post-metadata">

### Author: ![testing123](https://forum.syncthing.net/letter_avatar_proxy/v4/letter/t/e19b73/32.png) [@testing123](https://forum.syncthing.net/u/testing123)
#### Post date: [May 14, 2025, 12:48pm UTC](https://forum.syncthing.net/t/flatten-folder-syncthing-fork-run-custom-script-after-folder-sync-completed/24225/15 "2025-05-14T12:48:57Z")

</div>

Got it working, it was a noob problem, CRLF line endings in the script. Here’s the one I use for flattening the structure (flatten-folder-after-sync.sh):

```plaintext
#!/bin/sh
TARGET="./"

# Move files from all visible subdirectories to $TARGET
find "$TARGET" -mindepth 2 -type f ! -path '*/.*' -exec mv -n "{}" "$TARGET/" \;

# Delete only empty, non-hidden subdirectories
find "$TARGET" -mindepth 1 -type d -empty ! -path '*/.*' -delete

exit 0

```

---

<div class="post-metadata">

### Author: ![system](https://forum-uploads.syncthingcdn.net/original/2X/0/0b50e0a202b22ae6a67190759e8c868805b8ed9f.png) [@system](https://forum.syncthing.net/u/system)
#### Post date: [June 13, 2025, 12:56pm UTC](https://forum.syncthing.net/t/flatten-folder-syncthing-fork-run-custom-script-after-folder-sync-completed/24225/17 "2025-06-13T12:56:52Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
