bash : add recursive function.
This commit is contained in:
parent
3e59bca232
commit
904edf910d
|
@ -538,6 +538,11 @@ Command|Description
|
||||||
---|---
|
---|---
|
||||||
`ps [PROGRAM]`|Show process info for matching [PROGRAM] name.
|
`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.
|
||||||
|
|
||||||
Shopt|Description
|
Shopt|Description
|
||||||
|
|
30
document/linux/config/bash/module/recursive.sh
Normal file
30
document/linux/config/bash/module/recursive.sh
Normal file
|
@ -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
|
Reference in a new issue