regex - Shell script to rename multiple files from their parent folders -
i'm looking script below structure:
before :
/description/testcvin/opencvin/namecv/..... /description/blacvin/baka/namecv_hubala/...... /description/cvintere/oldcvimg/namecv_add/.....
after:
/description/testaplcvin/openaplcvin/nameaplcv/..... /description/blaapcvlin/baka/nameaplcv_hubala/...... /description/aplcvintere/oldaplcvimg/nameaplcv_add/.....
i want rename " cv or cv or cv " >> "aplcv or aplcv or aplcv" in folder regular expression...
my script like:
#!/bin/sh printf "input directory path: -> " read dir cd "$dir" filecase=$(find . -iname "*cv*") last_dir_name="" fdir in $filecase if [[ -d $fdir ]]; last_dir_name=$fdir fi file=$(echo $fdir | sed -e "s/\([cc][vv]\)/arpl\1/g") echo "la file $file" if ([[ -f $fdir ]] && [[ "$fdir" =~ "$last_dir_name" ]]); filecase=$(find . -iname "*cv*") tmp=$(echo $last_dir_name | sed -e "s/\([cc][vv]\)/arpl\1/g") fdir=$(echo $fdir | sed -e 's|'$last_dir_name'|'$tmp'|g') fi mv -- "$fdir" "$file" done
but throws error ..:(
how write rename files according folder names?
you can this
#!/bin/sh printf "input directory path: -> " read dir cd "$dir" myarray=$(find . -iname "*cv*" ) touch "tmpfile" fdir in $myarray echo "$fdir" >> "tmpfile" done myarray=$(tac "tmpfile") fdir in $myarray cd "$fdir" prev=$(cd -) base=$(basename $fdir) cd .. ndir=$(echo "$base" | sed -e "s/\([cc][vv]\)/arpl\1/g") mv "$base" "$ndir" cd $prev done rm -f "tmpfile"
also 1 issue think tac
command not included in mac os x.instead tac
use tail -r
myarray=$(tail -r "tmpfile")
Comments
Post a Comment