How to use the command fswatch (with examples)
fswatch is a cross-platform file change monitor that allows users to monitor file changes in real-time. It can be particularly useful for running specific commands when certain events occur, tracking file changes, and filtering events based on event type.
Use case 1: Run a Bash command on file creation, update, or deletion
Code:
fswatch path/to/file | xargs -n 1 bash_command
Motivation: Running a Bash command on file creation, update, or deletion can be extremely helpful in automating tasks or triggering actions based on file changes. By utilizing fswatch and xargs together, users can easily monitor a specific file and execute a command whenever the file is modified or changed.
Explanation:
fswatch
: The main command that monitors file changes.path/to/file
: The path to the file that you want to monitor for changes.|
: A pipe symbol used to redirect the output of the fswatch command to the following command.xargs
: A command that takes the output from a previous command and uses it as arguments for another command.-n 1
: Specifies that the command following xargs should be run once for each argument.bash_command
: The Bash command that you want to execute when changes occur.
Example output:
If the file path/to/file
is updated, the bash_command
will be executed.
Use case 2: Watch one or more files and/or directories
Code:
fswatch path/to/file path/to/directory path/to/another_directory/**/*.js | xargs -n 1 bash_command
Motivation: Monitoring multiple files and directories simultaneously is often necessary, especially in larger projects. This use case allows users to track changes across multiple sources and take appropriate actions based on those changes.
Explanation:
fswatch
: The main command that monitors file changes.path/to/file
: The path to the file you want to monitor for changes.path/to/directory
: The path to the directory you want to monitor for changes.path/to/another_directory/**/*.js
: The path to the directory and all its subdirectories, matching any JavaScript file.|
: A pipe symbol used to redirect the output of the fswatch command to the following command.xargs
: A command that takes the output from a previous command and uses it as arguments for another command.-n 1
: Specifies that the command following xargs should be run once for each argument.bash_command
: The Bash command that you want to execute when changes occur.
Example output:
If any changes or modifications occur in path/to/file
, path/to/directory
, or any JavaScript file in path/to/another_directory
, the bash_command
will be executed accordingly.
Use case 3: Print the absolute paths of the changed files
Code:
fswatch path/to/directory | xargs -n 1 -I {} echo {}
Motivation: Sometimes it can be helpful to simply see the paths of the files that have changed. This use case allows users to quickly identify which files have been modified without performing any additional actions.
Explanation:
fswatch
: The main command that monitors file changes.path/to/directory
: The path to the directory you want to monitor for changes.|
: A pipe symbol used to redirect the output of the fswatch command to the following command.xargs
: A command that takes the output from a previous command and uses it as arguments for another command.-n 1
: Specifies that the command following xargs should be run once for each argument.-I {}
: Specifies a placeholder{}
which is replaced by the output of the fswatch command for each iteration.echo {}
: The command that prints the path of the changed file to the terminal.
Example output:
If a file within path/to/directory
is modified, the absolute path of that file will be printed to the terminal.
Use case 4: Filter by event type
Code:
fswatch --event Updated|Deleted|Created path/to/directory | xargs -n 1 bash_command
Motivation: In certain scenarios, users may only be interested in specific event types. Filtering by event type can help narrow down the actions triggered by monitoring file changes.
Explanation:
fswatch
: The main command that monitors file changes.--event Updated|Deleted|Created
: Specifies the event types to be monitored. In this example, we are monitoring events such as file updates, deletions, and creations.path/to/directory
: The path to the directory you want to monitor for changes.|
: A pipe symbol used to redirect the output of the fswatch command to the following command.xargs
: A command that takes the output from a previous command and uses it as arguments for another command.-n 1
: Specifies that the command following xargs should be run once for each argument.bash_command
: The Bash command that you want to execute when changes occur.
Example output:
If any file is updated, deleted, or created within path/to/directory
, the bash_command
will be executed accordingly.
Conclusion:
fswatch is a versatile tool that provides users with the ability to monitor file changes and take appropriate actions based on those changes. By combining fswatch with other commands like xargs and echo, users can automate tasks, track file modifications, and filter events based on specific criteria. Whether you need to watch files and directories, execute commands on file changes, or filter events by type, fswatch provides a flexible solution for monitoring file systems effectively.