2023-11-25 19:21:41 +03:00
|
|
|
# Run something recursively over all directories.
|
|
|
|
# Usage: recursive <COMMAND>
|
2023-12-07 01:44:42 +03:00
|
|
|
function recursive() {
|
2023-12-07 04:02:47 +03:00
|
|
|
if [[ "${*}" = "" ]]; then
|
|
|
|
help recursive
|
|
|
|
return 2
|
|
|
|
fi
|
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
local IFS=$'\n'
|
|
|
|
local current="${PWD}"
|
|
|
|
local dirs=$(find -type d)
|
|
|
|
local total=$(find -type d | wc -l) # TODO: don't call find twice. won't work with "echo ${dirs}".
|
|
|
|
local count=0
|
|
|
|
local failed=0
|
2023-10-29 20:09:39 +03:00
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
for dir in ${dirs}; do
|
|
|
|
# increment counter.
|
|
|
|
((count++))
|
2023-10-29 20:09:39 +03:00
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
# cd into the next dir.
|
|
|
|
cd "${current}" || failed=${?}
|
|
|
|
cd "${dir}" || failed=${?}
|
2023-10-29 20:09:39 +03:00
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
# echo status.
|
|
|
|
echo -e "${color_bblue}[${count}/${total}] ${dir}${color_default}"
|
2023-10-29 20:09:39 +03:00
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
# run command.
|
2023-12-07 04:02:47 +03:00
|
|
|
${*} || failed=${?}
|
2023-12-17 21:15:50 +03:00
|
|
|
|
|
|
|
# Add newline if not the last one.
|
|
|
|
[[ "${count}" = "${total}" ]] || echo
|
2023-12-05 21:50:45 +03:00
|
|
|
done
|
2023-10-29 20:09:39 +03:00
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
# return back on complete.
|
|
|
|
cd "${current}" || failed=${?}
|
2023-11-25 19:21:41 +03:00
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
return ${failed}
|
2023-10-29 20:09:39 +03:00
|
|
|
}
|
|
|
|
|
2023-12-07 04:02:47 +03:00
|
|
|
# Run something recursively over directories of 1 depth (excluding current dir).
|
2023-11-25 19:21:41 +03:00
|
|
|
# Usage: recursive1 <COMMAND>
|
2023-12-07 01:44:42 +03:00
|
|
|
function recursive1() {
|
2023-12-07 04:02:47 +03:00
|
|
|
if [[ "${*}" = "" ]]; then
|
|
|
|
help recursive1
|
|
|
|
return 2
|
|
|
|
fi
|
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
local IFS=$'\n'
|
|
|
|
local current="${PWD}"
|
|
|
|
local dirs=$(find -mindepth 1 -maxdepth 1 -type d)
|
|
|
|
local total=$(find -mindepth 1 -maxdepth 1 -type d | wc -l) # TODO: don't call find twice. won't work with "echo ${dirs}".
|
|
|
|
local count=0
|
|
|
|
local failed=0
|
2023-11-18 11:09:47 +03:00
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
for dir in ${dirs}; do
|
|
|
|
# increment counter.
|
|
|
|
((count++))
|
2023-11-18 11:09:47 +03:00
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
# cd into the next dir.
|
|
|
|
cd "${current}"
|
|
|
|
cd "${dir}"
|
2023-11-18 11:09:47 +03:00
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
# echo status.
|
|
|
|
echo -e "${color_bblue}[${count}/${total}] ${dir}${color_default}"
|
2023-11-18 11:09:47 +03:00
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
# run command.
|
2023-12-07 04:02:47 +03:00
|
|
|
${*} || failed=${?}
|
2023-12-17 21:15:50 +03:00
|
|
|
|
|
|
|
# Add newline if not the last one.
|
|
|
|
[[ "${count}" = "${total}" ]] || echo
|
2023-12-05 21:50:45 +03:00
|
|
|
done
|
2023-11-18 11:09:47 +03:00
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
# return back on complete.
|
|
|
|
cd "${current}"
|
2023-11-25 19:21:41 +03:00
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
return ${failed}
|
2023-11-18 11:09:47 +03:00
|
|
|
}
|
|
|
|
|
2023-10-29 20:09:39 +03:00
|
|
|
# autocomplete.
|
2023-11-18 11:09:47 +03:00
|
|
|
complete -F _autocomplete_nested recursive recursive1
|