Reputation: 2337
I am trying to run a bash script which will run on the current directory, find files and do some operation on them and when a directory is found do the same for it and over and over again.
The thing that got me confused was my operation, it is supposed to convert each graphic file, resize it, and put it in a parallel directory structure, doing that for four parallel directories, meaning it will mimic the original directory structure creating folders as it goes.
The problem is it doesn't work and also it keeps recursing into the newly created directories..
Can you help make it right?
recurse() {
for i in "$1"/*;do
if [ -d "$i" ];then
echo "dir: $i"
mkdir "res-ldpi/$i"
mkdir "res-hdpi/$i"
mkdir "res-mdpi/$i"
mkdir "res-xhdpi/$i"
recurse "$i";
elif [ -f "$i" ]; then
convert ./"$i" -resize 38% -unsharp 0x1 res-ldpi/"$i"
fi
done
}
recurse .
Upvotes: 0
Views: 94
Reputation: 62379
Perhaps something along these lines (not tested...):
while read fn
do
dn=$(dirname "${fn}")
bn=$(basename "${fn}")
[[ -d "res-ldpi/${dn}" ]] || mkdir -p "res-ldpi/${dn}"
convert "${fn}" -resize 38% -unsharp 0x1 "res-ldpi/${dn}/${bn}"
done < <(find "${1}"/. -type f -print)
The idea is to 1) use find
to just give you the name of every file in the directory and its subdirectories, 2) split that into a directory and file name, 3) check that the destination directory exists and create it if necessary, then 4) run convert
as desired.
That didn't bother with the res-hdpi
, res-mdpi
and res-xhdpi
directories, since your sample code didn't use them anyway - it shouldn't be too difficult to figure out how to add those in...
Upvotes: 1