This repository has been archived on 2024-03-04. You can view files and clone it, but cannot push or open issues or pull requests.
linux/.config/bash/module/recursive.sh

66 lines
1.4 KiB
Bash
Raw Normal View History

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-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.
$* || failed=${?}
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-11-25 19:21:41 +03:00
# Run something recursively over all directories.
# Usage: recursive1 <COMMAND>
2023-12-05 23:56:04 +03:00
# TODO: create generic function.
2023-12-07 01:44:42 +03:00
function recursive1() {
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.
$* || failed=${?}
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