summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBill <bill@billserver.senders.io>2022-03-19 11:14:03 -0400
committerBill <bill@billserver.senders.io>2022-03-19 11:14:03 -0400
commitc371488a184ce659bf28171a3fdde37f3ec390bf (patch)
tree62b97828d3b3f512d0154a6c3acc47c9d2b3bfa4
Create automv script
Automv script will allow a cronjob to easily move files from one dir to another based on a find regex query. You can dynamically set what files to look for and where using .conf files in the automv.d folder present next to the script - or by setting its location in AUTOMV_DIR. Dev note: I specifically avoided using $HOME/.local/automv.d/ because my specific use case the user whose cron this will be in doesn't have a home directory. An improvement could be expanding the smart defaults to doing something like: if [[ -n "$HOME" ]]; then if [[ -d $HOME/.local/automv.d ]]; then # use that else # etc else # etc But I don't like long bash if/else nesting so I figured just set a local default otherwise override in the env
-rw-r--r--.gitignore1
-rwxr-xr-xautomv.sh36
-rw-r--r--readme.txt48
3 files changed, 85 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..2fead5c
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+automv.d
diff --git a/automv.sh b/automv.sh
new file mode 100755
index 0000000..16d7683
--- /dev/null
+++ b/automv.sh
@@ -0,0 +1,36 @@
+#!/usr/bin/env bash
+set -e
+
+if [[ -n "$AUTOMV_DIR" ]]; then
+ automvdir=$AUTOMV_DIR
+else
+ automvdir=$(dirname "$0")/automv.d
+fi
+
+function extract {
+ local file=$1
+ local prop=$2
+ grep "^${prop}=.*$" $file | cut -d '=' -f2
+}
+
+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/
+}
+
+echo "Executing automv functions from $automvdir"
+for file in $automvdir/*.conf
+do
+ echo "found $file"
+ if [[ -f $file ]]; then
+ # Extract file configs
+ query=$(extract $file "query")
+ sourced=$(extract $file "source")
+ target=$(extract $file "target")
+
+ automv $query $sourced $target
+ fi
+done
diff --git a/readme.txt b/readme.txt
new file mode 100644
index 0000000..05922ef
--- /dev/null
+++ b/readme.txt
@@ -0,0 +1,48 @@
+automv.sh
+=========
+
+Usage:
+ ./automv.sh
+
+Prereqs:
+ Requires bash with the tools:
+ grep
+ xargs
+ find
+
+Setup:
+ the script searches the directory:
+ ./automv.d/
+ for any .conf files
+
+Overriding the automv.d location:
+ If you want to set a different automv.d location setup the:
+ AUTOMV_DIR
+ environment variable
+
+automv.d/ conf syntax
+=====================
+
+The conf files use the following syntax:
+
+# comments
+query=<query>
+ this is the query you want to use in 'find -regex'
+source=<source-dir>
+ this is the directory you want to search in
+target=<target-dir>
+ this is the target directory to move the found files to
+
+We explicitly parse out those 3 properties (see extract function in script)
+
+!!! Note - do not quote wrap anything - the script expects unwrapped values
+
+example conf
+============
+
+# jpegmv.conf
+# moves jpeg files downloaded to pictures
+query=.*\.jp[e]?g
+source=/home/user/Downloads/
+target=/home/user/Pictures/
+