From 904edf910d499ff14fc2657e3136a529cb01d4d2 Mon Sep 17 00:00:00 2001 From: desktop Date: Sun, 29 Oct 2023 20:09:39 +0300 Subject: [PATCH] bash : add recursive function. --- .README.md | 5 ++++ .../linux/config/bash/module/recursive.sh | 30 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 document/linux/config/bash/module/recursive.sh diff --git a/.README.md b/.README.md index 33186e1..d0709b4 100644 --- a/.README.md +++ b/.README.md @@ -538,6 +538,11 @@ Command|Description ---|--- `ps [PROGRAM]`|Show process info for matching [PROGRAM] name. +## Recursive. +Command|Description +---|--- +`recursive [COMMAND]`|Cd into every directory recursively and run specified command in each dir. + ## Shopt. Shopt|Description diff --git a/document/linux/config/bash/module/recursive.sh b/document/linux/config/bash/module/recursive.sh new file mode 100644 index 0000000..5ea19bc --- /dev/null +++ b/document/linux/config/bash/module/recursive.sh @@ -0,0 +1,30 @@ +# run something recursively over all directories. +# usage: recursive [COMMAND] +recursive() +{ + local current="${PWD}" + local dirs=$(find -type d) + local total=$(echo ${dirs} | wc -w) + local count=0 + + for dir in ${dirs}; do + # increment counter. + ((count++)) + + # cd into the next dir. + cd "${current}" + cd "${dir}" + + # echo status. + echo -e "${color_blue}[${count}/${total}] recursive dir: ${dir}${color_default}" + + # run command. + $* + done + + # return back on complete. + cd "${current}" +} + +# autocomplete. +complete -F _autocomplete_nested recursive