Take a deep breath and solve the problems one at a time, striving for simple solutions.
First, everything we need to do happens under the directory /a/2015-08-17
. So let's change to that directory.
cd /a/2015-08-17
Now we need to create a bunch of symbolic links based on existing directories. (I assume that the directories already exist since you don't explain how to determine what directories to create.) The basic command to do something on all subdirectories of the current directory is
find . -type d …
What we need to do is create a symbolic link based on the directory depth. The name of the symbolic link is the base name of the directory plus .zip
at the end (except that it's a.zip
at the top), and the target is calculated from the depth. Let's do the link name first.
We're going to need to do some calculations on each directory found by find
, so we need to invoke a shell.
find . -type d -exec sh -c '???' {} \;
The path to the directory (starting with .
) is available in the script as $0
. If $0
is just .
, we need to create a.zip
.
find . -type d -exec sh -c '
if [ "$0" = "." ]; then ln -s 1stdepth.zip a.zip; fi
' {} \;
Otherwise we need to extract the base name of the directory. That can be done with the shell parameter expansion construct ${0##*/}
(value of $0
minus the longest prefix ending in a /
):
find . -type d -exec sh -c '
if [ "$0" = "." ]; then
ln -s 1stdepth.zip a.zip
else
ln -s ???depth.zip "$0/${0##*/}.zip"
fi
' {} \;
Now we need to calculate the depth. This is the number of slashes in $0
, plus one since the root has depth 1. One way to count the number of slashes is to remove all other characters and count the length of what remains.
find . -type d -exec sh -c '
depth=$(printf %s "$0" | tr -dc / | wc -c)
if [ "$0" = "." ]; then
ln -s 1stdepth.zip a.zip
else
ln -s $((depth+1))???depth.zip "$0/${0##*/}.zip"
fi
' {} \;
Almost there; we need to calculate the suffix to get 1st, 2nd, etc.
find . -type d -exec sh -c '
depth=$(($(printf %s "$0" | tr -dc / | wc -c) + 1))
if [ "$0" = "." ]; then
ln -s 1stdepth.zip a.zip
else
case $depth in
*1?) suffix=th;;
*1) suffix=st;;
*2) suffix=nd;;
*3) suffix=rd;;
*) suffix=th;;
esac
ln -s "${depth}${suffix}depth.zip" "$0/${0##*/}.zip"
fi
' {} \;