{"id":20560303,"url":"https://github.com/andreaskoch/go-fswatch","last_synced_at":"2025-07-14T09:07:54.643Z","repository":{"id":9864270,"uuid":"11862297","full_name":"andreaskoch/go-fswatch","owner":"andreaskoch","description":"fswatch is a go library for monitoring filesystem changes that does not depend on inotify","archived":false,"fork":false,"pushed_at":"2023-01-08T15:48:42.000Z","size":212,"stargazers_count":64,"open_issues_count":2,"forks_count":8,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-07T08:21:19.143Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/andreaskoch.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-08-03T11:38:09.000Z","updated_at":"2025-03-18T02:59:01.000Z","dependencies_parsed_at":"2023-01-13T15:36:21.348Z","dependency_job_id":null,"html_url":"https://github.com/andreaskoch/go-fswatch","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/andreaskoch/go-fswatch","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreaskoch%2Fgo-fswatch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreaskoch%2Fgo-fswatch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreaskoch%2Fgo-fswatch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreaskoch%2Fgo-fswatch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andreaskoch","download_url":"https://codeload.github.com/andreaskoch/go-fswatch/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreaskoch%2Fgo-fswatch/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265265514,"owners_count":23737059,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-16T03:54:11.027Z","updated_at":"2025-07-14T09:07:54.591Z","avatar_url":"https://github.com/andreaskoch.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# fswatch\n\nfswatch is a go library for recursively watching file system changes to **does not** depend on inotify and therefore is not limit by the ulimit of your operating system.\n\n## Motivation\n\nWhy not use [inotify](http://en.wikipedia.org/wiki/Inotify)? Even though there are great libraries like [fsnotify](https://github.com/howeyc/fsnotify) that offer cross platform file system change notifications - the approach breaks when you want to watch a lot of files or folder.\n\nFor example the default ulimit for Mac OS is set to 512. If you need to watch more files you have to increase the ulimit for open files per process. And this sucks.\n\n## Usage\n\n### Watching a single file\n\nIf you want to watch a single file use the `NewFileWatcher` function to create a new file watcher:\n\n```go\ngo func() {\n\tcheckIntervalInSeconds := 2\n\tfileWatcher := fswatch.NewFileWatcher(\"Some-file\", checkIntervalInSeconds).Start()\n\n\tfor fileWatcher.IsRunning() {\n\n\t\tselect {\n\t\tcase \u003c-fileWatcher.Modified:\n\n\t\t\tgo func() {\n\t\t\t\t// file changed. do something.\n\t\t\t}()\n\n\t\tcase \u003c-fileWatcher.Moved:\n\n\t\t\tgo func() {\n\t\t\t\t// file moved. do something.\n\t\t\t}()\n\t\t}\n\n\t}\n}()\n```\n\n### Watching a folder\n\nTo watch a whole folder (and all of its child directories) for new, modified or deleted files you can use the `NewFolderWatcher` function.\n\nParameters:\n\n1. The path to the directory you want to monitor\n2. A flag indicating whether the folder shall be watched recursively\n3. An expression which decides which files are skipped\n4. The check interval in seconds (1 - n seconds)\n\n\n```go\ngo func() {\n\n\trecurse := true // include all sub directories\n\n\tskipDotFilesAndFolders := func(path string) bool {\n\t\treturn strings.HasPrefix(filepath.Base(path), \".\")\n\t}\n\n\tcheckIntervalInSeconds := 2\n\n\tfolderWatcher := fswatch.NewFolderWatcher(\n\t\t\"some-directory\",\n\t\trecurse,\n\t\tskipDotFilesAndFolders,\n\t\tcheckIntervalInSeconds\n\t)\n\t\n\tfolderWatcher.Start()\n\n\tfor folderWatcher.IsRunning() {\n\n\t\tselect {\n\n\t\tcase \u003c-folderWatcher.Modified():\n\t\t\tfmt.Println(\"New or modified items detected\")\n\n\t\tcase \u003c-folderWatcher.Moved():\n\t\t\tfmt.Println(\"Items have been moved\")\n\n\t\tcase changes := \u003c-folderWatcher.ChangeDetails():\n\n\t\t\tfmt.Printf(\"%s\\n\", changes.String())\n\t\t\tfmt.Printf(\"New: %#v\\n\", changes.New())\n\t\t\tfmt.Printf(\"Modified: %#v\\n\", changes.Modified())\n\t\t\tfmt.Printf(\"Moved: %#v\\n\", changes.Moved())\n\n\t\t}\n\t}\n\n}()\n```\n## go-fswatch in action\n\nYou can see go-fswatch in action in the **live-reload** feature of my [markdown webserver \"allmark\"](https://allmark.io/).\n\nSee:  [github.com/andreaskoch/allmark/blob/master/src/allmark.io/modules/dataaccess/filesystem/watcher.go](https://github.com/andreaskoch/allmark/blob/master/src/allmark.io/modules/dataaccess/filesystem/watcher.go)\n\nI would still prefer using inotify, but go-fswatch has been doing it's job in allmark pretty well and works easily with relatively large folder structures.\n\n## Build Status\n\n[![Build Status](https://travis-ci.org/andreaskoch/go-fswatch.png?branch=master)](https://travis-ci.org/andreaskoch/go-fswatch)\n\n## Contribute\n\nAll contributions are welcome. Especially if you have an idea\n\n- how to reliably increase the limit for the maximum number of open files from within an application so we can use inotify for large folder structures.\n- how to overcome the limitations of inotify without having to resort to checking the files for changes over and over again\n- or how to make the existing code more efficient\n\nplease send me a message or a pull request.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandreaskoch%2Fgo-fswatch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandreaskoch%2Fgo-fswatch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandreaskoch%2Fgo-fswatch/lists"}