for dir in A/B/C/*/; do
name=$(basename "$dir")
tar -cv -f "$name.tar" -C A/B/C "$name"
done
This would create an archive for each individual (non-hidden) subdirectory under A/B/C
. The archives would be created in the current directory.
The -C
option makes tar
set the working directory for the following files ($name
).
You could use -C "$(dirname "$dir")"
in place of -C A/B/C
for slightly more generic code.
To create the archives under the C
subdirectory
( cd A/B/C &&
for dir in */; do
tar -cv -f "${dir%/}.tar" "$dir"
done )
The subshell around the whole command prevents the working directory from being changed in the rest of the shell/script, and ${dir%/}
removes the trailing slash at the end of the value in $dir
.
Unfortunately, the -C
option does not affect the working directory for the archive file specified by -f
, otherwise we could just have moved the -C
bit before the -f
option.
Alternatively, as a variant of the first loop:
for dir in A/B/C/*/; do
name=$( basename "$dir" )
( cd "$( dirname "$dir" )" && tar -cv -f "$name.tar" "$name" )
done
tree
) and ii) what you want to happen when you un-tar your tar file in a new location.