2 added missing quotes; show a more robust and more portable way to strip the slash
source | link

I eventually used the following bash script:

for d in */ ; do
   tar -c -I lbzip2 -f $"${d::-1d%/}.tar.bz2bz2" $d"$d"
done

Some comments:

I eventually used the following bash script:

for d in */ ; do
   tar -c -I lbzip2 -f ${d::-1}.tar.bz2 $d
done

Some comments:

I eventually used the following bash script:

for d in */ ; do
   tar -c -I lbzip2 -f "${d%/}.tar.bz2" "$d"
done

Some comments:

  • for d in */ ; do loop through directories (How do I loop through only directories in bash?)
  • ${d%/} removes the trailing slash of the folder name (it would return $d unchanged if there was no trailing slash).
  • This creates an archive for both subdirectories of the current directories, and symbolic links in the current directory that point to an existing directory.
1
source | link

I eventually used the following bash script:

for d in */ ; do
   tar -c -I lbzip2 -f ${d::-1}.tar.bz2 $d
done

Some comments: