diff options
author | Bill <bill@billserver.senders.io> | 2022-03-19 14:09:23 -0400 |
---|---|---|
committer | Bill <bill@billserver.senders.io> | 2022-03-19 14:09:23 -0400 |
commit | beb08521b1cb06aa2c282a6752c7b588326fa025 (patch) | |
tree | e0f5a653ace4bd7f6f212b17691b618ce4e20e80 /automv.sh | |
parent | c371488a184ce659bf28171a3fdde37f3ec390bf (diff) |
Improve logging and change default .d dir
Rather than looking at the environment varible otherwise use the local
automv.d directory - set it to something more centralized in the
environement e.g /usr/local/etc/automv.d/
Also the logs were unclear what was happening so extracted the find
query into a function to be able to run/rerun it a few times to improve
the existing logging. I wonder if there is a better way to print what is
happening in the xargs w/o repeating the move a bunch:
fn_mv {
echo "Moving: $1 to $2"
mv $1 $2
}
and call:
find ... | xargs -I'{}' fn_mv {} $target
Perhaps?
Diffstat (limited to 'automv.sh')
-rwxr-xr-x | automv.sh | 24 |
1 files changed, 20 insertions, 4 deletions
@@ -1,10 +1,15 @@ #!/usr/bin/env bash -set -e if [[ -n "$AUTOMV_DIR" ]]; then automvdir=$AUTOMV_DIR else - automvdir=$(dirname "$0")/automv.d + if [[ -d "/usr/local/etc/automv.d" ]]; then + automvdir=/usr/local/etc/automv.d + else + echo 'Missing automv.d location: please create confs in /usr/local/etc/automv.d + or supply a directory in $AUTOMV_DIR' + exit 1 + fi fi function extract { @@ -13,12 +18,23 @@ function extract { grep "^${prop}=.*$" $file | cut -d '=' -f2 } +function fn_find { + find $1 -type f \( -regex "${2}" ! -iname ".*" \) +} + function automv { local query=$1 local source=$2 local target=$3 - echo "Moving files in $source that match '$query' to $target" - find $source -type f \( -regex "${query}" ! -iname ".*" \) | xargs -I '{}' mv {} $target/ + + file_cnt=$(fn_find $source $query | wc -l) + if [[ $file_cnt > 0 ]]; then + echo "Moving $file_cnt files in $source that match '$query' to $target" + fn_find $source $query + fn_find $source $query | xargs -I '{}' mv {} $target/ + else + echo "No files matching '$query' in $source" + fi } echo "Executing automv functions from $automvdir" |