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
--appendis a GNU extension totar. If you're stuck with POSIXtar(the question currently is not tagged specifically enough to tell), the-uupdate option can be used to update an existing tar file.Andrew Henle– Andrew Henle2018-06-30 01:56:19 +00:00Commented 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.schily– schily2018-06-30 08:07:02 +00:00Commented Jun 30, 2018 at 8:07
- BTW: did you read my example that creates the archive in one pass using
star?schily– schily2018-06-30 08:08:42 +00:00Commented Jun 30, 2018 at 8:08
--transformto strip the paths - however the resulting archive would still include the (then empty) directories I thinkfindoptions, you could just remove the-maxdepth 1to enable recursion.