Skip to content

Shell snippets

September 29, 2023
December 8, 2014

tokozedg/sman: Command-line snippet manager

Watch files

Command Line – watch and fswatch » Linux Magazine
watch: execute a program periodically, showing output fullscreen | procps-ng Commands | Man Pages | ManKier
fswatch: Ask for notification when the contents of the specified files or directory hierarchies are modified. | fswatch Commands | Man Pages | ManKier

inotifywait

Home · rvoicilas/inotify-tools Wiki
linux - How to execute a command whenever a file changes? - Super User

#!/bin/bash

test_watch() {
  clear
  inotifywait --quiet --recursive --monitor --format "Changed: %w%f" \
    --event close_write --exclude '(\.sw|~|[[:digit:]]+|node_modules)' \
    . | bin/test;
  test_watch
}

test_watch

watchman

Watchman A file watching service | Watchman

seems complicated on Windows

watchr

bevry/watchr: Better file system watching for Node.js. Provides a normalised API the file watching APIs of different node versions, nested/recursive file and directory watching, and accurate detailed events for file/directory changes, deletions and creations.

chokidar

paulmillr/chokidar: A neat wrapper around node.js fs.watch / fs.watchFile / FSEvents

kimmobrunfeldt/chokidar-cli: Fast cross-platform cli utility to watch file system changes

gaze

shama/gaze: A globbing fs.watch wrapper built from the best parts of other fine watch libs.

doowb/watch-cli: Watch files and execute an npm script when files change.

loop files

while IFS= read -r -d '' file; do
  some command "$file"
done < <(find . -type f -name '*.csv' -print0)

test element in array

# test element in array
in_array()
{
    local hay needle=$1;
    shift;
    for hay in "$@"; do
        echo $hay
        [[ $hay == $needle ]] && return 0;
    done;
    return 1
}

test logic expression

Test()
{
    eval "$@" && echo "True" || echo "False";
}