I want to tar compress every file in the directory, including the files in sub directories into a tar archive without any sub directories. So, that all the files are together in a single archive directory.
1 Answer
A solution , without copy using tar in append mode
find /etc -type f | (
CNT=1 ;
TARDST="/tmp/a_flat_archive.tar"
while read F ;
do
D=$(dirname $F) ;
SF=$(basename $F) ;
if [ $CNT -eq 1 ];
then
tar -C "$D" -cf $TARDST "$SF" ;
else
tar -C "$D" --append -f $TARDST "$SF" ;
fi ;
CNT=$(( $CNT +1 )) ;
done
)
-
Note that
--append
is a GNU extension totar
. If you're stuck with POSIXtar
(the question currently is not tagged specifically enough to tell), the-u
update option can be used to update an existing tar file. Jun 30, 2018 at 1:56 -
Both, the append option and -u are dangerous with GNU tar in case that the archive is not created with the same tar implementation or same command. GNU tar does not honor existing archive formats and happily creates archives that change the archive format in the middle. Such archives tend to be unreadable by other cleanly written archivers.– schilyJun 30, 2018 at 8:07
-
BTW: did you read my example that creates the archive in one pass using
star
?– schilyJun 30, 2018 at 8:08
--transform
to strip the paths - however the resulting archive would still include the (then empty) directories I thinkfind
options, you could just remove the-maxdepth 1
to enable recursion.