diff --git a/debian/docs/koha-foreach.xml b/debian/docs/koha-foreach.xml
index 4e844634e9e..ccf1ab563c5 100644
--- a/debian/docs/koha-foreach.xml
+++ b/debian/docs/koha-foreach.xml
@@ -23,7 +23,7 @@
- koha-foreach|
+ koha-foreach|
@@ -31,7 +31,20 @@
Run a command for each Koha instance. Takes the same arguments as koha-list.The string "__instancename__" is replaced in the argument list with the name of the Koha instance in each iteration.
-
+
+
+ Options
+
+
+
+
+ This option makes the command jump into the instance's home directory before running the required command. This prevents
+ directory traversal permissions issues.
+
+
+
+
+
See alsokoha-create-dirs(8)
diff --git a/debian/scripts/koha-foreach b/debian/scripts/koha-foreach
index 50f96c71dfa..47f92c5973d 100755
--- a/debian/scripts/koha-foreach
+++ b/debian/scripts/koha-foreach
@@ -28,10 +28,14 @@ else
exit 1
fi
+chdir="no"
+starting_dir=$(pwd)
+
listopts=""
while [ ! -z "$1" ]
do
case "$1" in
+ --chdir) chdir="yes";;
--email) listopts="$listopts --email";;
--noemail) listopts="$listopts --noemail";;
--enabled) listopts="$listopts --enabled";;
@@ -53,6 +57,13 @@ do
cmd=`echo "$@" | sed -e s/__instancename__/${name}/g`
if [ "${cmd}" != "" ]; then
+
+ # Change to the instance's home dir if required
+ [ "chdir" != "no" ] && eval cd ~$name"-koha"
+
koha-shell ${name} -c "${cmd}"
+
+ # Go back to the original dir if required
+ [ "chdir" != "no" ] && cd $starting_dir
fi
done