shell snippet to rename filesystem tree search and replace
the original purpose if to change " " to "#" and back, for an easy tar and rpm
constaints are "no fancy" : ksh, and old version of read find
it works on linux AIX Solaris. yes it does.
net-credits to
http://www.askdavetaylor.com/does_prune_work_like_maxdepth_in_unix_find_on_aix.html
http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html
http://ubuntuforums.org/showthread.php?t=256691
#!/bin/ksh
#guy kastenbaum, replacearbo arbo path car1 car2
#whitespaces okay (not "'*$), ie: replacearbo "." " " "#"
function replacearbo
{
rnb_rootpath=$1
rnb_car1=$2
rnb_car2=$3
#maxdepth
rnb_maxdepth=$(find $rnb_rootpath -type d|sed -e 's:[^/]::g'|sort -r -u | head -n 1|wc -c)
rnb_currdepth=0
while ( [ $rnb_currdepth -le $rnb_maxdepth ] )
do #foreach depth (with prune, --maxdepth is linux only)
rnb_currdepth=$(expr $rnb_currdepth + 1)
find $rnb_rootpath -prune -name "*$rnb_car1*" | while read rnb_file1
do #foreach file containing rnb_car1
rnb_file2=$(echo "$rnb_file1"|sed -e "s/$rnb_car1/$rnb_car2/g")
if [ "$rnb_file1" != "$rnb_file2" ];then
if [ -e "$rnb_file2" ];then echo "ERR $rnb_file2 exists!";return 1;fi;
#move "$rnb_file1" "$rnb_file2"
mv "$rnb_file1" "$rnb_file2"
fi
done
rnb_rootpath="$rnb_rootpath/*"
done
return 0;
}
#TESTS:
mkdir -p t/bxcxd/axxbxc t/bxcxd/axbbxc t/bxcxd/axbbxc
touch t/bxcxd/axf t/bxcxd/axbbxc/kxf t/bxcxd/axbbxc/kxff
find t;
replacearbo t x " ";
find t;
replacearbo t " " "#_a_#";
find t;
replacearbo t "#_a_#" x;
find t;
rm -rf ./t/
t/bxcxd/axbbxc/kxf => t/b c d/a bb c/k f => t/b#_a_#c#_a_#d/a#_a_#bb#_a_#c/k#_a_#f => t/bxcxd/axbbxc/kxf
Aucun commentaire:
Enregistrer un commentaire